Patterns: targeting hosts and groups
When you execute Ansible through an ad hoc command or by running a playbook, you must choose which managed nodes or groups you want to execute against. Patterns let you run commands and playbooks against specific hosts and/or groups in your inventory. An Ansible pattern can refer to a single host, an IP address, an inventory group, a set of groups, or all hosts in your inventory. Patterns are highly flexible — you can exclude or require subsets of hosts, use wildcards or regular expressions, and more. Ansible executes on all inventory hosts included in the pattern.
Using patterns
You use a pattern almost any time you execute an ad hoc command or a playbook. The pattern is the only element of an ad hoc command that has no flag. It is usually the second element:
In a playbook the pattern is the content of the hosts: line for each play:
Since you often want to run a command or playbook against multiple hosts at once, patterns often refer to inventory groups. Both the ad hoc command and the playbook above will execute against all machines in the webservers group.
Common patterns
This table lists common patterns for targeting inventory hosts and groups.
host1:host2 (or host1,host2)
all hosts in webservers plus all hosts in dbservers
all hosts in webservers except those in atlanta
Intersection of groups
any hosts in webservers that are also in staging
You can use either a comma ( , ) or a colon ( : ) to separate a list of hosts. The comma is preferred when dealing with ranges and IPv6 addresses.
Once you know the basic patterns, you can combine them. This example:
targets all machines in the groups ‘webservers’ and ‘dbservers’ that are also in the group ‘staging’, except any machines in the group ‘phoenix’.
You can use wildcard patterns with FQDNs or IP addresses, as long as the hosts are named in your inventory by FQDN or IP address:
You can mix wildcard patterns and groups at the same time:
Limitations of patterns
Patterns depend on inventory. If a host or group is not listed in your inventory, you cannot use a pattern to target it. If your pattern includes an IP address or hostname that does not appear in your inventory, you will see an error like this:
Your pattern must match your inventory syntax. If you define a host as an alias :
you must use the alias in your pattern. In the example above, you must use host1 in your pattern. If you use the IP address, you will once again get the error:
Pattern processing order
The processing is a bit special and happens in the following order:
This positioning only accounts for processing order inside each operation: a:b:&c:!d:!e == &c:a:!d:b:!e == !d:a:!e:&c:b
All of these result in the following:
Host in/is (a or b) AND host in/is all(c) AND host NOT in/is all(d, e).
Now a:b:!e:!d:&c is a slight change as the !e gets processed before the !d , though this doesn’t make much of a difference:
Host in/is (a or b) AND host in/is all(c) AND host NOT in/is all(e, d).
Advanced pattern options
The common patterns described above will meet most of your needs, but Ansible offers several other ways to define the hosts and groups you want to target.
Using variables in patterns
You can use variables to enable passing group specifiers via the -e argument to ansible-playbook:
Using group position in patterns
You can define a host or subset of hosts by its position in a group. For example, given the following group:
you can use subscripts to select individual hosts or ranges within the webservers group:
Using regexes in patterns
You can specify a pattern as a regular expression by starting the pattern with
Patterns and ad-hoc commands
You can change the behavior of the patterns defined in ad-hoc commands using command-line options. You can also limit the hosts you target on a particular run with the —limit flag.
Limit to one host
Limit to multiple hosts
Negated limit. Note that single quotes MUST be used to prevent bash interpolation.
Limit to host group
Patterns and ansible-playbook flags
You can change the behavior of the patterns defined in playbooks using command-line options. For example, you can run a playbook that defines hosts: all on a single host by specifying -i 127.0.0.2, (note the trailing comma). This works even if the host you target is not defined in your inventory, but this method will NOT read your inventory for variables tied to this host and any variables required by the playbook will need to be specified manually at the command line. You can also limit the hosts you target on a particular run with the —limit flag, which will reference your inventory:
Finally, you can use —limit to read the list of hosts from a file by prefixing the file name with @ :
If RETRY_FILES_ENABLED is set to True , a .retry file will be created after the ansible-playbook run containing a list of failed hosts from all plays. This file is overwritten each time ansible-playbook finishes running.
To apply your knowledge of patterns with Ansible commands and playbooks, read Introduction to ad hoc commands and Ansible playbooks .
Examples of basic commands
Learning the Ansible configuration management language
Questions? Help? Ideas? Stop by the list on Google Groups
How to join Ansible chat channels
© Copyright Ansible project contributors. Last updated on Dec 14, 2022.