It is also possible to make groups of groups using the ``:children`` suffix in INI or the ``children:`` entry in YAML.
You can make groups of groups using the ``:children`` suffix in INI or the ``children:`` entry in YAML.
You can apply variables using ``:vars`` or ``vars:``:
You can apply variables to these groups of groups using ``:vars`` or ``vars:``:
..code-block:: guess
..code-block:: guess
@ -256,7 +257,7 @@ Child groups have a couple of properties to note:
.._default_groups:
.._default_groups:
Default groups
Default groups
++++++++++++++
==============
There are two default groups: ``all`` and ``ungrouped``. ``all`` contains every host.
There are two default groups: ``all`` and ``ungrouped``. ``all`` contains every host.
``ungrouped`` contains all hosts that don't have another group aside from ``all``.
``ungrouped`` contains all hosts that don't have another group aside from ``all``.
@ -265,22 +266,18 @@ Though ``all`` and ``ungrouped`` are always present, they can be implicit and no
.._splitting_out_vars:
.._splitting_out_vars:
Splitting Out Host and Group Specific Data
Organizing host and group variables
++++++++++++++++++++++++++++++++++++++++++
===================================
The preferred practice in Ansible is to not store variables in the main inventory file.
Although you can store variables in the main inventory file, storing separate host and group variables files may help you track your variable values more easily.
In addition to storing variables directly in the inventory file, host and group variables can be stored in individual files relative to the inventory file (not directory, it is always the file).
Host and group variables can be stored in individual files relative to the inventory file (not directory, it is always the file).
These variable files are in YAML format. Valid file extensions include '.yml', '.yaml', '.json', or no file extension.
These variable files are in YAML format. Valid file extensions include '.yml', '.yaml', '.json', or no file extension.
See :ref:`yaml_syntax` if you are new to YAML.
See :ref:`yaml_syntax` if you are new to YAML.
Assuming the inventory file path is::
Let's say, for example, that you keep your inventory file at ``/etc/ansible/hosts``. You have a host named 'foosball' that's a member of two groups: 'raleigh' and 'webservers'. That host will use variables
in YAML files at the following locations::
/etc/ansible/hosts
If the host is named 'foosball', and in groups 'raleigh' and 'webservers', variables
in YAML files at the following locations will be made available to the host::
/etc/ansible/group_vars/raleigh # can optionally end in '.yml', '.yaml', or '.json'
/etc/ansible/group_vars/raleigh # can optionally end in '.yml', '.yaml', or '.json'
/etc/ansible/group_vars/webservers
/etc/ansible/group_vars/webservers
@ -304,7 +301,7 @@ Ansible will read all the files in these directories in lexicographical order. A
All hosts that are in the 'raleigh' group will have the variables defined in these files
All hosts that are in the 'raleigh' group will have the variables defined in these files
available to them. This can be very useful to keep your variables organized when a single
available to them. This can be very useful to keep your variables organized when a single
file starts to be too big, or when you want to use :doc:`Ansible Vault<playbooks_vault>` on a part of a group's
file starts to be too big, or when you want to use :ref:`Ansible Vault<playbooks_vault>` on a part of a group's
variables.
variables.
Tip: The ``group_vars/`` and ``host_vars/`` directories can exist in
Tip: The ``group_vars/`` and ``host_vars/`` directories can exist in
@ -320,8 +317,8 @@ is an excellent way to track changes to your inventory and host variables.
.._how_we_merge:
.._how_we_merge:
How Variables Are Merged
How variables are merged
++++++++++++++++++++++++
========================
By default variables are merged/flattened to the specific host before a play is run. This keeps Ansible focused on the Host and Task, so groups don't really survive outside of inventory and host matching. By default, Ansible overwrites variables including the ones defined for a group and/or host (see :ref:`DEFAULT_HASH_BEHAVIOUR<DEFAULT_HASH_BEHAVIOUR>`). The order/precedence is (from lowest to highest):
By default variables are merged/flattened to the specific host before a play is run. This keeps Ansible focused on the Host and Task, so groups don't really survive outside of inventory and host matching. By default, Ansible overwrites variables including the ones defined for a group and/or host (see :ref:`DEFAULT_HASH_BEHAVIOUR<DEFAULT_HASH_BEHAVIOUR>`). The order/precedence is (from lowest to highest):
@ -348,8 +345,8 @@ In this example, if both groups have the same priority, the result would normall
.._behavioral_parameters:
.._behavioral_parameters:
List of Behavioral Inventory Parameters
Connecting to hosts: behavioral inventory parameters