我有超过 200 台服务器,我必须在其中复制 Repo 文件,而我不想在 AppCluster 和 DBCluster 服务器等服务器上复制该文件。因为集群有不同的Repo文件,标准有不同的Repo文件
[AppCluster]
172.16.55.5
172.16.55.6
172.16.55.7
172.16.55.8
[DBCluster]
172.16.56.5
172.16.56.6
172.16.55.7
172.16.55.8
我试过
- name: Copy YUM File copy: src: /home/sysadmin/ansible_files/modules_utils/templates/repofile/rhel-remote.repo dest: /etc/yum.repos.d/ owner: root group: root mode: '0644' when: ansible_distribution_major_version == "7" and (group_names != "AppCluster" or group_names != "DBCluster")
但似乎不起作用
任何建议
答案1
问:“不要在 AppCluster 和 DBCluster 服务器上复制该文件。”
答:试试这个条件
when:
- ansible_distribution_major_version == '7'
- group_names|intersect(['AppCluster', 'DBCluster'])|length == 0
例子
鉴于库存
shell> cat hosts
srv1
srv2
srv3
[AppCluster]
srv2
[DBCluster]
srv3
剧本
shell> cat pb.yml
- hosts: all
gather_facts: false
tasks:
- debug:
msg: "Copy repo to {{ inventory_hostname }}"
when: group_names|intersect(['AppCluster', 'DBCluster'])|length == 0
给出
shell> ansible-playbook -i hosts pb.yml
PLAY [all] ********************************************************************************
TASK [debug] ******************************************************************************
skipping: [srv3]
ok: [srv1] =>
msg: Copy repo to srv1
skipping: [srv2]
PLAY RECAP ********************************************************************************
srv1: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
srv2: ok=0 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
srv3: ok=0 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
答案2
我认为你可以排除带有主机的组。
主持人:
all
!AppCluster
!DBCluster