-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow terraform module to specify complex variable structures #4797
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution! Please add a changelog fragment.
Please also note that we had a similar proposal in the past (#4281) which had to be reverted for breaking backwards compatibility. It looks to me like your approach should work better, but I'm not using terraform so I cannot really judge.
Added the change log fragment. |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as resolved.
This comment was marked as resolved.
@@ -449,12 +480,72 @@ def main(): | |||
if state == 'present' and module.params.get('parallelism') is not None: | |||
command.append('-parallelism=%d' % module.params.get('parallelism')) | |||
|
|||
def format_args(vars): | |||
if isinstance(vars, str): | |||
return '"{string}"'.format(string=vars) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should better use json.dumps()
here, otherwise there will be problems with properly escaping quotes and other things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although json.dumps()
do encode special characters, so is the shlex_quote()
. Terraform did not like the double encapsulation, although the code/commands work without fail, it did not pass the correct string from ansible to terraform. I created a test case in https://github.com/kosalaat/terrafrom-tests using terraform null_resource, which now test all the scenarios, including checking whether string are passed in intact.
Is there a way we can make this part of the repo?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the above TF workfiles, the following ansible playbooks works:
---
- hosts: localhost
gather_facts: false
tasks:
- community.general.terraform:
project_path: "../../terraform/null-resource"
state: present
variables:
dictionaries:
name: kosala
age: 44
list_of_strings:
- kosala
- neloufer
- punu
- ari
list_of_objects:
- name: kosala
age: 44
- name: neloufer
age: 44
- name: punu
age: 9
- name: ari
age: 1
boolean: true
string_type: "randomstring2&$%@"
list_of_lists:
- [ 1 ]
- [ 11, 12, 13 ]
- [ 2 ]
- [ 3 ]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although
json.dumps()
do encode special characters, so is theshlex_quote()
. Terraform did not like the double encapsulation,
I'm not sure what you mean. There is no shlex_quote()
involved anywhere (with the variables you are touching).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if string
happens to be a"b
here? Then terraform will get passed "a"b"
. I'm pretty sure it will not be happy with that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No it's not. shlex_quote()
inside run_command()
will sort that out. More explanation in the next comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still wondering how strings containing "
will be handled. Simply adding quotes around them is probably not ok?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You want to look at https://www.terraform.io/language/expressions/strings#escape-sequences to see how to correctly quote strings.
Terrform variable types are mapped to ansible veriable types
Currently handles Dict, List, Str, Int, Bool types
Updated the documentation accordingly
Updated with an example.
SUMMARY
Added the capability to specify complex variables structures in the terraform module. With this change we can specify Terraform Object / Nested type variables via ansible:
For example, in TF:
Would translate to:
ISSUE TYPE
COMPONENT NAME
terraform
ADDITIONAL INFORMATION
Changed the variable_args to be constructed via a recursive function process_args. It's capable of handling nested lists, dictionaries, bools, string and numbers.