如何根据条件中断with_items循环

时间:2018-11-06 12:16:42

标签: ansible jinja2 ansible-2.x ansible-inventory ansible-template

我想根据条件摆脱with_items循环。出于参数考虑,该条件是命令的标准输出等于特定字符串。

显然下面的示例不起作用,但这是我想做的事情。

例如:

- name: testing loop
  shell: "echo {{ item }}"
  with_items:
     - "one"
     - "two"
     - "three"
  register: shell_command # registering the shell command and it's attributes
  when: shell_command.stdout == "two" # break once the stdout of the run shell command matches the string "two". So it will run twice and break on the second.

2 个答案:

答案 0 :(得分:1)

目前看来这是不可能的,因为您可以看到here。那里有未经测试的hack

答案 1 :(得分:0)

如果您想中止整个剧本,请尝试以下操作:

 - name: testing loop
            shell: "echo {{ item }}"
            with_items:
              - "one"
              - "two"
              - "three"
            register: shell_command
            failed_when: "'two' in shell_command.stdout"

或者您也可以添加ignore_errors: yes