Help passing list from Ansible to Terraform list variable

All:

I am using the terraform module and have a terraform plan that contains a list type variable. (Ansible 2.7.2)

It may be my lack of deep knowledge of YAML, but every attempt I’ve tried, results in a validation error.

My suspicion is that terraform expects the list to be passed in a certain format. This is based on the terraform docs which show that one can pass a list in as environment variable with the format:
`

$ TF_VAR_somelist='["ami-abc123", "ami-bcd234"]' terraform plan

`

But I have been utterly unable to figure out how to pass this format from Ansible (i.e. yaml) to Terraform.

Does anyone know how I can get this to work without resorting to an environment variable.

Thx,

-steve

I haven’t used the terraform module. but have just had a look at the Ansible doc for it, and it describes a “variables” parameter as “a group of key-values to override template variables or those in variables files.” Seems that this format in ansible would work to pass that parameter:

  • terraform:
    […]
    variables:
    plain_var: “string value”
    list_var: [ “list_element1”, “list_element_2” ]

map_var:
one_thing: “blah”
otherthing: “bloo”

[…]

Note that “variables” is the literal name of the attribute; it is literally the word “variables”.

However, I suspect that “plain_var”, “list_var” etc need to be variable names that Terraform expects to see, and the contents have to be forms that Terraform expects.

I can see several ways this might not be working:

  • you have a syntax error in your Ansible code; this would cause an error message from Ansible, and Terraform would not be called at all.
  • you are not using the “variables” attribute, and are trying to pass the variable directly as an attribute to the terraform module. This would cause some kind of “unrecognised attribute” error in Ansible, and Terraform would not be called at all.
  • you are using the “variables” attribute, but passing a variable name in it that Terraform doesn’t know about; this would cause some kind of error in Terraform
  • you are using the “variables” attribute and you are using a variable name known to Terraform, but the contents of the variable you are passing are in some way incomplete or defective. This would cause some kind of error in Terraform.
    It would be helpful to see the actual code you are trying and the actual errors produced when you do.

Regards, K.

Hi Karl:

Thanks for the reply. I was hoping it was something obvious :-). This is with Ansible 2.7.4 and Terraforn 0.11.11. Currently my workaround is to pass it in through an environment variable which would not be my preferred means.

Following are the details:

Ansible playbook: TF_LOG=DEBUG ansible-playbook -i localhost -vvvv test_terraform_list.yaml

`

Wild shot in the dark, try this:

variables:
- test_list: [ "list_element1", "list_element_2" ]
- test2: "Hi"

That’s a list of maps…

Regards, K.

  • test