In Ansible, you can exclude hosts from your target set using the ! operator in your host patterns. Here’s how to do it:
-
Excluding a Group: To exclude an entire group from your command, use the
!operator followed by the group name. For example:ansible all:!dbservers -i /path/to/inventory -m pingThis command targets all hosts except those in the
dbserversgroup. -
Excluding Specific Hosts: You can also exclude specific hosts by listing them with the
!operator. For example:ansible all:!host1,host2 -i /path/to/inventory -m pingThis targets all hosts except
host1andhost2. -
Combining Exclusions with Other Patterns: You can combine exclusions with other patterns. For example:
ansible webservers:!web2 -i /path/to/inventory -m pingThis targets all hosts in the
webserversgroup exceptweb2.
Example
Given an inventory like this:
[webservers]
web1
web2
[dbservers]
db1
db2
If you want to ping all hosts except those in the dbservers group, you would run:
ansible all:!dbservers -i /path/to/inventory -m ping
For more information, you can refer to the Ansible documentation on patterns.
