Check-mode simulation accuracy for state-dependent Ansible modules
0 reputation · 16 Jan 2021, 14:15 UTC
0 reputation · 16 Jan 2021, 14:15 UTC
check_mode variable.
A significant challenge arises when using complex, state-dependent scripts or modules that do not explicitly handle check-run logic. In these instances, the module may report a false positive by indicating a change will occur, or may fail to simulate the state transition entirely because certain side effects are only triggered during actual execution.
When combining --check with --diff, Ansible attempts to show the intended changes, but it cannot accurately predict outcomes for tasks where the final state logic is opaque to the simulation engine.
34775 reputation · 16 Jan 2021, 14:46 UTC
To ensure a custom Python module provides an accurate check-mode simulation, you must explicitly implement a read-only state comparison logic that mirrors the actual modification path without executing the change. Ansible does not automatically simulate state transitions; it relies on the module to report whether a change would have occurred.
For a module to be "check-mode aware," it must handle the check_mode boolean passed via the module arguments. Follow these implementation patterns:
changed=True and return the result.if not module.check_mode:.--diff, calculate the difference between the current and desired state during the check-mode pass and include this in the diff key of the return dictionary.supports_check_mode=False in the module's metadata. This prevents the module from reporting misleading results.There is no native Ansible engine mechanism to automatically scan a playbook and detect "improperly" implemented check-mode logic. Because the engine trusts the module's return value, a module that fails to implement check_mode logic but returns changed=False will appear successful but be functionally invisible during a dry run.
To identify these gaps, use the following verification workflow:
--check --diff. Note all tasks reported as ok (unchanged).--check. Any task that reports changed during the actual run but reported ok during the check run is an improperly implemented module.--check and verify via system logs or manual inspection that no actual changes occurred on the target node.# Verify if a custom module accurately predicts change
ansible-playbook site.yml --check --diff
# Compare result with actual execution
ansible-playbook site.yml
Missing Diagnostic Detail: Are you utilizing the AnsibleModule` base class or writing raw Python scripts? The implementation of the check_mode variable differs slightly between the legacy API and the modern AnsibleModule class.
Use comments to ask for clarification. Post a solution as an answer.
6,260 reputation · 17 Jan 2021, 01:33 UTC
One gap worth flagging: when a module declares supports_check_mode: false, Ansible skips it during --check rather than reporting a failure, so a playbook can look green while the task never simulated its state change. That makes detection harder than simply scanning for errors.
For custom modules, the safest verification is to unit-test the check_mode branch directly—instantiate AnsibleModule with supports_check_mode=True and assert that exit_json returns the predicted diff without calling mutating APIs. In CI, combine ansible-lint check-mode rules with a disposable-target --check --diff run, because become plugins and async handlers can behave differently outside the controller.