terraformopenstackterraform-provider-openstack

Terraform Openstack: deploy new instance with same security groups as existing one


Assume I already have an existing host with security groups assigned to it. I can retrieve the state of the host the following way:

data "openstack_compute_instance_v2" "host_data_01" {
  # ID for host A
  id = "88c96f9d-7951-4ab5-9e37-f1c5b33e8889"
}

In the statefile I can see what security groups are assigned so I can just go ahead and write them down. But is there a way to assign all security groups of that host to my new instance I'm trying to deploy?

An example of assigning the first security group entry of that set.

security_groups = [
  element(sort(data.openstack_compute_instance_v2.host_data_01.security_groups), 0)
]

In pseudo-code what I want is:

security_groups = [
  for entry in data.module.module_name.security_groups
    add entry
]

Kind regards Roman


Solution

  • After a few days of taking a break from it, I found the incredibly simple solution.

    The output of a security_groups element is structured the same way as an input should be, so you can very simply assign a security_group output as a variable to a new host:

    resource "openstack_compute_instance_v2" "host_01" {
      ...
      security_groups = data.openstack_compute_instance_v2.test_host.security_groups  
      ...
    }
    
    data "openstack_compute_instance_v2 "test_host" {
      id = "12345"
    }