Ansible 如何根据条件中断with_items循环
我想根据一个条件打破with_items循环。为参数起见,条件是命令的stdout是否等于特定字符串 显然,下面的例子不起作用,但这是我想要做的一个想法 例如:Ansible 如何根据条件中断with_items循环,ansible,jinja2,ansible-2.x,ansible-inventory,ansible-template,Ansible,Jinja2,Ansible 2.x,Ansible Inventory,Ansible Template,我想根据一个条件打破with_items循环。为参数起见,条件是命令的stdout是否等于特定字符串 显然,下面的例子不起作用,但这是我想要做的一个想法 例如: - name: testing loop shell: "echo {{ item }}" with_items: - "one" - "two" - "three" register: shell_command # registering the shell command and it's
- 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.
如果要中止整个剧本,请尝试以下操作:
- name: testing loop
shell: "echo {{ item }}"
with_items:
- "one"
- "two"
- "three"
register: shell_command
failed_when: "'two' in shell_command.stdout"
或者您可以只添加
忽略\u错误:是
,正如您所看到的,目前这似乎不可能。存在一个未经测试的问题。我不想放弃整个剧本。我只想打破循环,这样我就可以继续玩剩下的游戏了欢迎使用堆栈溢出!将来,您可能希望指定示例“不起作用”的方式。你的问题很好,但我们收到很多问题,其中对问题的整体描述是“它不起作用”,这往往会使评论者对这些词产生偏见。@Haem明白了!谢谢