From 7733a3198645c5d3bbbd4248b14afcacf8af8adb Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Sat, 8 Jul 2023 13:41:40 +0530 Subject: [PATCH] feat: divide nodes into master and follower categories and show classification in ansible inventory file --- centos/conf.tf | 112 +++++++++++++++++++++++++------ centos/templates/hosts.yml.tftpl | 9 ++- 2 files changed, 98 insertions(+), 23 deletions(-) diff --git a/centos/conf.tf b/centos/conf.tf index 047bd0f..36a6bd7 100644 --- a/centos/conf.tf +++ b/centos/conf.tf @@ -28,17 +28,28 @@ resource "libvirt_volume" "centos8-qcow2" { format = "qcow2" } - - -variable "vm_count" { - default = 3 +variable "master_count" { + default = 1 } -resource "libvirt_volume" "domain_centos_basic_volume" { - name = "domain_centos_basic_volume-${count.index}" +variable "follower_count" { + default = 2 +} + + +resource "libvirt_volume" "domain_centos_master_volume" { + name = "domain_centos_master_volume-${count.index}" base_volume_id = libvirt_volume.centos8-qcow2.id - count = var.vm_count + count = var.master_count + size = 85368709120 +} + + +resource "libvirt_volume" "domain_centos_follower_volume" { + name = "domain_centos_follower_volume-${count.index}" + base_volume_id = libvirt_volume.centos8-qcow2.id + count = var.follower_count size = 85368709120 } @@ -61,11 +72,11 @@ resource "libvirt_cloudinit_disk" "commoninit" { pool = libvirt_pool.centos_basic.name } -# Create the machine -resource "libvirt_domain" "domain_centos_basic" { - count = var.vm_count +# Create the follower machine +resource "libvirt_domain" "domain_centos_follower" { + count = var.follower_count - name = "centos_basic_${count.index}" + name = "centos_follower_${count.index}" memory = "3000" vcpu = 4 @@ -92,7 +103,48 @@ resource "libvirt_domain" "domain_centos_basic" { } disk { - volume_id = element(libvirt_volume.domain_centos_basic_volume.*.id, count.index) + volume_id = element(libvirt_volume.domain_centos_follower_volume.*.id, count.index) + } + + graphics { + type = "spice" + listen_type = "address" + autoport = true + } +} + +# Create the machine +resource "libvirt_domain" "domain_centos_master" { + count = var.master_count + + name = "centos_master_${count.index}" + memory = "3000" + vcpu = 4 + + cloudinit = libvirt_cloudinit_disk.commoninit.id + + network_interface { + network_name = "default" + 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 = element(libvirt_volume.domain_centos_master_volume.*.id, count.index) } graphics { @@ -103,21 +155,34 @@ resource "libvirt_domain" "domain_centos_basic" { } locals { - vm_ips = [for i in libvirt_domain.domain_centos_basic : i.network_interface.0.addresses[0]] - vm_names = [for i in libvirt_domain.domain_centos_basic : i.name] + follower_vm_ips = [for i in libvirt_domain.domain_centos_follower : i.network_interface.0.addresses[0]] + follower_vm_names = [for i in libvirt_domain.domain_centos_follower : i.name] # vm_names = zipmap(vm_names, vm_ips) - vm_map = [for i in libvirt_domain.domain_centos_basic : { + follower_vm_map = [for i in libvirt_domain.domain_centos_follower : { ip = i.network_interface.0.addresses[0], name = i.name }] + + master_vm_ips = [for i in libvirt_domain.domain_centos_master : i.network_interface.0.addresses[0]] + master_vm_names = [for i in libvirt_domain.domain_centos_master : i.name] + # vm_names = zipmap(vm_names, vm_ips) + master_vm_map = [for i in libvirt_domain.domain_centos_master : { + ip = i.network_interface.0.addresses[0], + name = i.name + }] + # libvirt_domain.domain_centos_basic.*.network_interface.0.addresses[0] } - -output "centos_ip" { +output "centos_follower_ip" { #value = [local.vm_ips, local.vm_names] - value = local.vm_map + value = local.follower_vm_map +} + +output "centos_master_ip" { + #value = [local.vm_ips, local.vm_names] + value = local.master_vm_map } resource "local_file" "hosts_yml" { @@ -132,9 +197,14 @@ resource "local_file" "hosts_yml" { # EOT content = templatefile("./templates/hosts.yml.tftpl", { - vm_ips = local.vm_ips, - vm_names = local.vm_names, - vms = local.vm_map + follower_vm_ips = local.follower_vm_ips, + follower_vm_names = local.follower_vm_names, + follower_vms = local.follower_vm_map, + + master_vm_ips = local.master_vm_ips, + master_vm_names = local.master_vm_names, + master_vms = local.master_vm_map + }) filename = "./ansible/inventory/hosts.ini" diff --git a/centos/templates/hosts.yml.tftpl b/centos/templates/hosts.yml.tftpl index 157042d..8df3edd 100644 --- a/centos/templates/hosts.yml.tftpl +++ b/centos/templates/hosts.yml.tftpl @@ -1,4 +1,9 @@ -[debainbasic] -%{ for vm in vms ~} +[centos_master] +%{ for vm in master_vms ~} +${vm.name} ansible_host=${vm.ip} ansible_user=root +%{ endfor ~} + +[centos_follower] +%{ for vm in follower_vms ~} ${vm.name} ansible_host=${vm.ip} ansible_user=root %{ endfor ~}