我正在运行自定义命令,因为我找不到正在运行的模块,我想调整changed
标志以反映实际行为:
- 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...'"
(我知道shell命令很难看,可以通过更复杂的脚本来改进,但我现在不想这样做)
在无法删除Docker镜像的主机上运行此任务会产生以下输出:
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..."
}
}
所以,我有这个stdout返回值"stdout": "Ignoring failure..."
,调试任务显示字符串相等,所以为什么任务仍然显示为"更改" ?
我正在使用ansible 1.9.1
。
我所指的文档就是这个:http://docs.ansible.com/ansible/playbooks_error_handling.html#overriding-the-changed-result
答案 0 :(得分:31)
我认为你可能误解了changed_when
的作用。
changed_when
根据对您的情况的条件语句的评估将任务标记为已更改:
“command_result.stdout =='忽略失败......'”
因此,只要此条件为真,任务就会被标记为已更改。