So in the inventory, you can store variable values
related to a specific host or group.
This allows you to add variables directly to hosts and groups
in your main inventory file. And as you add more managed
hosts to your Ansible inventory,
you will most likely want to store variables in
separate hosts and group variable files.
So Ansible looks for host variable files in a directory called
host_var and group variable files in a directory called
group_var, and we will demo this pretty soon.
So remember that Ansible expects these directories to be
either a directory that contains your playbooks or the
directory adjacent to your inventory file.
You could even break things out even further if you
want to go one step further. Here,
Ansible lets you define, for example, group_vars
production as a directory instead of a file.
So we briefly mentioned behavior inventory parameters already. This
allows you to, let's say, describe your machines along with
additional parameters in your inventory file. I find quite useful the
Ansible connection parameter. By default, Ansible supports multiple
means of transport, which is the mechanisms Ansible uses to connect
to the managed host.
The following is a list of some of these behavioral inventory
variables and the behaviors they intend to modify.
Firstly, we have ansible_host. This is the DNS name or the
Docker container name that Ansible will initiate a connection
to. Then we have ansible_port.
This specifies the port number that Ansible will use to connect an inventory
host if it's not the default value of 22. Then we have the ansible_user.
This specifies the username Ansible will use to connect with the inventory
host regardless of the connection type.
So you can use your inventory to put hosts and group variables if
you don't have too many hosts in your environment.
However, as your environment gets much larger,
it will become more difficult to manage variables in your inventory.
In this case, you need to find a more scalable approach to
keep track of your host and group variables.
One important point to note is that even though Ansible
variables can hold Booleans, strings,
lists, and dictionaries, in the inventory file,
you can only have Booleans and strings.
Therefore, we have a more scalable approach to keeping track of hosts and group
variables. Here, you can create a variable file for each hosting group. So you
can create a separate variable file for each host and group, and Ansible
expects these variable files to be in YAML format. This allows you to break the
inventory into multiple files.
So as a final note before we get into the demo, if you want,
you can have a regular inventory file and a dynamic inventory script or a
combination of static and dynamic inventory files. Here,
you can just put them in the same directory and configure
Ansible to use that directory as the inventory.
You can do this via the inventory parameter in the ansible.cfg file or
the ‑i flag on the command line. Ansible will process all files and
merge the results into a single inventory.