From 52b79a87ee345c4191f4bdcf311644818ca10111 Mon Sep 17 00:00:00 2001 From: Ryan Brown Date: Thu, 31 May 2018 14:35:40 -0400 Subject: [PATCH] Terraform: Pass module targets to terraform plan as well as apply (#40958) * Pass module targets to terraform plan as well as apply * Fix undefined variable * Add target for plan-only operation --- lib/ansible/modules/cloud/misc/terraform.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/ansible/modules/cloud/misc/terraform.py b/lib/ansible/modules/cloud/misc/terraform.py index 381281bf32..0553ec9011 100644 --- a/lib/ansible/modules/cloud/misc/terraform.py +++ b/lib/ansible/modules/cloud/misc/terraform.py @@ -165,11 +165,15 @@ def init_plugins(bin_path, project_path): module.fail_json(msg="Failed to initialize Terraform modules:\r\n{0}".format(err)) -def build_plan(bin_path, project_path, variables_args, state_file, plan_path=None): +def build_plan(bin_path, project_path, variables_args, state_file, targets, plan_path=None): if plan_path is None: f, plan_path = tempfile.mkstemp(suffix='.tfplan') command = [bin_path, 'plan', '-input=false', '-no-color', '-detailed-exitcode', '-out', plan_path] + + for t in (module.params.get('targets') or []): + command.extend(['-target', t]) + command.extend(_state_args(state_file)) rc, out, err = module.run_command(command + variables_args, cwd=project_path) @@ -255,7 +259,7 @@ def main(): needs_application, changed = True, True if state == 'planned': - plan_file, needs_application = build_plan(command[0], project_path, variables_args, state_file, plan_file) + plan_file, needs_application = build_plan(command[0], project_path, variables_args, state_file, module.params.get('targets'), plan_file) if state == 'absent': # deleting cannot use a statefile needs_application = True @@ -266,7 +270,7 @@ def main(): elif plan_file and not os.path.exists(plan_file): module.fail_json(msg='Could not find plan_file "{0}", check the path and try again.'.format(plan_file)) else: - plan_file, needs_application = build_plan(command[0], project_path, variables_args, state_file, plan_file) + plan_file, needs_application = build_plan(command[0], project_path, variables_args, state_file, module.params.get('targets'), plan_file) command.append(plan_file) if needs_application and not module.check_mode and not state == 'planned':