对于Ansible,可以使用通配符来执行组中的一部分主机。有两种通配符模式可用:
- 使用asterisk( *)匹配组内所有主机:
- name: Restart haproxy on all nodes in the web group
hosts: web*
become: true
tasks:
- name: Restart HAProxy
service:
name: haproxy
state: restarted
- 使用问号(?)匹配组中单个字符:
- name: Restart HAProxy on host matching webserver[12]
hosts: webserver[12]
become: true
tasks:
- name: Restart HAProxy
service:
name: haproxy
state: restarted
除此之外,还可以使用替代方案。例如Ansible的'动态清单”功能使您可以使用Python脚本编写清单或执行可迭代和生成广泛动态群集。
- name: Use dynamic inventory script
hosts: tag_server
tasks:
- name: Install Apache
yum: name=httpd state=present
这将使用Python脚本作为动态清单来执行任务。