[英]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:
我可以通过以下方式检索主机的 state:
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.在状态文件中,我可以看到分配了哪些安全组,因此我可以提前 go 并将它们写下来。 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亲切的问候罗马
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: security_groups 元素的 output 的结构与输入的结构相同,因此您可以非常简单地将 security_group output 作为变量分配给新主机:
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"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.