如何根据条件打破 with_items 循环

问题描述:

我想根据条件跳出 with_items 循环.参数的条件是命令的标准输出是否等于特定字符串.

I want to break out of the with_items loop based on a condition. That condition for arguments sake is if the stdout of a command is equal to a particular string.

显然下面的例子不起作用,但这是我想要做的一个想法.

Obviously the example below does not work but this is an idea of what I want to do.

例如:

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

目前这似乎不可能,因为您可以看到 此处.存在未经测试的hack.

This seems not possible at the moment as you can see here. There exists an untested hack out there.