Ansible tasks should be idempotent. Eg. if the task does not modify anything, it should return ok
instead of changed
. Most builtin modules and tasks are already, but for tasks such as command
and shell
you need to help ansible a bit.
For a task that does purely checking and does not modify anything, you should add:
changed_when: False always_run: yes
The latter allows the task to run even in check mode.
For the sake of completeness, such tasks are usually combined with another that does the actual modification, eg:
- command: check command that returns true when no change needed register: result changed_when: False always_run: yes - command: modify command when: result.rc != 0