简体   繁体   English

Ansible 在使用 include 时会中断循环

[英]Ansible breaks the loop when using include

I am trying to run multiple tasks in a loop, but whenever I get an error inside the loop, ansible exits out of it.我试图在一个循环中运行多个任务,但是每当我在循环内遇到错误时,ansible 就会退出它。 I want to be able to run the loop until the end of the list for each item.我希望能够运行循环,直到每个项目的列表结束。 If something breaks for an item, the loop stops only for this item, not for the rest.如果某个项目出现问题,则循环仅针对该项目停止,而不是 rest。

My inventory structure is json, but looks something like this:我的库存结构是 json,但看起来像这样:

webhost1:
  items: 
      [item1, item2]

My ansible playbook does this:我的 ansible 剧本这样做:

  - include: multiple_tasks_to_be_run_for_each_item.yml
    with_items: items

The expected result is similar to running:预期结果类似于运行:

  - shell: /bin/false
    with_items:
       - "1"
       - "2"
       - "3"

Even if it fails on the first item, it continues to run.即使它在第一个项目上失败,它也会继续运行。

Please try with loop, loop_control and loop_var within Ansible Role, and let me know if it helps.请尝试在 Ansible 角色中使用 loop、loop_control 和 loop_var,如果有帮助,请告诉我。

sample.yml
-----------
    - include_tasks: "repeat.yml"
      loop: "{{ list_of_values }}"
      loop_control:
        loop_var: var1


repeat.yml ( You can use the var1 inside the repeat.yml)
-----------

---
- name: Repeat Tasks
  block:
    - name : task 1
      .........

    - name : task 2
      .........

Apparently this is the expected behaviour.显然这是预期的行为。 Answer from github issue here.来自github 问题的回答在这里。

This is working as intended.这是按预期工作的。 Use block and rescue to control failure.使用blockrescue来控制故障。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM