Terraform 在尝试获取多个实例的 IP 地址时停止? [英] Terraform stalls while trying to get IP addresses of multiple instances?

查看:24
本文介绍了Terraform 在尝试获取多个实例的 IP 地址时停止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我使用 terraform 来配置 ec2 实例和 openstack 实例.我正在尝试引用我正在创建的实例的 IP 地址,因为我需要运行使用它们的命令(以设置领事).但是,在添加对这些变量的引用之后,terraform 就会停止运行,并且在我运行 terraform applyterraform plan 后什么都不做:

So I am using terraform to provision ec2 instances and openstack instances. I am trying to reference the IP addresses of the instances I am creating because I need to run commands that use them (to set up consul). However after adding references to these variables terraform just stalls out and does absolutely nothing after I run a terraform apply or terraform plan:

这是我尝试运行的资源块示例:

Here is a sample of the resource block for what I am trying to run:

resource "aws_instance" "consul" {
  count = 3
  ami = "ami-ce5a9fa3"
  instance_type = "t2.micro"
  key_name = "ansible_aws"
  tags {
    Name = "consul"
  }

  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",
      "echo ${aws_instance.consul.0.private_ip} >> /home/ubuntu/test.txt",
      "echo ${aws_instance.consul.1.private_ip} >> /home/ubuntu/test.txt",
      "echo ${aws_instance.consul.2.private_ip} >> /home/ubuntu/test.txt"
    ]
  }
}

更新:所以我尝试在我的 openstack 云上运行类似的命令并遇到了同样的问题:

Update: so I tried running a similar command with my openstack cloud and got the same problem:

  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}"
    ]
  }

所以我发现如果我只使用其中一个 IP 地址,那么在创建第一个实例之前,甚至不会创建其他实例,如下面的块中所示:

So I have found if instead I use only one of the IP addresses then the other instances wont even be created until my first instance is created, like in the block below:

  provisioner "remote-exec" {
    inline = [
      "echo ${openstack_compute_instance_v2.consul.0.network.0.fixed_ip_v4}",
    ]
  }

我需要同时创建我的所有实例,并在创建后立即访问所有其他实例的 IP 地址.

I need all my instances to be created at the same time and have access to the IP addresses of all the other instances created as soon as they are created.

推荐答案

所以仔细看一下,它看起来实际上是一个 Terraform 的问题,它被锁定在依赖循环中.

So taking a longer look at this it looks like it is actually an issue with Terraform where it gets locked into a dependency cycle.

同样的问题还包括使用 null_resource 然后连接到所有配置的实例并针对它们运行脚本.

That same issue also includes a workaround for this by using a null_resource to then connect to all of the provisioned instances and run a script against them.

对于您的用例,您可以使用如下内容:

For your use case you could use something like this:

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("
", aws_instance.consul.*.private_ip)}' > /home/ubuntu/test.txt"
    ]
  }
}

这应该会启动 3 个实例,然后连接到它们,安装 curl,然后创建一个文件,其中包含 3 个实例的私有 IP 地址的新行分隔列表.

This should bring up 3 instances and then connect to them, install curl and then create a file with a new line separated list of the private IP addresses of the 3 instances.

这篇关于Terraform 在尝试获取多个实例的 IP 地址时停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆