Combining Variables Into One Comma-Delimited String?

2,497 views
Skip to first unread message

Tennis Smith

unread,
Jun 29, 2016, 5:05:39 PM6/29/16
to Terraform


Hi,

I'm trying to figure out how I can join several static IP addresses for passing to a module.  Once in the module, they will be split again.

Example:


foo.a = "1.1.1.1"
foo.b = "11.1.11.2"
foo.c = "10.1.1.3"


Given that construct, is there some way to join them (comma delimited) into a single variable?

I tried

access_ips = "${join(",",var.access_ip.*)}"

...but that didn't work.  Is this even possible?

TIA,
-T




Kevin Lee

unread,
Aug 3, 2016, 12:03:23 PM8/3/16
to Terraform
Try

access_ips = "${foo.a},${foo.b},${foo.c}"

Tennis Smith

unread,
Aug 4, 2016, 11:42:06 AM8/4/16
to terrafo...@googlegroups.com
I'll try that.  Thanks, Kevin,
-T

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to a topic in the Google Groups "Terraform" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/terraform-tool/c3s7_Mehi2M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to terraform-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/2d548b14-5d6a-4575-b9bb-978a42e97fea%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Kevin Lee

unread,
Aug 4, 2016, 2:58:04 PM8/4/16
to terrafo...@googlegroups.com
If you’re on 0.7.0 you can use native lists.

So, if your access_ip’s are defined like so in your vars file…

access_ips = [“1.1.1.1”, “2.2.2.2”, “3.3.3.3”]

Then you can just pass the list to the module so that you don’t have to split them again.  Make sure you declare the variable as type=list.

variable "access_ips" { type = "list" }

You can access them inside the module in whatever resource you need it in and iterate through the list if it supports “count"…. 

resource “null_resource” "my resource" {
  count = “3
  …
  provisioner “local-exec" {
    command = “echo ${access_ips[count.index]} > /dev/null"
  }

  …
}

Using lists avoids having to use whole join/split workaround that was necessary prior to 0.7.0.

Here’s a link to the git PR that implements native lists for more info...


To unsubscribe from this group and all its topics, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/CAK72EKwjsf5wL%2Bv1aKTNpW6Cwy2LcDaBQ6uiAatDeewf1M6Wrw%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages