I am running a custom command because I haven't found a working module doing what I need, and I want to adjust the changed
flag to reflect the actual behaviour:
- name: Remove unused images shell: '[ -n "$(docker images -q -f dangling=true)" ] && docker rmi $(docker images -q -f dangling=true) || echo Ignoring failure...' register: command_result changed_when: "command_result.stdout == 'Ignoring failure...'" - debug: var="1 {{ command_result.stdout }}" when: "command_result.stdout != 'Ignoring failure...'" - debug: var="2 {{ command_result.stdout }}" when: "command_result.stdout == 'Ignoring failure...'"
(I know the shell command is ugly and could be improved by a more complex script but I don't want to for now)
Running this task on an host where no Docker image can be removed gives the following output:
TASK: [utils.dockercleaner | Remove unused images] **************************** changed: [cloud-host] => {"changed": true, "cmd": "[ -n \"$(docker images -q -f dangling=true)\" ] && docker rmi $(docker images -q -f dangling=true) || echo Ignoring failure...", "delta": "0:00:00.064451", "end": "2015-07-30 18:37:25.620135", "rc": 0, "start": "2015-07-30 18:37:25.555684", "stderr": "", "stdout": "Ignoring failure...", "stdout_lines": ["Ignoring failure..."], "warnings": []} TASK: [utils.dockercleaner | debug var="DIFFERENT {{ command_result.stdout }}"] *** skipping: [cloud-host] TASK: [utils.dockercleaner | debug var="EQUAL {{ command_result.stdout }}"] *** ok: [cloud-host] => { "var": { "EQUAL Ignoring failure...": "EQUAL Ignoring failure..." } }
So, I have this stdout return value "stdout": "Ignoring failure..."
, and the debug task shows the strings are equal, so why is the task still displayed as "changed"?
I am using ansible 1.9.1
.
The documentation I am refering to is this one: http://docs.ansible.com/ansible/playbooks_error_handling.html#overriding-the-changed-result
Ansible changed_when property or parameters is defined to deal with the output of the specific task once a task is triggered on the remote node and based on the return code or the output, we can determine whether the task should be reported in the ansible statistic or need to use the trigger to handle the condition and ...
Ignoring failed commands By default Ansible stops executing tasks on a host when a task fails on that host. You can use ignore_errors to continue on in spite of the failure. The ignore_errors directive only works when the task is able to run and returns a value of 'failed'.
Ansible normally has defaults that make sure to check the return codes of commands and modules and it fails fast – forcing an error to be dealt with unless you decide otherwise. Sometimes a command that returns different than 0 isn't an error.
stdout. Some modules execute command line utilities or are geared for executing commands directly (raw, shell, command, and so on). This field contains the normal output of these utilities.
I think you may have misinterpreted what changed_when
does.
changed_when
marks the task as changed based on the evaluation of the conditional statement which in your case is:
"command_result.stdout == 'Ignoring failure...'"
So whenever this condition is true, the task will be marked as changed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With