tf-ansible-workflow

Terraform/Ansible Workflow for Libvirt
git clone https://git.in0rdr.ch/tf-ansible-workflow.git
Log | Files | Refs | Pull requests |Archive

commit a052b016b5c3a163889ca53b8e5d0ddc6635cb33
parent d3f6c928dff0fa2f5a05889939e598e8d45f7055
Author: Andreas Gruhler <andreas.gruhler@adfinis.com>
Date:   Wed, 30 Jun 2021 13:55:57 +0200

add type2 hosts and baseimage

Diffstat:
Alibvirt/terraform/inventory.tf | 15+++++++++++++++
Mlibvirt/terraform/terraform.tfvars.example | 7+++++--
Mlibvirt/terraform/variables.tf | 11+++++++++++
Alibvirt/terraform/vms-type2.tf | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mlibvirt/terraform/vms.tf | 17++---------------
5 files changed, 96 insertions(+), 17 deletions(-)

diff --git a/libvirt/terraform/inventory.tf b/libvirt/terraform/inventory.tf @@ -0,0 +1,14 @@ +resource "null_resource" "update_inventory" { + triggers = { + # when a host id changes + host_ids = join(" ", concat(values(libvirt_domain.host)[*].id, values(libvirt_domain.hosts_type2)[*].id)) + } + provisioner "local-exec" { + # recreate ansible inventory + command = "echo '${templatefile("${path.module}/templates/inventory.tpl", { hosts = merge(libvirt_domain.host, libvirt_domain.hosts_type2) })}' > ../ansible/inventory" + } + provisioner "local-exec" { + # recreate mapping of qemu VM id to hostnames + command = "echo '${templatefile("${path.module}/templates/qemu-config.yml.tpl", { hosts = merge(libvirt_domain.host, libvirt_domain.hosts_type2) })}' > ../ansible/qemu-config.yml" + } +} +\ No newline at end of file diff --git a/libvirt/terraform/terraform.tfvars.example b/libvirt/terraform/terraform.tfvars.example @@ -1,5 +1,7 @@ project = "myCluster" hosts = ["myCluster01", "myCluster02"] +# type2 hosts have a different baseimage, see baseimage_type2 below +type2_hosts = ["myCluster03"] # network = "10.66.3.0/24" # vcpu = 1 # memory = 1024 @@ -11,4 +13,5 @@ hosts = ["myCluster01", "myCluster02"] # baseimage = "https://cloud.centos.org/centos/8/x86_64/images/CentOS-8-GenericCloud-8.2.2004-20200611.2.x86_64.qcow2" # baseimage = "https://linuximages.de/openstack/arch/arch-openstack-LATEST-image-bootstrap.qcow2" # baseimage = "http://download.opensuse.org/distribution/leap/15.2/appliances/openSUSE-Leap-15.2-JeOS.x86_64-kvm-and-xen.qcow2" -# baseimage = "http://download.opensuse.org/tumbleweed/appliances/openSUSE-Tumbleweed-JeOS.x86_64-kvm-and-xen.qcow2" -\ No newline at end of file +# baseimage = "http://download.opensuse.org/tumbleweed/appliances/openSUSE-Tumbleweed-JeOS.x86_64-kvm-and-xen.qcow2" +# baseimage_type2 = "" +\ No newline at end of file diff --git a/libvirt/terraform/variables.tf b/libvirt/terraform/variables.tf @@ -8,6 +8,11 @@ variable "hosts" { default = ["node0", "node1"] } +variable "type2_hosts" { + type = list + default = ["node2", "node3"] +} + variable "vcpu" { type = number default = 1 @@ -48,6 +53,12 @@ variable "baseimage" { description = "URL to a qcow2 image used as backing image for all VMs" } +variable "baseimage_type2" { + type = string + default = "https://cloud-images.ubuntu.com/releases/xenial/release/ubuntu-16.04-server-cloudimg-amd64-disk1.img" + description = "URL to a qcow2 image used as backing image for type2 VMs" +} + variable "baseimage_format" { type = string default = "qcow2" diff --git a/libvirt/terraform/vms-type2.tf b/libvirt/terraform/vms-type2.tf @@ -0,0 +1,62 @@ +resource "libvirt_volume" "base_volume_type2" { +# resource "libvirt_volume_type2" "volume" { + name = "${var.project}-base" + pool = libvirt_pool.pool.name + source = var.baseimage_type2 + format = var.baseimage_format + + # todo: required for "terraform destroy" + depends_on = [libvirt_pool.pool] +} + +resource "libvirt_volume" "volume_type2" { + # unique image (based on backing file) for each host + for_each = toset(var.type2_hosts) + + name = "${var.project}-cow-${each.value}" + pool = libvirt_pool.pool.name + base_volume_id = libvirt_volume.base_volume_type2.id + size = var.disk +} + +resource "libvirt_domain" "hosts_type2" { + # create each host + for_each = toset(var.type2_hosts) + + name = each.value + memory = var.memory + vcpu = var.vcpu + + cloudinit = libvirt_cloudinit_disk.commoninit.id + qemu_agent = true + + network_interface { + network_name = libvirt_network.network.name + wait_for_lease = true + } + + # IMPORTANT: this is a known bug on cloud images, since they expect a console + # we need to pass it + # https://bugs.launchpad.net/cloud-images/+bug/1573095 + console { + type = "pty" + target_port = "0" + target_type = "serial" + } + + console { + type = "pty" + target_type = "virtio" + target_port = "1" + } + + disk { + volume_id = libvirt_volume.volume_type2[each.value].id + } + + graphics { + type = "spice" + listen_type = "address" + autoport = true + } +} +\ No newline at end of file diff --git a/libvirt/terraform/vms.tf b/libvirt/terraform/vms.tf @@ -202,18 +202,4 @@ resource "null_resource" "update_cloudinit" { # recreate cloudinit config command = "echo '${templatefile("${path.module}/templates/cloud_init.cfg.tpl", { public_key = tls_private_key.id_rsa.public_key_openssh })}' > ./cloud_init.cfg" } -} -resource "null_resource" "update_inventory" { - triggers = { - # when a host id changes - host_ids = join(" ", values(libvirt_domain.host)[*].id) - } - provisioner "local-exec" { - # recreate ansible inventory - command = "echo '${templatefile("${path.module}/templates/inventory.tpl", { hosts = libvirt_domain.host })}' > ../ansible/inventory" - } - provisioner "local-exec" { - # recreate mapping of qemu VM id to hostnames - command = "echo '${templatefile("${path.module}/templates/qemu-config.yml.tpl", { hosts = libvirt_domain.host })}' > ../ansible/qemu-config.yml" - } -} +} +\ No newline at end of file