diff --git a/centos/README.md b/centos/README.md new file mode 100644 index 0000000..6db8073 --- /dev/null +++ b/centos/README.md @@ -0,0 +1,14 @@ +# Lab of Centos Machines + +Fetch the latest CentOS Stream 8 cloud image. + +```sh +sudo curl -Lo /var/lib/libvirt/images/centos-stream-8.qcow2 http://cloud.centos.org/centos/8-stream/x86_64/images/CentOS-Stream-GenericCloud-8-20201217.0.x86_64.qcow2 +``` + +Then, deploy the lab. + +```sh +terraform init +terraform apply +``` diff --git a/centos/ansible/prepare.yaml b/centos/ansible/prepare.yaml new file mode 100644 index 0000000..c7305f1 --- /dev/null +++ b/centos/ansible/prepare.yaml @@ -0,0 +1,20 @@ +- name: Sample playbook + hosts: all + become: yes + gather_facts: no + pre_tasks: + - name: Wait for all nodes to become ready + wait_for_connection: + delay: 5 + timeout: 60 + - name: Wait for cloud-init to finish + raw: test -f /var/lib/cloud/instance/boot-finished + retries: 12 + delay: 5 + register: result + until: result.rc == 0 + changed_when: false + - name: Gather facts + setup: + tasks: + - ping: diff --git a/centos/ansible/requirements.yml b/centos/ansible/requirements.yml new file mode 100644 index 0000000..61f68a1 --- /dev/null +++ b/centos/ansible/requirements.yml @@ -0,0 +1,3 @@ +collections: [] +#- collection1 +#- collection2 diff --git a/centos/centos.tf b/centos/centos.tf new file mode 100644 index 0000000..309f32c --- /dev/null +++ b/centos/centos.tf @@ -0,0 +1,48 @@ + +resource "libvirt_cloudinit_disk" "centos_cloudinit" { + name = "centos-cloudinit.iso" + user_data = file("${path.module}/templates/cloud-init.cfg") + network_config = file("${path.module}/templates/network-config.cfg") + pool = var.pool_name +} + +resource "libvirt_volume" "centos_disk" { + name = "${format(var.centos_hostname_format, count.index + 1)}.${var.volume_format}" + count = var.centos_machine_count + format = var.volume_format + pool = var.pool_name + base_volume_name = "${var.centos_image}.${var.volume_format}" +} + +resource "libvirt_domain" "centos_machine" { + count = var.centos_machine_count + name = format(var.centos_hostname_format, count.index + 1) + vcpu = "1" + memory = "1024" + cloudinit = libvirt_cloudinit_disk.centos_cloudinit.id + autostart = true + + disk { + volume_id = element(libvirt_volume.centos_disk.*.id, count.index) + } + + # Makes the tty0 available via `virsh console` + console { + type = "pty" + target_port = "0" + } + + network_interface { + network_id = libvirt_network.lab_net.id + hostname = format(var.centos_hostname_format, count.index + 1) + + # When creating the domain resource, wait until the network interface gets + # a DHCP lease from libvirt, so that the computed IP addresses will be + # available when the domain is up and the plan applied. + wait_for_lease = true + } +} + +locals { + centos_machines = { for i in libvirt_domain.centos_machine : i.name => i.network_interface.0.addresses[0] } +} diff --git a/centos/main.tf b/centos/main.tf new file mode 100644 index 0000000..c4af6a0 --- /dev/null +++ b/centos/main.tf @@ -0,0 +1,47 @@ +terraform { + required_version = ">= 0.13" + required_providers { + libvirt = { + source = "dmacvicar/libvirt" + version = ">=0.6.3" + } + local = { + source = "hashicorp/local" + version = ">=2.0.0" + } + template = { + source = "hashicorp/template" + version = ">=2.2.0" + } + } +} + +output "machines" { + value = local.centos_machines +} + +resource "local_file" "ansible_inventory" { + content = templatefile("${path.module}/templates/inventory", { centos_machines = local.centos_machines, network_domain = var.network_domain }) + filename = "ansible/inventory" + file_permission = "0644" + + provisioner "local-exec" { + working_dir = "${path.module}/ansible" + command = <