diff --git a/changelogs/fragments/10961-terraform-complexvars-null-bugfix.yaml b/changelogs/fragments/10961-terraform-complexvars-null-bugfix.yaml new file mode 100644 index 0000000000..284da1f888 --- /dev/null +++ b/changelogs/fragments/10961-terraform-complexvars-null-bugfix.yaml @@ -0,0 +1,2 @@ +bugfixes: + - terraform - fix bug when ``null`` values inside complex vars are throwing error instead of being passed to terraform. Now terraform can handle ``null``s in ``complex_vars`` itself (https://github.com/ansible-collections/community.general/pull/10961). diff --git a/plugins/modules/terraform.py b/plugins/modules/terraform.py index 6a8ba58974..05bbf140c4 100644 --- a/plugins/modules/terraform.py +++ b/plugins/modules/terraform.py @@ -548,7 +548,9 @@ def main(): command.append('-parallelism=%d' % module.params.get('parallelism')) def format_args(vars): - if isinstance(vars, str): + if vars is None: + return 'null' + elif isinstance(vars, str): return '"{string}"'.format(string=vars.replace('\\', '\\\\').replace('"', '\\"')).replace('\n', '\\n') elif isinstance(vars, bool): if vars: @@ -565,7 +567,7 @@ def main(): ret_out.append('{0}={{{1}}}'.format(k, process_complex_args(v))) elif isinstance(v, list): ret_out.append("{0}={1}".format(k, process_complex_args(v))) - elif isinstance(v, (integer_types, float, str, bool)): + elif isinstance(v, (integer_types, float, str, bool)) or v is None: ret_out.append('{0}={1}'.format(k, format_args(v))) else: # only to handle anything unforeseen @@ -577,7 +579,7 @@ def main(): l_out.append("{{{0}}}".format(process_complex_args(item))) elif isinstance(item, list): l_out.append("{0}".format(process_complex_args(item))) - elif isinstance(item, (str, integer_types, float, bool)): + elif isinstance(item, (str, integer_types, float, bool)) or item is None: l_out.append(format_args(item)) else: # only to handle anything unforeseen