ajoy sinha
ajoy sinha

Reputation: 1236

How to store Terraform provisioner "local-exec" output in local variable and use variable value in "remote-exec"

I am working with Terraform provisionar. and in one scenario I need to execute a 'local-exec' provisionar and use the output [This is array of IP addesses] of the command into next 'remote-exec' provisionar.

And i am not able to store the 'local-exec' provisionar output in local variable to use later. I can store it in local file but not in intermediate variable

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

this is not working.


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

} 

Upvotes: 25

Views: 22908

Answers (2)

Chiel
Chiel

Reputation: 2169

You can do it, sort of. It's a bit elaborate. In my use case, I wanted to get the home directory to store SSH keys there.

Creating a shell script:

HOME_DIR_PATH=$(echo ~/ | sed 's:/*$::')
jq -n --arg home_dir_path "$HOME_DIR_PATH" '{"home_dir_path":$home_dir_path}'

And it my Terraform code I used:

data "external" "home_dir" {
  program = ["bash", "${path.module}/get_home_dir.sh"]
}

locals {
  home_dir_path = data.external.home_dir.result.home_dir_path
}

resource "local_file" "private_key" {
  content         = tls_private_key.ssh_key.private_key_openssh
  filename        = "${local.home_dir_path}/.ssh/${var.deployment_name}"
  file_permission = "0600"
}

Obviously, you can skip the creation of the local variable. But that is the closest you can get to a real variable.

Upvotes: 0

Jenny
Jenny

Reputation: 166

Unfortunately you can't. The solution I have found is to instead use an external data source block. You can run a command from there and retrieve the output(s), the only catch is that the command needs to produce json to standard output (stdout). See documentation here. I hope this is some help to others trying to solve this problem.

Upvotes: 15

Related Questions