How do I report an Ansible command/shell task as changed in check mode?
Solution 1:
I know this is kind of old, but I'm looking for the solution to this problem. I'm also tired of surprises with command module as OP.
What if we change the 'command' part so that it executes a different command in check mode. And we run the task always, even in check mode.
Note: Before you complain, I know this is a bad example. I know there is a 'creates' option in 'command' module. This is just to give an example.
This is my solution so far:
- name: Get some info
stat: {path: /tmp/somefile}
register: file_info
- name: Run the command conditionally
command:
"{{ 'true' if (file_info['stat']['exists'] or ansible_check_mode)
else 'touch /tmp/somefile' }}"
changed_when: not file_info['stat']['exists']
check_mode: false
It works, but is bloated.
Please tell me if there are better solutions.