Working with Patterns

    We’ll go over how to use the command line in Introduction To Ad-Hoc Commands section, however, basically it looks like this:

    Such as:

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

    1. all
    2. *

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

    1. one.example.com
    2. one.example.com:two.example.com
    3. 192.0.2.50
    4. 192.0.2.*

    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:

    1. webservers
    2. webservers:dbservers

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

    1. webservers:&staging

    You can do combinations:

      The above configuration means “all machines in the groups ‘webservers’ and ‘dbservers’ are to be managed if they are inthe 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 thisis uncommonly used:

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

      You also don’t have to manage by strictly defined groups. Individual host names, IPs and groups, can also be referenced usingwildcards

      1. *.example.com
      2. *.com

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

      You can select a host or subset of hosts from a group by their position. For example, given the following group:

      1. [webservers]
      2. cobweb
      3. webbing
      4. weber
      1. webservers[0] # == cobweb
      2. webservers[-1] # == weber
      3. webservers[0:2] # == webservers[0],webservers[1]
      4. # == cobweb,webbing
      5. webservers[:3] # == cobweb,webbing,weber

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

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

      While we’re jumping a bit ahead, additionally, you can add an exclusion criteria just by supplying the flag to /usr/bin/ansible or /usr/bin/ansible-playbook:

      1. ansible-playbook site.yml --limit datacenter2

      And if you want to read the list of hosts from a file, prefix the file name with ‘@’.:

      Easy enough. See and then Working With Playbooks for how to apply this knowledge.

      Note

      You can use ‘,’ instead of ‘:’ as a host list separator. The ‘,’ is preferred specially when dealing with ranges and ipv6.

      See also

      • Examples of basic commands
      • Working With Playbooks
      • Learning ansible’s configuration management language
      • Questions? Help? Ideas? Stop by the list on Google Groups
      • irc.freenode.net
      • ansible IRC chat channel