From af8c4fb95e785bb9314f5837c2bf965f0a1b4d99 Mon Sep 17 00:00:00 2001 From: nbragin4 <139489942+nbragin4@users.noreply.github.com> Date: Tue, 28 Oct 2025 22:41:04 +0300 Subject: [PATCH] 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 Co-authored-by: Felix Fontein * Update plugins/modules/terraform.py Co-authored-by: Felix Fontein * Update plugins/modules/terraform.py Co-authored-by: Felix Fontein * Fix condition to check for None type in terraform.py --------- Co-authored-by: Felix Fontein --- .../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 b3ef1d6b93..c9799f7b50 100644 --- a/plugins/modules/terraform.py +++ b/plugins/modules/terraform.py @@ -572,7 +572,9 @@ def main(): command.append(f"-parallelism={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: @@ -589,7 +591,7 @@ def main(): ret_out.append(f'{k}={{{process_complex_args(v)}}}') elif isinstance(v, list): ret_out.append(f"{k}={process_complex_args(v)}") - elif isinstance(v, (int, float, str, bool)): + elif isinstance(v, (int, float, str, bool)) or v is None: ret_out.append(f'{k}={format_args(v)}') else: # only to handle anything unforeseen @@ -601,7 +603,7 @@ def main(): l_out.append(f"{{{process_complex_args(item)}}}") elif isinstance(item, list): l_out.append(f"{process_complex_args(item)}") - elif isinstance(item, (str, int, float, bool)): + elif isinstance(item, (str, int, float, bool)) or item is None: l_out.append(format_args(item)) else: # only to handle anything unforeseen