【问题标题】:How can I break the with_items loop based on a condition如何根据条件打破 with_items 循环
【发布时间】:2018-11-06 12:16:42
【问题描述】:

我想根据条件跳出 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.

【问题讨论】:

  • 欢迎来到 Stack Overflow!将来,您可能希望指定您的示例如何“不起作用”。您的问题很好,但我们收到很多问题的整个描述都是“它不起作用”,这往往会使审稿人对这些词产生偏见。
  • @Haem 明白了!谢谢

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


【解决方案1】:

目前这似乎不可能,您可以看到 here。存在一个未经测试的hack

【讨论】:

    【解决方案2】:

    如果你想中止整个剧本,试试这个:

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

    或者你可以添加ignore_errors: yes

    【讨论】:

    • 我不想中止整个剧本。我只想打破循环,这样我就可以continue 继续播放其余部分
    猜你喜欢
    • 1970-01-01
    • 2023-01-09
    • 2017-06-04
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 2014-09-03
    • 2020-04-21
    • 1970-01-01
    相关资源
    最近更新 更多