From b8f55cccdfdae6135670dcaf7d5efabd5e6c9277 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Tue, 28 Oct 2025 21:23:35 +0100 Subject: [PATCH] [PR #11003/9cb619ff backport][stable-10] [stable-11] terraform: Fix bug when None values aren't processed correctly (#10961) (#11004) [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) (cherry picked from commit 9cb619ff6c98d0f2051ab9b84a3b13c29b910325) Co-authored-by: Felix Fontein 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 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