ansible/docsite/rst/intro_patterns.rst

4.1 KiB

Patterns

Patterns in Ansible are how we decide which hosts to manage. This can mean what hosts to communicate with, but in terms of playbooks it actually means what hosts to apply a particular configuration or IT process to.

We'll go over how to use the command line in intro_adhoc section, however, basically it looks like this:

ansible <pattern_goes_here> -m <module_name> -a <arguments>

Such as:

ansible webservers -m service -a "name=httpd state=restarted"

A pattern usually refers to a set of groups (which are sets of hosts) -- in the above case, machines in the "webservers" group.

Anyway, to use Ansible, you'll first need to know how to tell Ansible which hosts in your inventory to talk to. This is done by designating particular host names or groups of hosts.

The following patterns are equivalent and target all hosts in the inventory:

all
*

It is also possible to address a specific host or set of hosts by name:

one.example.com
one.example.com:two.example.com
192.168.1.50
192.168.1.*

The following patterns address one or more groups. Groups separated by a colon indicate an "OR" configuration. This means the host may be in either one group or the other:

webservers
webservers:dbservers

You can exclude groups as well, for instance, all machines must be in the group webservers but not in the group phoenix:

webservers:!phoenix

You can also specify the intersection of two groups. This would mean the hosts must be in the group webservers and the host must also be in the group staging:

webservers:&staging

You can do combinations:

webservers:dbservers:&staging:!phoenix

The above configuration means "all machines in the groups 'webservers' and 'dbservers' are to be managed if they are in the group 'staging' also, but the machines are not to be managed if they are in the group 'phoenix' ... whew!

You can also use variables if you want to pass some group specifiers via the "-e" argument to ansible-playbook, but this is uncommonly used:

webservers:!{{excluded}}:&{{required}}

You also don't have to manage by strictly defined groups. Individual host names, IPs and groups, can also be referenced using wildcards:

*.example.com
*.com

It's also ok to mix wildcard patterns and groups at the same time:

one*.com:dbservers

Most people don't specify patterns as regular expressions, but you can. Just start the pattern with a '~':

~(web|db).*\.example\.com

Limiting Selected Hosts

What hosts you select to manage can be additionally constrained by using the '--limit' parameter or by using 'batch' (or 'range') selectors.

As mentioned above, patterns can be strung together to select hosts in more than one group:

$ ansible webservers:dbservers -m command -a "/bin/foo xyz"

This is an "or" condition. If you want to further constrain the selection, use --limit, which also works with ansible-playbook:

$ ansible webservers:dbservers -m command -a "/bin/foo xyz" --limit region

Assuming version 0.9 or later, as with other host patterns, values to limit can be separated with ";", ":", or ",".

Now let's talk about range selection. Suppose you have 1000 servers in group 'datacenter', but only want to target one at a time. This is also easy:

$ ansible webservers[0-99] -m command -a "/bin/foo xyz"
$ ansible webservers[100-199] -m command -a "/bin/foo xyz"

This will select the first 100, then the second 100, host entries in the webservers group. (It does not matter what their names or IP addresses are).

Both of these methods can be used at the same time, and ranges can also be passed to the --limit parameter.

Easy enough. See intro_adhoc and then playbooks for how to apply this knowledge.

intro_adhoc

Examples of basic commands

playbooks

Learning ansible's configuration management language

Mailing List

Questions? Help? Ideas? Stop by the list on Google Groups

irc.freenode.net

#ansible IRC chat channel