ansible - 如何在 ansible 中迭代子组?

ansible - how to iterate children groups in ansible?

我有如下的库存文件

[父组]
儿童组 1
儿童组 2

[儿童组 1]
主机 1
主机 2
host3

[儿童组 2]
主机4
主机5
host6

现在我想迭代 Children wise..

即在 host1、host2、host3 上并行执行我的任务,即仅在 ChildrenGroup1 中存在的主机上,一旦成功,我需要与 ChildrenGroup2 一起去,即在 host4、host5、host6

注意事项?

  1. 如果任何一个子组主机出现任何故障,那么我们需要 wait/pause 在继续下一个子组之前
  2. 我的库存中会有很多子组
  3. 我一次只需要对一个儿童组执行我的任务。
  4. 我也会确保所有的儿童组都一次性解决。

你能就如何推进这项工作提出建议吗?

这里的关键限制是一个剧本不能启动另一个剧本。唯一的选择是 import_playbook。剧本启动时,导入的文件必须可用。结果,解决方案是two-step过程。在第一步中创建剧本,然后 运行 它们。例如,给定库存

shell> cat hosts
[ParentGroup:children]
ChildrenGroup1
ChildrenGroup2

[ChildrenGroup1]
host1
host2
host3

[ChildrenGroup2]
host4
host5
host6

您想 运行 剧本 pb.yml 如问题中所述。获取剧本并通过将 {{ item }} 放入 hosts:

来创建模板
shell> cat pb.yml.j2
- hosts: "{{ item }}"
  gather_facts: false
  tasks:
    - debug:
        msg: "{{ inventory_hostname }}: Playbook started."

1.创建剧本

下面的剧本在第一个任务中创建组 my_groups 的列表。然后模板任务迭代此列表并为组创建剧本。下一个模板任务将这些剧本导入剧本pb-groups.yml

shell> cat pb-init.yml
- hosts: localhost
  vars:
    groups_other: [ParentGroup, all, ungrouped]
  tasks:
    - set_fact:
        my_groups: "{{ groups.keys()|difference(groups_other) }}"
    - template:
        src: pb.yml.j2
        dest: "pb-{{ item }}.yml"
      loop: "{{ my_groups }}"
    - template:
        src: pb-groups.yml.j2
        dest: pb-groups.yml
shell> cat pb-groups.yml.j2
- hosts: localhost
  gather_facts: false

{% for group in my_groups %}
- import_playbook: pb-{{ group }}.yml
{% endfor %}

查看创建的文件

shell> cat pb-ChildrenGroup1.yml 
- hosts: "ChildrenGroup1"
  gather_facts: false
  tasks:
    - debug:
        msg: "localhost: Playbook started."
shell> cat pb-ChildrenGroup2.yml 
- hosts: "ChildrenGroup2"
  gather_facts: false
  tasks:
    - debug:
        msg: "localhost: Playbook started."
shell> cat pb-groups.yml
- hosts: localhost
  gather_facts: false

- import_playbook: pb-ChildrenGroup1.yml
- import_playbook: pb-ChildrenGroup2.yml

2。 运行 创建了剧本

shell> ansible-playbook pb-groups.yml

PLAY [localhost] ****

PLAY [ChildrenGroup1] ****

TASK [debug] ****
ok: [host1] => 
  msg: 'localhost: Playbook started.'
ok: [host2] => 
  msg: 'localhost: Playbook started.'
ok: [host3] => 
  msg: 'localhost: Playbook started.'

PLAY [ChildrenGroup2] ****

TASK [debug] ****
ok: [host4] => 
  msg: 'localhost: Playbook started.'
ok: [host5] => 
  msg: 'localhost: Playbook started.'
ok: [host6] => 
  msg: 'localhost: Playbook started.'

PLAY RECAP ****

  ...


我的库存中有许多子组

更改库存。例如

shell> cat hosts
[ParentGroup:children]
ChildrenGroup1
ChildrenGroup2
ChildrenGroup3

[ChildrenGroup1]
host1
host2

[ChildrenGroup2]
host4
host5

[ChildrenGroup3]
host3
host6

下面的命令按预期工作

shell> ansible-playbook pb-init.yml
  ...
shell> ansible-playbook pb-groups.yml

PLAY [localhost] ****

PLAY [ChildrenGroup1] ****

TASK [debug] ****
ok: [host1] => 
  msg: 'localhost: Playbook started.'
ok: [host2] => 
  msg: 'localhost: Playbook started.'

PLAY [ChildrenGroup2] ****

TASK [debug] ****
ok: [host4] => 
  msg: 'localhost: Playbook started.'
ok: [host5] => 
  msg: 'localhost: Playbook started.'

PLAY [ChildrenGroup3] ****

TASK [debug] ****
ok: [host3] => 
  msg: 'localhost: Playbook started.'
ok: [host6] => 
  msg: 'localhost: Playbook started.'

PLAY RECAP ****

  ...