Can I combine different host patterns?

QuestionsQuestions8 SkillsProAnsible Ad-Hoc CommandsOct, 15 2025
084

Yes, you can combine different host patterns in Ansible to target specific subsets of hosts. Here are some ways to do this:

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

    This targets all hosts in the webservers group and the specific host host1.

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

    This targets all hosts that start with "web".

  3. Excluding Hosts: You can exclude certain groups or hosts using the ! operator. For example:

    ansible all:!dbservers -i /path/to/inventory -m ping

    This targets all hosts except those in the dbservers group.

  4. Complex Patterns: You can create more complex patterns by combining the above methods. For example:

    ansible 'webservers,db1:!db2' -i /path/to/inventory -m ping

    This targets all hosts in the webservers group and db1, but excludes db2.

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.

0 Comments

no data
Be the first to share your comment!