hosts:从指定组中 排除 或 选择 某个 host 主机
一、介绍
对于 剧本中 hosts 字段进行的操作:例如,对 router-02 组进行操作,但需要排除该组中的一台主机:storage02-mariadb
二、hosts 主机清单:
---all: hosts: router2: ansible_host: 192.168.10.254 public_ip: 200.1.1.253 hostname: router LB01-Nginx: ansible_host: 192.168.10.100 hostname: LB01-Nginx gateway: 192.168.10.254 LB02-Nginx: ansible_host: 192.168.10.101 hostname: LB02-Nginx gateway: 192.168.10.254 RS01-Apache: ansible_host: 192.168.10.102 hostname: RS01-Apache gateway: 192.168.10.254 RS02-php: ansible_host: 192.168.10.103 hostname: RS02-php gateway: 192.168.10.254 RS03-php: ansible_host: 192.168.10.104 hostname: RS03-php gateway: 192.168.10.254 storage01-nfs: ansible_host: 192.168.10.105 hostname: storage01-nfs gateway: 192.168.10.254 storage02-mariadb: ansible_host: 192.168.10.106 hostname: storage02-mariadb gateway: 192.168.10.254 children: router-02: hosts: router2: LB01-Nginx: LB02-Nginx: RS01-Apache: RS02-php: storage01-nfs: storage02-mariadb:
三、剧本示例:
---- name: Config nfs Client hosts: router-02:!storage01-nfs:!storage02-mariadb:!router2 # 排除 storage01-nfs、storage02-mariadb 和 router2 gather_facts: False tasks: - name: Install NFS Client ansible.builtin.apt: update_cache: False pkg: - nfs-common - name: Create Web Root-Directory ansible.builtin.file: path: /var/www/html/www state: directory mode: '0777' - name: Mount NFS Directory ansible.posix.mount: src: 192.168.10.105:/website path: /var/www/html/www opts: rw,sync,hard state: mounted fstype: nfs