在 Ansible 清单中,我可以从主机组中排除子组吗?

在 Ansible 清单中,我可以从主机组中排除子组吗?

假设您已使用 ini 文件格式设置了 Ansible 库存,如以下有点做作的示例:

[idrac]
host1-idrac
host2-idrac
host3-idrac
; ...and 97 more that I won't write here

[idrac_use_py3]
host1-idrac
host3-idrac

[idrac_use_py2:children]
; syntax to include group idrac
; and exclude hosts that are in idrac_use_py3?

我尝试了各种库存变化并通过 进行了测试ansible-inventory --list。例如,我尝试了idrac:!idrac_use_py3。这会出现语法错误:

[WARNING]:  * Failed to parse /path/to/my/inventory with ini plugin: /path/to/my/inventory:30: Expected group name, got: idrac:!idrac_use_py3

我尝试过idrac然后按!idrac_use_py3,结果出现了不同的语法错误:

[WARNING]:  * Failed to parse /path/to/my/inventory with ini plugin: /path/to/my/inventory:32: Section [idrac_use_py2:children] includes undefined group: !idrac_use_py3

我尝试将该idrac_use_py2:children组移动到最后按词汇加载的单独文件中,但这并不能解决错误undefined group

在库存定义中,是否可以将一个组的成员排除在另一个组之外?

答案1

问:在 Ansible 清单中,我可以从主机组中排除子组吗?

答:不。模式不能在库存文件中使用。引用自使用模式

“模式是临时命令中唯一没有标志的元素。它通常是第二个元素:”

shell> ansible <pattern> -m <module_name> -a "<module options>"

“在剧本中,模式是每场比赛主持人的内容:行:”

- name: <play_name>
  hosts: <pattern>

例如,库存

shell> cat hosts
[idrac]
host1-idrac
host2-idrac
host3-idrac
; ...and 97 more that I won't write here

[idrac_use_py3]
host1-idrac
host3-idrac

[idrac_use_py2:children]
; syntax to include group idrac
; and exclude hosts that are in idrac_use_py3?

和剧本

shell> cat pb.yml
- hosts: idrac:!idrac_use_py3
  gather_facts: false
  tasks:
    - debug:
        var: ansible_play_hosts_all
      run_once: true

按预期使用所需模式

shell> ansible-playbook -i hosts pb.yml

PLAY [idrac:!idrac_use_py3] *********************************************

TASK [debug] ************************************************************
ok: [host2-idrac] => 
  ansible_play_hosts_all:
  - host2-idrac

PLAY RECAP **********************************************************************
host2-idrac: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

下一个选项是在第一个游戏中动态创建组,并在第二个游戏中使用它,例如

shell> cat pb.yml
- hosts: all
  gather_facts: false
  tasks:
    - add_host:
        name: "{{ item }}"
        groups: idrac_use_py2
      loop: "{{ groups.idrac|difference(groups.idrac_use_py3) }}"
      run_once: true

- hosts: idrac_use_py2
  gather_facts: false
  tasks:
    - debug:
        var: ansible_play_hosts_all
      run_once: true

给出

shell> ansible-playbook -i hosts pb.yml

PLAY [all] **************************************************************

TASK [add_host] *********************************************************
changed: [host1-idrac] => (item=host2-idrac)

PLAY [idrac_use_py2] ****************************************************

TASK [debug] ************************************************************
ok: [host2-idrac] => 
  ansible_play_hosts_all:
  - host2-idrac

PLAY RECAP **************************************************************
host1-idrac: ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
host2-idrac: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

相关内容