我的问题类似于这个git hub帖子:
https://github.com/hashicorp/terraform/issues/745
它还与我的另一个堆栈交换帖有关:
Terraform stalls while trying to get IP addresses of multiple instances?
我正在尝试引导多个服务器,并且我需要在我的实例上运行几个命令,这些命令需要所有其他实例的IP地址。但是,在创建实例之前,我无法访问包含新创建实例的IP地址的变量。所以当我尝试像这样运行provisioner "remote-exec"
块时:
provisioner "remote-exec" {
inline = [
"sudo apt-get update",
"sudo apt-get install -y curl",
"echo ${openstack_compute_instance_v2.consul.0.network.0.fixed_ip_v4}",
"echo ${openstack_compute_instance_v2.consul.1.network.1.fixed_ip_v4}",
"echo ${openstack_compute_instance_v2.consul.2.network.2.fixed_ip_v4}"
]
}
没有任何事情发生,因为所有实例都在等待所有其他实例完成创建,因此首先不创建任何内容。所以我需要一种方法来创建我的资源,然后在创建它们之后运行我的provisioner "remote-exec"
块命令,terraform可以访问我所有实例的IP地址。
答案 0 :(得分:3)
解决方案是创建一个resource "null_resource" "nameYouWant" { }
,然后在其中运行命令。它们将在创建初始资源后运行:
resource "aws_instance" "consul" {
count = 3
ami = "ami-ce5a9fa3"
instance_type = "t2.micro"
key_name = "ansible_aws"
tags {
Name = "consul"
}
}
resource "null_resource" "configure-consul-ips" {
count = 3
connection {
user = "ubuntu"
private_key="${file("/home/ubuntu/.ssh/id_rsa")}"
agent = true
timeout = "3m"
}
provisioner "remote-exec" {
inline = [
"sudo apt-get update",
"sudo apt-get install -y curl",
"sudo echo '${join("\n", aws_instance.consul.*.private_ip)}' > /home/ubuntu/test.txt"
]
}
}
另见答案:
Terraform stalls while trying to get IP addresses of multiple instances?
非常感谢@ydaetskcor的回答
答案 1 :(得分:0)
除了@ alex-cohen答案外,还有https://github.com/hashicorp/terraform/issues/8266#issuecomment-454377049的另一个提示。
如果要发起对local-exec
的调用,无论创建资源如何,请使用triggers
:
resource "null_resource" "deployment" {
provisioner "local-exec" {
command = "echo ${PATH} > output.log"
}
triggers = {
always_run = "${timestamp()}"
}
}