Ansible result.failed 与 failed_when、result.changed 与changed_when 等

Ansible result.failed 与 failed_when、result.changed 与changed_when 等

Ansible 在 1.4 及更高版本中提供了一种指定此行为的方法,如下所示:

- 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"

但常见的返回值已经包括.failed.changed

在我看来,这些特征是相互矛盾的。有官方解决方案吗? Ansible 是否将其中之一定义failed_whenchanged_when首先评估并影响后者?它是否定义了对两者进行评估而没有看到对方的效果?

例如,定义的行为是什么

  1. command_result.failedfailed_when在表达式内
  2. command_result.changedchanged_when在表达式内
  3. command_result.failedchanged_when在表达式内
  4. command_result.changedchanged_when在表达式内

更清楚地说,例如对于第三个,我感兴趣的是以下结果的定义如何明确(可靠):

- package:
    name: systemd
    state: present
  check_mode: yes
  register: command_result
  failed_when: command_result.changed

假设我对 Ansible 2.0 及更高版本感兴趣。

答案1

这看起来不对

您的示例为您的剧本添加了另一条路径。如果未安装 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 }

任务.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

相关内容