主机上的 Ansible 顺序 with_items

Ansible sequential with_items over hosts

我有一种情况想要遍历列表 (with_items) 并执行命令。但是,除非第一个项目在所有主机上完成,否则下一个项目无法开始。我将遍历的项目数量将是巨大的。但是,主机中不应有任何关于正在使用的项目的漂移。除非项目 1 在整个主机池中完成,否则主机不应启动项目 2。

为了创建概念验证,我希望实现以下目标

- name: proof of concept
  command: bash -c /home/user/my_test_script.sh
  with_items: 
    - item_1
    - item_2
    ....
    - item_n

其中 my_test_script.sh 在每台主机上有不同的 sleep(x),模拟某些主机执行作业可能较慢。

我已经看过 wait_for、wait_until 等。但还没有找到执行并行任务 (with_items) 的解决方案,该任务运行(对于列表中的每个项目)'in sync'(时间上不是 rsync)对于许多给定的项目。

如有任何帮助,我们将不胜感激。

相关:.

如果您使用的是 Ansible 2.3.0 或更高版本,请查看 include_role。您可以编写一个角色,该角色将使用可变参数调用您的脚本。然后你的剧本可以循环这个角色。

角色:

- name: proof of concept
  command: bash -c /home/user/my_test_script.sh {{ script_param }}

剧本:

  - include_role:
      name: test_script
    with_items:
    - item1
    - item2
    - item3
    vars:
      script_param: "{{ item }}"