Arbab Nazar
Arbab Nazar

Reputation: 23811

quote around the output value using terraform "local-exec" provisioner

I want to put the quote around the output value(in simple, I want to write the output to the file using "local-exec" provisioner but unfortunately it didn't put the quote around it although it did echo the correct value to the file. I have also used the escape character () but still no luck. Any help will be highly appreciate. Thanks

code snippet for reference:

provisioner "local-exec" {
command = " echo **ELB_DNS_NAME: \"${aws_elb.elb.dns_name}\"** >> ${var.name}.yml"
}

Upvotes: 1

Views: 2323

Answers (2)

cloudartisan
cloudartisan

Reputation: 656

Use the template_file data source and the file provisioner so you don't have to mess with quoting/escaping characters.

data "template_file" "name" {
  template = "${file("${path.root}/templates/name.tpl")}"
  vars {
      elb_dns_name = "${aws_elb.elb.dns_name}"
  }
}

[...]

resource "null_resource" "render_templates"
  provisioner "file" {
    content = "${data.template_file.name.rendered}"
    destination = "name.yml"
  }
}

In <your root TF directory>/templates/name.tpl you'd simply have:

**ELB_DNS_NAME: "${elb_dns_name}"**

Upvotes: 0

Liam
Liam

Reputation: 1171

I'm not sure if this is the simplest or most ideal solution, but I imagine the multi-line string syntax should work, as seen here and documented here.

resource "aws_iam_user_policy" "lb_ro" {
    name = "test"
    user = "${aws_iam_user.lb.name}"
    policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": [
        "ec2:Describe*"
      ],
      "Effect": "Allow",
      "Resource": "*"
    }
  ]
}
EOF
}

resource "aws_iam_user" "lb" {
  name = "loadbalancer"
  path = "/system/"
}

resource "aws_iam_access_key" "lb" {
  user = "${aws_iam_user.lb.name}"
}

Upvotes: 0

Related Questions