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
This commit is contained in:
Ryan Brown 2018-05-31 14:35:40 -04:00 committed by GitHub
parent fa5c0282a4
commit 52b79a87ee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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':