I currently use Terraform to create Openstack instances with different modules. Here is the module instances
and a small bit of its definition :
// Module
module "instances" {
source = "./modules/instance"
for_each = local.instances
env = var.env
type = each.key
icount = each.value.count
image = each.value.image
flavor = each.value.flavor
volume = each.value.volume
volumes = each.value.volumes
network_id = local._network.id
servergroup_name = var.servergroups ? each.value.servergroup_name : null
dns = each.value.dns
networks = each.value.networks
//[...]
}
// Instance definition
resource "openstack_compute_instance_v2" "instance" {
count = var.icount
name = "${var.env}-${var.type}-${count.index + 1}"
flavor_name = var.flavor
block_device {
uuid = openstack_blockstorage_volume_v3.boot[count.index].id
source_type = "volume"
boot_index = 0
destination_type = "volume"
delete_on_termination = var.volume.delete
}
//[...]
scheduler_hints {
group = openstack_compute_servergroup_v2.servergroup.id // This bit does not work
}
}
// The .tfvars file used to declare variables that are use to create
// the whole infrastructure
instances = {
nginx = {
image = "rhel-9.1"
servergroup_name = "nginx-aa"
// [...]
flavor = "m4.medium"
volume = { size = 50, delete = false }
}
}
+ servergroups = {
+ "nginx-aa" = {
+ policy = "soft-anti-affinity"
+ }
+ }
Just in case, here is the directory architecture of my Terraform
├── 00-locals.tf
├── 01-cloud_init.tf
├── 02-network.tf
├── 03-security_groups.tf
├── 04-infra.tf
├── modules
│ ├── instance
│ │ ├── main.tf
│ │ ├── output.tf
│ │ ├── secgroup
│ │ │ ├── main.tf
│ │ │ ├── variable.tf
│ │ │ └── versions.tf
│ │ ├── variable.tf
│ │ └── versions.tf
│ ├── secgroup
│ │ ├── main.tf
│ │ ├── main.tf.ori
│ │ ├── output.tf
│ │ ├── variable.tf
│ │ └── versions.tf
│ └── servergroup
│ ├── main.tf
│ ├── variables.tf
│ └── versions.tf
├── variable.tf
└── versions.tf
I would like now to add a feature to first create a servergroup and add some instances to this servergroup. How to find the id of the servergroup called "nginx-aa" and use it in the group
directive of the scheduler_hints
block in the resource openstack_compute_instance_v2
?
At that time I already wrote a servergroups module, but what I did wrong was the way I summoned the servergroup id. That's what actually worked in the instance module :
module "instances" {
//[...]
servergroup = each.value.servergroup
servergroup_id = module.servergroups[each.value.servergroup].srvgrp_id
}
srvgrp_id
is an output containing this :
output "srvgrp_id" {
value = openstack_compute_servergroup_v2.servergroup.id
}