仅当主机不属于一个母马时才运行任务

时间:2019-06-11 12:47:41

标签: ansible

仅当主机不属于一个以上组时运行任务:

例如:

[web_1]

[one_web_2]

[server_3]

我希望在前两组上努力

“'web'不在group_names中”->>第一和第二

1 个答案:

答案 0 :(得分:0)

Special Variable个<组名>

  

group_names当前主机所属的组的列表

以下播放显示了主持人所属的选定组( my_groups )中的多少。

- hosts: all
  gather_facts: no
  vars:
    my_groups: [ 'web_1', 'web_2', 'web_3' ]
  tasks:
    - debug:
        msg: "{{ inventory_hostname }} is member of
              {{ my_groups|intersect(group_names)|length }} group(s)."

下面有库存

[test]
test_01
test_02
test_03

[web_1]
test_01
test_02

[web_2]
test_01
test_02

[web_3]
test_03

戏剧给了

ok: [test_01] => {
    "msg": "test_01 is member of 2 group(s)."
}
ok: [test_02] => {
    "msg": "test_02 is member of 2 group(s)."
}
ok: [test_03] => {
    "msg": "test_03 is member of 1 group(s)."
}

使用下面的播放器

  

仅当主机不属于多个组时才运行任务

- hosts: all
  gather_facts: no
  vars:
    my_groups: [ 'web_1', 'web_2', 'web_3' ]
  tasks:
    - fail:
        msg: "{{ inventory_hostname }} is member of
              {{ my_groups|intersect(group_names)|length }} groups.
              Play failed."
      when: my_groups|intersect(group_names)|length != 1
    - debug:
        msg: "{{ inventory_hostname }} is member of
              {{ my_groups|intersect(group_names)|length }} group.
              Play continues."