Sometimes, it can be desirable to completely suppress changes. This is often used when executing a command in order to gather data. The command execution isn't actually changing anything; instead, it's just gathering info, like the setup module. Suppressing changes on such tasks can be helpful for quickly determining whether a playbook run resulted in any actual change in the fleet.
To suppress change, simply use false as an argument to the changed_when task key. Let's extend one of our previous examples to discover the active iscsi sessions to suppress changes:
- name: discover iscsi sessions
command: /sbin/iscsiadm -m session
register: sessions
failed_when:
- sessions.rc != 0
- not sessions.stderr is
search('No active sessions')
changed_when: false
Now, no matter what comes in the return data, Ansible will treat the task as ok rather than changed:
Thus, there are only two possible states to this task now—failed and ok. We have actually negated the possibility of a changed task result.