2013-10-05 19:58:03 +02:00
|
|
|
Patterns
|
|
|
|
++++++++
|
2012-03-09 17:44:14 +01:00
|
|
|
|
2013-10-05 19:25:05 +02:00
|
|
|
.. contents::
|
|
|
|
:depth: 2
|
|
|
|
|
2013-09-29 20:45:00 +02:00
|
|
|
Patterns in Ansible are how we decide which hosts to manage. This can mean what hosts to communicate with, but in terms
|
|
|
|
of :doc:`playbooks` it actually means what hosts to apply a particular configuration or IT process to.
|
2012-03-09 20:39:29 +01:00
|
|
|
|
2013-09-30 01:30:10 +02:00
|
|
|
We'll go over how to use the command line in :doc:`intro_adhoc` section, however, basically it looks like this::
|
2012-03-31 16:38:24 +02:00
|
|
|
|
|
|
|
ansible <pattern_goes_here> -m <module_name> -a <arguments>
|
2012-12-11 03:48:38 +01:00
|
|
|
|
2012-03-31 16:38:24 +02:00
|
|
|
Such as::
|
|
|
|
|
|
|
|
ansible webservers -m service -a "name=httpd state=restarted"
|
|
|
|
|
2013-10-05 20:57:45 +02:00
|
|
|
A pattern usually refers to a set of groups (which are sets of hosts) -- in the above case, machines in the "webservers" group.
|
|
|
|
|
2013-09-29 20:45:00 +02:00
|
|
|
Anyway, to use Ansible, you'll first need to know how to tell Ansible which hosts in your inventory to talk to.
|
2012-03-31 16:38:24 +02:00
|
|
|
This is done by designating particular host names or groups of hosts.
|
|
|
|
|
2013-10-05 20:57:45 +02:00
|
|
|
The following patterns are equivalent and target all hosts in the inventory::
|
2012-03-08 19:53:48 +01:00
|
|
|
|
|
|
|
all
|
2012-12-11 03:48:38 +01:00
|
|
|
*
|
2012-03-08 19:36:47 +01:00
|
|
|
|
2013-10-05 20:57:45 +02:00
|
|
|
It is also possible to address a specific host or set of hosts by name::
|
2012-03-08 19:36:47 +01:00
|
|
|
|
|
|
|
one.example.com
|
|
|
|
one.example.com:two.example.com
|
2012-03-11 20:34:21 +01:00
|
|
|
192.168.1.50
|
|
|
|
192.168.1.*
|
2012-12-11 03:48:38 +01:00
|
|
|
|
2013-10-06 03:19:19 +02:00
|
|
|
The following patterns address one or more groups. Groups separated by a colon indicate an "OR" configuration.
|
2013-09-29 20:45:00 +02:00
|
|
|
This means the host may be in either one group or the other::
|
2012-03-08 19:36:47 +01:00
|
|
|
|
|
|
|
webservers
|
|
|
|
webservers:dbservers
|
|
|
|
|
2013-09-29 20:45:00 +02:00
|
|
|
You can exclude groups as well, for instance, all machines must be in the group webservers but not in the group phoenix::
|
2012-07-04 23:44:39 +02:00
|
|
|
|
|
|
|
webservers:!phoenix
|
|
|
|
|
2013-09-29 20:45:00 +02:00
|
|
|
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::
|
2013-02-09 16:47:52 +01:00
|
|
|
|
|
|
|
webservers:&staging
|
|
|
|
|
|
|
|
You can do combinations::
|
|
|
|
|
2013-09-29 20:45:00 +02:00
|
|
|
webservers:dbservers:&staging:!phoenix
|
2013-02-09 16:47:52 +01:00
|
|
|
|
2013-09-29 20:45:00 +02:00
|
|
|
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::
|
2013-02-09 16:47:52 +01:00
|
|
|
|
2013-04-27 18:04:22 +02:00
|
|
|
webservers:!{{excluded}}:&{{required}}
|
2013-02-09 16:47:52 +01:00
|
|
|
|
2013-09-29 20:45:00 +02:00
|
|
|
You also don't have to manage by strictly defined groups. Individual host names, IPs and groups, can also be referenced using
|
2012-03-09 17:44:14 +01:00
|
|
|
wildcards::
|
2012-03-08 19:36:47 +01:00
|
|
|
|
2012-03-09 20:39:29 +01:00
|
|
|
*.example.com
|
|
|
|
*.com
|
2012-03-08 19:36:47 +01:00
|
|
|
|
2012-03-08 19:53:48 +01:00
|
|
|
It's also ok to mix wildcard patterns and groups at the same time::
|
2012-03-08 19:36:47 +01:00
|
|
|
|
2012-03-09 20:39:29 +01:00
|
|
|
one*.com:dbservers
|
2012-03-08 19:36:47 +01:00
|
|
|
|
2013-10-07 05:22:09 +02:00
|
|
|
Most people don't specify patterns as regular expressions, but you can. Just start the pattern with a '~'::
|
2013-04-27 18:04:22 +02:00
|
|
|
|
|
|
|
~(web|db).*\.example\.com
|
2013-03-23 06:05:40 +01:00
|
|
|
|
2013-10-21 00:59:12 +02:00
|
|
|
Limiting Selected Hosts
|
|
|
|
```````````````````````
|
2013-06-01 16:40:39 +02:00
|
|
|
|
2013-10-21 00:59:12 +02:00
|
|
|
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.
|
2012-04-18 01:54:23 +02:00
|
|
|
|
2013-09-29 20:45:00 +02:00
|
|
|
Easy enough. See :doc:`intro_adhoc` and then :doc:`playbooks` for how to apply this knowledge.
|
2012-07-20 17:51:03 +02:00
|
|
|
|
2012-03-31 15:29:31 +02:00
|
|
|
.. seealso::
|
|
|
|
|
2013-09-29 20:45:00 +02:00
|
|
|
:doc:`intro_adhoc`
|
2012-03-31 15:29:31 +02:00
|
|
|
Examples of basic commands
|
|
|
|
:doc:`playbooks`
|
|
|
|
Learning ansible's configuration management language
|
2012-03-31 15:55:37 +02:00
|
|
|
`Mailing List <http://groups.google.com/group/ansible-project>`_
|
|
|
|
Questions? Help? Ideas? Stop by the list on Google Groups
|
|
|
|
`irc.freenode.net <http://irc.freenode.net>`_
|
|
|
|
#ansible IRC chat channel
|
2012-03-11 20:34:21 +01:00
|
|
|
|