feat: divide nodes into master and follower categories and show
classification in ansible inventory file
This commit is contained in:
parent
686ae05ce7
commit
7733a31986
2 changed files with 98 additions and 23 deletions
112
centos/conf.tf
112
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"
|
||||
|
|
|
@ -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 ~}
|
||||
|
|
Loading…
Reference in a new issue