2018-02-13 15:23:55 +00:00
Working with Patterns
=====================
2012-03-09 16:44:14 +00:00
2013-12-26 19:32:01 +00:00
.. contents :: Topics
2013-09-29 18:45:00 +00: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 19:39:29 +00:00
2013-09-29 23:30:10 +00: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 14:38:24 +00:00
ansible <pattern_goes_here> -m <module_name> -a <arguments>
2012-12-11 02:48:38 +00:00
2012-03-31 14:38:24 +00:00
Such as::
ansible webservers -m service -a "name=httpd state=restarted"
2013-10-05 18:57:45 +00: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 18:45:00 +00: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 14:38:24 +00:00
This is done by designating particular host names or groups of hosts.
2013-10-05 18:57:45 +00:00
The following patterns are equivalent and target all hosts in the inventory::
2012-03-08 18:53:48 +00:00
all
2012-12-11 02:48:38 +00:00
*
2012-03-08 18:36:47 +00:00
2013-10-05 18:57:45 +00:00
It is also possible to address a specific host or set of hosts by name::
2012-03-08 18:36:47 +00:00
one.example.com
2015-10-31 20:25:35 +00:00
one.example.com:two.example.com
2016-09-11 09:38:36 +00:00
192.0.2.50
192.0.2.*
2012-12-11 02:48:38 +00:00
2015-12-01 03:44:18 +00:00
The following patterns address one or more groups. Groups separated by a colon indicate an "OR" configuration.
2013-09-29 18:45:00 +00:00
This means the host may be in either one group or the other::
2012-03-08 18:36:47 +00:00
webservers
2015-10-31 20:25:35 +00:00
webservers:dbservers
2012-03-08 18:36:47 +00:00
2013-09-29 18:45:00 +00: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 21:44:39 +00:00
2015-11-06 18:54:39 +00:00
webservers:!phoenix
2012-07-04 21:44:39 +00:00
2013-09-29 18:45:00 +00: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 15:47:52 +00:00
2015-10-31 20:25:35 +00:00
webservers:&staging
2013-02-09 15:47:52 +00:00
You can do combinations::
2015-11-06 18:54:39 +00:00
webservers:dbservers:&staging:!phoenix
2013-02-09 15:47:52 +00:00
2013-09-29 18:45:00 +00: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 15:47:52 +00:00
2015-10-31 20:25:35 +00:00
webservers:!{{excluded}}:&{{required}}
2013-02-09 15:47:52 +00:00
2013-09-29 18:45:00 +00:00
You also don't have to manage by strictly defined groups. Individual host names, IPs and groups, can also be referenced using
2017-01-07 19:38:52 +00:00
wildcards
.. code-block :: none
2012-03-08 18:36:47 +00:00
2012-03-09 19:39:29 +00:00
*.example.com
*.com
2012-03-08 18:36:47 +00:00
2012-03-08 18:53:48 +00:00
It's also ok to mix wildcard patterns and groups at the same time::
2012-03-08 18:36:47 +00:00
2015-10-31 20:25:35 +00:00
one*.com:dbservers
2012-03-08 18:36:47 +00:00
2015-08-14 01:44:42 +00:00
You can select a host or subset of hosts from a group by their position. For example, given the following group::
2014-09-30 19:17:28 +00:00
2015-08-14 01:44:42 +00:00
[webservers]
cobweb
webbing
weber
2014-09-30 19:17:28 +00:00
2015-09-17 08:05:37 +00:00
You can refer to hosts within the group by adding a subscript to the group name::
2015-08-14 01:44:42 +00:00
webservers[0] # == cobweb
webservers[-1] # == weber
2018-02-23 10:46:31 +00:00
webservers[0:1] # == webservers[0]
# == cobweb
2015-09-18 16:58:34 +00:00
webservers[1:] # == webbing,weber
2018-02-23 10:46:31 +00:00
webservers[:3] # == cobweb,webbing,weber
2014-09-30 19:17:28 +00:00
2013-10-07 03:22:09 +00:00
Most people don't specify patterns as regular expressions, but you can. Just start the pattern with a '~'::
2013-04-27 16:04:22 +00:00
~(web|db).*\.example\.com
2013-03-23 05:05:40 +00:00
2014-01-22 05:05:21 +00:00
While we're jumping a bit ahead, additionally, you can add an exclusion criteria just by supplying the `` --limit `` flag to /usr/bin/ansible or /usr/bin/ansible-playbook::
2013-06-01 14:40:39 +00:00
2013-11-08 15:51:28 +00:00
ansible-playbook site.yml --limit datacenter2
2012-04-17 23:54:23 +00:00
2017-11-07 19:28:56 +00:00
And if you want to read the list of hosts from a file, prefix the file name with '@'.::
2014-10-01 23:49:33 +00:00
ansible-playbook site.yml --limit @retry_hosts.txt
2013-09-29 18:45:00 +00:00
Easy enough. See :doc: `intro_adhoc` and then :doc: `playbooks` for how to apply this knowledge.
2012-07-20 15:51:03 +00:00
2017-11-22 04:14:27 +00:00
.. note :: You can use ',' instead of ':' as a host list separator. The ',' is preferred specially when dealing with ranges and ipv6.
2015-10-31 20:35:13 +00:00
2012-03-31 13:29:31 +00:00
.. seealso ::
2013-09-29 18:45:00 +00:00
:doc: `intro_adhoc`
2012-03-31 13:29:31 +00:00
Examples of basic commands
:doc: `playbooks`
Learning ansible's configuration management language
2012-03-31 13:55:37 +00: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