In this demo, we will be looking at ways to organize your inventory.
You can have, for example, groups of groups and group variables. If you
recall from the first module in the first demo, we added variables to
hosts. Now we will add variables to groups. As you add more and more
managed nodes to your Ansible inventory, you will likely want to store
variables in separate host and group variable files as opposed to the
inventory itself.
Keeping host and group‑specific variables separate from the inventory
file is the preferred best practice in Ansible and not to store
variables in the main inventory file. So we can have inventory groups
that have a number of hosts, and hosts can have groups, and groups
can have child groups.
Then, through the inventory system you can apply variables. That
could be set to hosts and to groups of hosts.
We would start by opening up the ansible.cfg file and adding
some plugins. Most inventories will be in any format, but we
can have a number of options here.
We do this with the enable plugin option in a new directory called inventory.
Next, we can look at examples of configuring inventory in any format.
This command will provide useful examples of the different ways you can
group hosts and groups and apply different variables.
We can also look at different examples of configured inventory in YAML format.
Next, I want to double check the inventory again.
So nothing has changed from the last module, and we still have three
managed hosts, one Ubuntu, and two Rocky, and one control node. Notice
that no additional variables have been assigned.
So we typically want to perform actions on groups of
hosts rather than on single hosts.
So you can query all hosts in the inventory, and we have two ways.
So here I will display both ways of looking at the date
of each host in the inventory. Here, Ansible defines a group called all,
which includes all hosts in the inventory.
Now let me demo some different ways of configuring host
and group variables and inventory.
So we could define groups and inventory host files, and Ansible
uses any file format for inventory host files, and it groups
configuration values into sections.
So here we are specifying non‑default SSH ports with the use of
behavioral variables. In the any format inventory file, the host
could also have an IP address or an FQDN.
So we can have one group, and you can have all the hosts
below belong to that group, or the host can be a member of
many groups as you need them to be.
I'm using the location and the location of Dublin, but you
can have a naming based on, for example,
East data center,
West data center or more than likely production or staging. Ideally, you
should not give the group the same name of the host,
especially if you plan to share the inventory file.
We made this error in the very first demo in module 2. As
you might expect, host1, host2, and host3 are actual
aliases and not real hostnames.
They are just useful for referring to these hosts.
Groups can also have other groups in the any format inventory, and you
can add nested groups with the children's suffix.
So we can have group variables where variables are
applied to the entire group at once.
So next, we're going to look at a more scalable approach to holding variables
where we can hold them in a separate file. If you want,
you can have a separate file for each host or each group.
So we're going to create a directory called group_vars.
And in this directory, we're going to call it testingwebserver. At
this moment, please take note of the group name, testingwebserver, as
this is really important to match later on.
So in this file, we're going to have simply a web package and the
web service, so it's going to be pretty basic.
Now let us create our inventory file, and we're going to be calling these two
variables that are in the group_vars/testingwebserver YAML file. So I'm going
to change the groups in my inventory file, too.
Remember that the group_vars file must match the groups in the inventory that
we're applying the variables to. This was testingwebserver.
Next, let us run the playbook, and you can notice that we're getting
an error, Could not match supplied host pattern.
So can you recall why this would be?
So let us go back to the inventory and make sure I've done the correct
naming. Here, you can see I've misspelled the group name.
So let me write this to testingwebserver, and then we'll run the playbook again.