【问题标题】:How to store Terraform provisioner "local-exec" output in local variable and use variable value in "remote-exec"如何将 Terraform 配置器“local-exec”输出存储在局部变量中并在“remote-exec”中使用变量值
【发布时间】:2019-10-21 19:17:04
【问题描述】:

我正在使用 Terraform 供应商。在一种情况下,我需要执行“local-exec”供应商并将命令的输出 [This is array of IP addesses] 用于下一个“remote-exec”供应商。

而且我无法将“local-exec”临时输出存储在局部变量中以供以后使用。我可以将其存储在本地文件中,但不能存储在中间变量中

count = "${length(data.local_file.instance_ips.content)}" 

这不起作用。


resource "null_resource" "get-instance-ip-41" {
    provisioner "local-exec" {
         command = "${path.module}\\scripts\\findprivateip.bat  > ${data.template_file.PrivateIpAddress.rendered}"
    }
}


data "template_file" "PrivateIpAddress" {
    template = "/output.log"
}

data "local_file" "instance_ips" {
    filename = "${data.template_file.PrivateIpAddress.rendered}"
    depends_on = ["null_resource.get-instance-ip-41"]
}

output "IP-address" {
    value = "${data.local_file.instance_ips.content}"
}



# ---------------------------------------------------------------------------------------------------------------------
# Update the instnaces by installing newrelic agent using remote-exec
# ---------------------------------------------------------------------------------------------------------------------

resource "null_resource" "copy_file_newrelic_v_29" {

  depends_on = ["null_resource.get-instance-ip-41"]

  count = "${length(data.local_file.instance_ips.content)}"

  triggers = {
    cluster_instance_id =  "${element(values(data.local_file.instance_ips.content[count.index]), 0)}"
  }

  provisioner "remote-exec" {

    connection {
        agent               = "true"
        bastion_host        = "${aws_instance.bastion.*.public_ip}"
        bastion_user        = "ec2-user"
        bastion_port        = "22"
        bastion_private_key = "${file("C:/keys/nvirginia-key-pair-ajoy.pem")}"
        user                = "ec2-user"
        private_key         = "${file("C:/keys/nvirginia-key-pair-ajoy.pem")}"
        host                = "${self.triggers.cluster_instance_id}"
    }

    inline = [
      "echo 'license_key: 34adab374af99b1eaa148eb2a2fc2791faf70661' | sudo tee -a /etc/newrelic-infra.yml",
      "sudo curl -o /etc/yum.repos.d/newrelic-infra.repo https://download.newrelic.com/infrastructure_agent/linux/yum/el/6/x86_64/newrelic-infra.repo",
      "sudo yum -q makecache -y --disablerepo='*' --enablerepo='newrelic-infra'",
      "sudo yum install newrelic-infra -y" 
    ]
  }

} 

【问题讨论】:

  • 你不能。还有一些基于此的其他问题,这将是重复的,但现在没有时间找到它们。
  • 鉴于此问题是搜索 “terraform local exec output into variable”时的第一个问题,在答案中提供解释或在结束时指出相关问题将很有帮助(s) 与答案。

标签: variables terraform terraform-provider-aws


【解决方案1】:

很遗憾,你不能。我找到的解决方案是改用外部数据源块。您可以从那里运行命令并检索输出,唯一的问题是该命令需要将 json 生成到标准输出(stdout)。请参阅文档here。我希望这对尝试解决此问题的其他人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-22
    • 2023-03-05
    • 2013-10-11
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多