add example of hosts in multiple inventory groups (#50895)
* add example of hosts in multiple groups * fixed spacing * added nested group example
This commit is contained in:
parent
fdcc2c233e
commit
b023b89ec9
1 changed files with 90 additions and 6 deletions
|
@ -59,10 +59,94 @@ A YAML version would look like:
|
|||
two.example.com:
|
||||
three.example.com:
|
||||
|
||||
.. _host_multiple_groups:
|
||||
|
||||
It is ok to put systems in more than one group, for instance a server could be both a webserver and a dbserver.
|
||||
If you do, note that variables will come from all of the groups they are a member of. Variable precedence is detailed in a later chapter.
|
||||
Hosts in multiple groups
|
||||
------------------------
|
||||
|
||||
You can put systems in more than one group, for instance a server could be both a webserver and in a specific datacenter. For example, you could create groups that track:
|
||||
|
||||
* What - An application, stack or microservice. (For example, database servers, web servers, etc).
|
||||
* Where - A datacenter or region, to talk to local DNS, storage, etc. (For example, east, west).
|
||||
* When - The development stage, to avoid testing on production resources. (For example, prod, test).
|
||||
|
||||
Extending the previous YAML inventory to include what, when, and where would look like:
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
all:
|
||||
hosts:
|
||||
mail.example.com:
|
||||
children:
|
||||
webservers:
|
||||
hosts:
|
||||
foo.example.com:
|
||||
bar.example.com:
|
||||
dbservers:
|
||||
hosts:
|
||||
one.example.com:
|
||||
two.example.com:
|
||||
three.example.com:
|
||||
east:
|
||||
hosts:
|
||||
foo.example.com:
|
||||
one.example.com:
|
||||
two.example.com:
|
||||
west:
|
||||
hosts:
|
||||
bar.example.com:
|
||||
three.example.com:
|
||||
prod:
|
||||
hosts:
|
||||
foo.example.com:
|
||||
one.example.com:
|
||||
two.example.com:
|
||||
test:
|
||||
hosts:
|
||||
bar.example.com:
|
||||
three.example.com:
|
||||
|
||||
You can see that ``one.example.com`` exists in the ``dbservers``, ``east``, and ``prod`` groups.
|
||||
|
||||
You could also use nested groups to simplify ``prod`` and ``test`` in this inventory, for the same result:
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
all:
|
||||
hosts:
|
||||
mail.example.com:
|
||||
children:
|
||||
webservers:
|
||||
hosts:
|
||||
foo.example.com:
|
||||
bar.example.com:
|
||||
dbservers:
|
||||
hosts:
|
||||
one.example.com:
|
||||
two.example.com:
|
||||
three.example.com:
|
||||
east:
|
||||
hosts:
|
||||
foo.example.com:
|
||||
one.example.com:
|
||||
two.example.com:
|
||||
west:
|
||||
hosts:
|
||||
bar.example.com:
|
||||
three.example.com:
|
||||
prod:
|
||||
children:
|
||||
east:
|
||||
test:
|
||||
children:
|
||||
west:
|
||||
|
||||
|
||||
If you do have systems in multiple groups, note that variables will come from all of the groups they are a member of. Variable precedence is detailed in :ref:`ansible_variable_precedence`.
|
||||
|
||||
|
||||
Hosts and non-standard ports
|
||||
-----------------------------
|
||||
If you have hosts that run on non-standard SSH ports you can put the port number after the hostname with a colon.
|
||||
Ports listed in your SSH config file won't be used with the `paramiko` connection but will be used with the `openssh` connection.
|
||||
|
||||
|
@ -110,7 +194,7 @@ In INI:
|
|||
|
||||
[webservers]
|
||||
www[01:50].example.com
|
||||
|
||||
|
||||
In YAML:
|
||||
|
||||
.. code-block:: yaml
|
||||
|
@ -360,9 +444,9 @@ In this example, if both groups have the same priority, the result would normall
|
|||
Using multiple inventory sources
|
||||
================================
|
||||
|
||||
As an advanced use case you can target multiple inventory sources (directories, dynamic inventory scripts
|
||||
or files supported by inventory plugins) at the same time by giving multiple inventory parameters from the command
|
||||
line or by configuring :envvar:`ANSIBLE_INVENTORY`. This can be useful when you want to target normally
|
||||
As an advanced use case you can target multiple inventory sources (directories, dynamic inventory scripts
|
||||
or files supported by inventory plugins) at the same time by giving multiple inventory parameters from the command
|
||||
line or by configuring :envvar:`ANSIBLE_INVENTORY`. This can be useful when you want to target normally
|
||||
separate environments, like staging and production, at the same time for a specific action.
|
||||
|
||||
Target two sources from the command line like this::
|
||||
|
|
Loading…
Reference in a new issue