Ansible 循环和条件

Ansible loops and conditionals

Ansible 文档指出:

Combining when with with_items (see Loops), be aware that the when statement is processed separately for each item.

然而,当我尝试跳过任务中的一项时,它不会那样工作:

value_var: [1, 5]

- name: register variable
  command: echo "4"
  register: var

- name: conditional check
  command: nevermind
  when: var.stdout > item

据我了解,我会在 conditional check 任务的第一项上获得 changed,在第二项上获得 skipped。但我得到:

changed: [guest] => (item=5)
changed: [guest] => (item=1)

我做错了什么?

与循环无关。您正在将一个字符串(echo 命令的结果)与一个整数进行比较。

您应该首先转换值:

when: var.stdout|int > item