Ansible in 1.4 and later provides a way to specify this behavior as follows:
Run Code Online (Sandbox Code Playgroud)- name: Fail task when the command error output prints FAILED command: /usr/bin/example-command -x -y -z register: command_result failed_when: "'FAILED' in command_result.stderr"
but the common Return Values already include .failed
and .changed
.
It seems to me these features contradict each other. Is there an official resolution to it? Does Ansible define one of failed_when
or changed_when
as being evaluated first and affecting the latter? Does it define that both are evaluated without seeing the effect of the other?
E.g. what is the defined behaviour of
command_result.failed
within a failed_when
expressioncommand_result.changed
within a changed_when
expressioncommand_result.failed
within a changed_when
expressioncommand_result.changed
within a changed_when
expression更清楚地说,例如对于第三个,我对以下结果的定义(可靠)程度感兴趣:
- package:
name: systemd
state: present
check_mode: yes
register: command_result
failed_when: command_result.changed
Run Code Online (Sandbox Code Playgroud)
假设我对 Ansible 2.0 及更高版本感兴趣。
您的示例为您的剧本添加了另一条路径。如果未安装 systemd,则输出将与安装时不同。第一次运行后它将被安装。这违背了 ansible 原则:
幂等性
如果执行一次操作的结果与重复执行操作而无需任何干预操作的结果完全相同,则该操作是幂等的。
我建议您运行命令which systemctl
并注册输出。检查输出以安装 systemd 并失败并显示失败任务。
我想没有真正的文件,我们必须调查。
我希望我抓住了所有案例:)但我现在无法填写图表。
剧本.yml:
---
- name: test some stuff
hosts: all
tasks:
- include_tasks: tasks.yml
with_items:
- { data: ping, changed: true }
- { data: ping, changed: false }
- { data: crash, changed: true }
- { data: crash, changed: false }
Run Code Online (Sandbox Code Playgroud)
任务.yml
---
- name: Check for command_result is defined and command_result
ping:
data: "{{ item.data }}"
register: command_result
changed_when: item.changed
failed_when: command_result is defined and command_result
ignore_errors: true
- name: Check for command_result is defined and command_result
file:
path: ./file
register: command_result
changed_when: item.changed
failed_when: command_result is defined and command_result
ignore_errors: true
- name: Check for command_result
ping:
data: "{{ item.data }}"
register: command_result
changed_when: item.changed
failed_when: command_result
ignore_errors: true
- name: Check for command_result
file:
path: ./file
register: command_result
changed_when: item.changed
failed_when: command_result
ignore_errors: true
- name: Check for command_result.changed is defined and command_result.changed
ping:
data: "{{ item.data }}"
register: command_result
changed_when: item.changed
failed_when: command_result.changed is defined and command_result.changed
- name: Check for command_result.changed is defined and command_result.changed
ping:
data: "{{ item.data }}"
register: command_result
changed_when: item.changed
failed_when: command_result.changed is defined and command_result.changed
ignore_errors: true
- name: Check for command_result.changed
ping:
data: "{{ item.data }}"
register: command_result
changed_when: item.changed
failed_when: command_result.changed
ignore_errors: true
- name: Check for command_result.changed
file:
path: ./file
register: command_result
changed_when: item.changed
failed_when: command_result.changed
ignore_errors: true
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
21096 次 |
最近记录: |