From 7ade6075257f9329819f2ad3672449d5b42eafc8 Mon Sep 17 00:00:00 2001 From: Mikhail Naletov <36846182+okgolove@users.noreply.github.com> Date: Thu, 1 Mar 2018 19:50:49 +0200 Subject: [PATCH] Add variables and variable_file in terraform module (#36879) * Fixes #36837 Added terraform variables for validate command * Fixes #36837 Changed method from append to extend. --- lib/ansible/modules/cloud/misc/terraform.py | 24 ++++++++++----------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/ansible/modules/cloud/misc/terraform.py b/lib/ansible/modules/cloud/misc/terraform.py index cfc26dfd18..dd0fcdd33e 100644 --- a/lib/ansible/modules/cloud/misc/terraform.py +++ b/lib/ansible/modules/cloud/misc/terraform.py @@ -136,13 +136,13 @@ APPLY_ARGS = ('apply', '-no-color', '-auto-approve=true') module = None -def preflight_validation(bin_path, project_path, variables_file=None, plan_file=None): +def preflight_validation(bin_path, project_path, variables_args=None, plan_file=None): if not os.path.exists(bin_path): module.fail_json(msg="Path for Terraform binary '{0}' doesn't exist on this host - check the path and try again please.".format(project_path)) if not os.path.isdir(project_path): module.fail_json(msg="Path for Terraform project '{0}' doesn't exist on this host - check the path and try again please.".format(project_path)) - rc, out, err = module.run_command([bin_path, 'validate'], cwd=project_path) + rc, out, err = module.run_command([bin_path, 'validate'] + variables_args, cwd=project_path) if rc != 0: module.fail_json(msg="Failed to validate Terraform configuration files:\r\n{0}".format(err)) @@ -209,7 +209,16 @@ def main(): else: command = [module.get_bin_path('terraform')] - preflight_validation(command[0], project_path) + variables_args = [] + for k, v in variables.items(): + variables_args.extend([ + '-var', + '{0}={1}'.format(k, v) + ]) + if variables_file: + variables_args.extend(['-var-file', variables_file]) + + preflight_validation(command[0], project_path, variables_args) if state == 'present': command.extend(APPLY_ARGS) @@ -224,15 +233,6 @@ def main(): if module.params.get('lock_timeout') is not None: command.append('-lock-timeout=%ds' % module.params.get('lock_timeout')) - variables_args = [] - for k, v in variables.items(): - variables_args.extend([ - '-var', - '{0}={1}'.format(k, v) - ]) - if variables_file: - variables_args.append('-var-file', variables_file) - for t in (module.params.get('targets') or []): command.extend(['-target', t])