How to exclude hosts in Ansible?

QuestionsQuestions8 SkillsProAnsible Ad-Hoc CommandsOct, 15 2025
0615

In Ansible, you can exclude hosts from your target set using the ! operator in your host patterns. Here’s how to do it:

  1. 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 ping

    This command targets all hosts except those in the dbservers group.

  2. 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 ping

    This targets all hosts except host1 and host2.

  3. Combining Exclusions with Other Patterns: You can combine exclusions with other patterns. For example:

    ansible webservers:!web2 -i /path/to/inventory -m ping

    This targets all hosts in the webservers group except web2.

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.

0 Comments

no data
Be the first to share your comment!