Yes, you can combine different host patterns in Ansible to target specific subsets of hosts. Here are some ways to do this:
-
Combining Groups and Hosts: You can specify multiple groups and individual hosts separated by commas. For example:
ansible webservers,host1 -i /path/to/inventory -m pingThis targets all hosts in the
webserversgroup and the specific hosthost1. -
Using Wildcards with Groups: You can use wildcards to match multiple hosts along with specific groups. For example:
ansible 'web*' -i /path/to/inventory -m pingThis targets all hosts that start with "web".
-
Excluding Hosts: You can exclude certain groups or hosts using the
!operator. For example:ansible all:!dbservers -i /path/to/inventory -m pingThis targets all hosts except those in the
dbserversgroup. -
Complex Patterns: You can create more complex patterns by combining the above methods. For example:
ansible 'webservers,db1:!db2' -i /path/to/inventory -m pingThis targets all hosts in the
webserversgroup anddb1, but excludesdb2.
Example
If your inventory file has:
[webservers]
web1
web2
[dbservers]
db1
db2
You can run:
ansible 'webservers,db1:!db2' -i /path/to/inventory -m ping
This will ping web1, web2, and db1, but not db2.
For more details, refer to the Ansible documentation on patterns.
