如何在本地变量中存储Terraform Provisioner“本地执行”输出并在“远程执行”中使用变量值

时间:2019-06-06 09:31:30

标签: variables terraform terraform-provider-aws

我正在与Terraform Provisionar合作。在一种情况下,我需要执行一个“ local-exec”临时服务器,并使用命令的输出[这是IP地址数组]进入下一个“ 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" 
    ]
  }

} 

1 个答案:

答案 0 :(得分:2)

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