Skip to content

add module arg corresponding to cli refresh flag #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelogs/fragments/20250121-add-refresh-arg.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
minor_changes:
- Use "refresh" arg on plan command line (i.e. "terraform plan
-refresh=<arg>", defaults true), and allow user override in module args
(https://github.com/ansible-collections/cloud.terraform/pull/168)
2 changes: 2 additions & 0 deletions plugins/module_utils/terraform_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def plan(
target_plan_file_path: str,
targets: List[str],
destroy: bool,
refresh: bool,
state_args: List[str],
variables_args: List[str],
) -> Tuple[bool, bool, str, str]:
Expand All @@ -123,6 +124,7 @@ def plan(
command.extend(["-target", t])
if destroy:
command.append("-destroy")
command.append(f"-refresh={refresh}")
command.extend(state_args)
command.extend(variables_args)

Expand Down
11 changes: 11 additions & 0 deletions plugins/modules/terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@
that accepts locks (such as S3+DynamoDB).
type: int
version_added: 1.0.0
refresh:
description:
- Boolean argument for the C(-refresh=) option, to freshen terraform
resource/data state during Plan generation. State refresh normally
happens by default, but can be countermanded by giving a C(false)
value here.
type: bool
default: true
version_added: 3.2.0
force_init:
description:
- To avoid duplicating infra, if a state file can't be found this will
Expand Down Expand Up @@ -445,6 +454,7 @@ def main() -> None:
lock=dict(type="bool", default=True),
lock_timeout=dict(type="int"),
force_init=dict(type="bool", default=False),
refresh=dict(type="bool", default=True),
backend_config=dict(type="dict"),
backend_config_files=dict(type="list", elements="path"),
init_reconfigure=dict(type="bool", default=False),
Expand Down Expand Up @@ -570,6 +580,7 @@ def main() -> None:
plan_result_changed, plan_result_any_destroyed, plan_stdout, plan_stderr = terraform.plan(
target_plan_file_path=plan_file_to_apply,
targets=module.params.get("targets"),
refresh=module.params.get("refresh", True),
destroy=state == "absent",
state_args=get_state_args(state_file),
variables_args=variables_args,
Expand Down
Loading