ansible/docs/docsite/rst/user_guide/intro_patterns.rst

172 lines
6.7 KiB
ReStructuredText
Raw Normal View History

.. _intro_patterns:
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.
.. contents::
:local:
2012-03-09 20:39:29 +01:00
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 :ref:`ad-hoc command<intro_adhoc>` that has no flag. It is usually the second element::
ansible <pattern> -m <module_name> -a "<module options>"
For example::
ansible webservers -m service -a "name=httpd state=restarted"
In a playbook the pattern is the content of the ``hosts:`` line for each play:
2013-10-05 20:57:45 +02:00
.. code-block:: yaml
- name: <play_name>
hosts: <pattern>
2012-03-08 19:53:48 +01:00
For example::
2012-03-08 19:36:47 +01:00
- name: restart webservers
hosts: webservers
2012-03-08 19:36:47 +01:00
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:
2012-03-08 19:36:47 +01:00
Common patterns
---------------
2012-03-08 19:36:47 +01:00
This table lists common patterns for targeting inventory hosts and groups.
2012-07-04 23:44:39 +02:00
.. table::
:class: documentation-table
2012-07-04 23:44:39 +02:00
====================== ================================ ===================================================
Description Pattern(s) Targets
====================== ================================ ===================================================
All hosts all (or \*)
One host host1
Multiple hosts host1:host2 (or host1,host2)
One group webservers
Multiple groups webservers:dbservers all hosts in webservers plus all hosts in dbservers
Excluding groups webservers:!atlanta all hosts in webservers except those in atlanta
Intersection of groups webservers:&staging any hosts in webservers that are also in staging
====================== ================================ ===================================================
.. note:: 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::
webservers:dbservers:&staging:!phoenix
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::
2012-03-08 19:36:47 +01:00
192.0.\*
\*.example.com
\*.com
2012-03-08 19:36:47 +01:00
You can mix wildcard patterns and groups at the same time::
2012-03-08 19:36:47 +01:00
one*.com:dbservers
2012-03-08 19:36:47 +01:00
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:
.. code-block:: text
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: Could not match supplied host pattern, ignoring: *.not_in_inventory.com
Your pattern must match your inventory syntax. If you define a host as an :ref:`alias<inventory_aliases>`:
.. code-block:: yaml
atlanta:
host1:
http_port: 80
maxRequestsPerChild: 808
host: 127.0.0.2
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::
[WARNING]: Could not match supplied host pattern, ignoring: 127.0.0.2
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::
webservers:!{{ excluded }}:&{{ required }}
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::
[webservers]
cobweb
webbing
weber
you can use subscripts to select individual hosts or ranges within the webservers group::
webservers[0] # == cobweb
webservers[-1] # == weber
webservers[0:2] # == webservers[0],webservers[1]
# == cobweb,webbing
webservers[1:] # == webbing,weber
webservers[:3] # == cobweb,webbing,weber
Using regexes in patterns
^^^^^^^^^^^^^^^^^^^^^^^^^
You can specify a pattern as a regular expression by starting the pattern with ``~``::
2013-04-27 18:04:22 +02:00
~(web|db).*\.example\.com
Patterns and ansible-playbook flags
-----------------------------------
[docs][backport]Backportapalooza 10 (#71621) * Feature freeze date has been merged with Ansible-2.10.0beta1 (#71494) (cherry picked from commit c586d436fabc8033055d5195038ca3341d7f0192) * Add --allow-disabled to sanity docs (#71524) (cherry picked from commit bc6461432ef4d0d9323d9b4355efd09e79c0a7b9) * Update intro_patterns.rst (#71542) Call out the trailing comma when specifying a single host. Small snag that took me a while to notice. (cherry picked from commit ec3920cef1692d54c08f1932cf0724239628aff1) * ansible-vault: Fix typo in help message (#71485) (cherry picked from commit 215eb730e1e69f6f7e1566c610ac1b614c50854e) * update install for 2.10 (#71543) * update install for 2.10 (cherry picked from commit f75223d2c6d38ae936a4b813190ff6324d361d7f) * User guide overhaul, Table of Contents (#71553) (cherry picked from commit b694dbadfe0774e1c0a0ad6c971ed8951c60326a) * update backport instructions for 2.11 (#71567) * update backport instructions in docs/docsite/rst/community/development_process.rst Co-authored-by: Matt Martz <matt@sivel.net> (cherry picked from commit 7f9258b024cdd134d2a26a4879f24ac04dfbdb07) * More docs updates to reflect collections ecosystem (#71597) (cherry picked from commit 96aee766f4c2ae2fe769d408a010834ef95dff71) * DOCS: Mentions ansible-base, adds collections pointers to Community and Dev Guides (#71480) (cherry picked from commit 29b20bd1b135d18e9e7942555154ad24e2b1578b) Co-authored-by: Toshio Kuratomi <a.badger@gmail.com> Co-authored-by: Amin Vakil <info@aminvakil.com> Co-authored-by: Matt Deacalion <matt@dirtymonkey.co.uk> Co-authored-by: Fabien Malfoy <fabien.malfoy@laposte.net> Co-authored-by: Alicia Cozine <879121+acozine@users.noreply.github.com>
2020-09-04 01:07:02 +02:00
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. You can also limit the hosts you target on a particular run with the ``--limit`` flag::
ansible-playbook site.yml --limit datacenter2
2012-04-18 01:54:23 +02:00
Finally, you can use ``--limit`` to read the list of hosts from a file by prefixing the file name with ``@``::
ansible-playbook site.yml --limit @retry_hosts.txt
If :ref:`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-playook`` finishes running.
ansible-playbook site.yml --limit @site.retry
To apply your knowledge of patterns with Ansible commands and playbooks, read :ref:`intro_adhoc` and :ref:`playbooks_intro`.
.. seealso::
:ref:`intro_adhoc`
Examples of basic commands
:ref:`working_with_playbooks`
Learning the Ansible configuration management language
`Mailing List <https://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