From 9cb619ff6c98d0f2051ab9b84a3b13c29b910325 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Tue, 28 Oct 2025 20:53:57 +0100 Subject: [PATCH] [stable-11] terraform: Fix bug when None values aren't processed correctly (#10961) (#11003) terraform: Fix bug when None values aren't processed correctly (#10961) * terraform: Fix bug when None values aren't processed correctly Just found that i can't pass null values as complex variables into terraform using this module, while i can do that with terraform itself. Fixed undesired behavior. * chore: changelog fragment 10961-terraform-complexvars-null-bugfix.yaml * Update changelogs/fragments/10961-terraform-complexvars-null-bugfix.yaml * Update plugins/modules/terraform.py * Update plugins/modules/terraform.py * Fix condition to check for None type in terraform.py --------- (cherry picked from commit af8c4fb95e785bb9314f5837c2bf965f0a1b4d99) Co-authored-by: nbragin4 <139489942+nbragin4@users.noreply.github.com> --- .../10961-terraform-complexvars-null-bugfix.yaml | 2 ++ plugins/modules/terraform.py | 8 +++++--- 2 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/10961-terraform-complexvars-null-bugfix.yaml 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 5e4742d07c..c073d7a1ba 100644 --- a/plugins/modules/terraform.py +++ b/plugins/modules/terraform.py @@ -584,7 +584,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: @@ -601,7 +603,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 @@ -613,7 +615,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