mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-27 23:21:22 -07:00
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:
parent
fa5c0282a4
commit
52b79a87ee
1 changed files with 7 additions and 3 deletions
|
@ -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))
|
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:
|
if plan_path is None:
|
||||||
f, plan_path = tempfile.mkstemp(suffix='.tfplan')
|
f, plan_path = tempfile.mkstemp(suffix='.tfplan')
|
||||||
|
|
||||||
command = [bin_path, 'plan', '-input=false', '-no-color', '-detailed-exitcode', '-out', plan_path]
|
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))
|
command.extend(_state_args(state_file))
|
||||||
|
|
||||||
rc, out, err = module.run_command(command + variables_args, cwd=project_path)
|
rc, out, err = module.run_command(command + variables_args, cwd=project_path)
|
||||||
|
@ -255,7 +259,7 @@ def main():
|
||||||
needs_application, changed = True, True
|
needs_application, changed = True, True
|
||||||
|
|
||||||
if state == 'planned':
|
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':
|
if state == 'absent':
|
||||||
# deleting cannot use a statefile
|
# deleting cannot use a statefile
|
||||||
needs_application = True
|
needs_application = True
|
||||||
|
@ -266,7 +270,7 @@ def main():
|
||||||
elif plan_file and not os.path.exists(plan_file):
|
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))
|
module.fail_json(msg='Could not find plan_file "{0}", check the path and try again.'.format(plan_file))
|
||||||
else:
|
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)
|
command.append(plan_file)
|
||||||
|
|
||||||
if needs_application and not module.check_mode and not state == 'planned':
|
if needs_application and not module.check_mode and not state == 'planned':
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue