Skip to content

Commit 6a5da75

Browse files
committed
Use '-refresh' cli arg for plan creation and add as module arg
Sometimes we want our task to create a plan without the usual implied state-refresh. The default behavior of Terraform (either no-arg, or '-refresh=true') is to always refresh state sources when the plan is created. When the user knows better, they can override, as they can already on cli.
1 parent aa17e71 commit 6a5da75

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
minor_changes:
3+
- Use "refresh" arg on plan command line (i.e. "terraform plan
4+
-refresh=<arg>", defaults true), and allow user override in module args
5+
(https://github.com/ansible-collections/cloud.terraform/pull/168)

plugins/module_utils/terraform_commands.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ def plan(
107107
target_plan_file_path: str,
108108
targets: List[str],
109109
destroy: bool,
110+
refresh: bool,
110111
state_args: List[str],
111112
variables_args: List[str],
112113
) -> Tuple[bool, bool, str, str]:
@@ -123,6 +124,7 @@ def plan(
123124
command.extend(["-target", t])
124125
if destroy:
125126
command.append("-destroy")
127+
command.append(f"-refresh={refresh}")
126128
command.extend(state_args)
127129
command.extend(variables_args)
128130

plugins/modules/terraform.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@
129129
that accepts locks (such as S3+DynamoDB).
130130
type: int
131131
version_added: 1.0.0
132+
refresh:
133+
description:
134+
- Boolean argument for the C(-refresh=) option, to freshen terraform
135+
resource/data state during Plan generation. State refresh normally
136+
happens by default, but can be countermanded by giving a C(false)
137+
value here.
138+
type: bool
139+
default: true
140+
version_added: 1.0.0
132141
force_init:
133142
description:
134143
- To avoid duplicating infra, if a state file can't be found this will
@@ -445,6 +454,7 @@ def main() -> None:
445454
lock=dict(type="bool", default=True),
446455
lock_timeout=dict(type="int"),
447456
force_init=dict(type="bool", default=False),
457+
refresh=dict(type="bool", default=True),
448458
backend_config=dict(type="dict"),
449459
backend_config_files=dict(type="list", elements="path"),
450460
init_reconfigure=dict(type="bool", default=False),
@@ -570,6 +580,7 @@ def main() -> None:
570580
plan_result_changed, plan_result_any_destroyed, plan_stdout, plan_stderr = terraform.plan(
571581
target_plan_file_path=plan_file_to_apply,
572582
targets=module.params.get("targets"),
583+
refresh=module.params.get("refresh", True),
573584
destroy=state == "absent",
574585
state_args=get_state_args(state_file),
575586
variables_args=variables_args,

0 commit comments

Comments
 (0)