In this first demo,
we're going to get you started with Ansible and set up the
control and managed hosts so that the environment can be
controlled by the Ansible controller.
So there are a few requirements on the managed host that need
to be performed before they can be managed.
The managed host must have SSH running and be reachable,
and there also needs to be a user with admin privileges.
We also need to make sure that the host names are in the inventory.
Once this is done,
we can start to experiment with Ansible ad hoc
commands and playbooks in later demos.
First, let us check the etc/host file,
as Ansible requires DNS either locally or remotely configured.
You can see we have a number of hosts.
We have four in total.
One of the hosts is the control node used to manage the managed assets,
and the other three are the managed assets.
Rocky, our first host, is our control node.
Ubuntu is one of the managed assets.
Then we have rocky1 and rocky2, which are also our managed assets,
similar to ubuntu.
I'm going to quickly ping by host name to test resolution,
and all is looking good so far.
If we do a cat on the release, on rocky,
which is our control host,
the Ansible software is an epel, which is an extra package for enterprise Linux.
Here we use the epel‑release, which gives you access to the epel repository.
The command dnf allows you to find packages that contain certain software.
Here we're going to select yes to both options,
and this will allow us to start the Ansible install.
Here the Ansible software is installing, along with its dependencies.
All of the Ansible dependencies are python3‑based packages,
and as you know, Ansible is developed on top of Python.
To discover the version we are using on our system
that Ansible has already installed, we make use of the ‑‑version argument.
Here you will see the Ansible version that has been installed,
along with the current working environment.
Here you can see that I'm using Ansible 2.12.2.
Ansible's behavior is defined by a configuration file,
and this central configuration file can be found at this
following location in the ansible.cfg, so let's take a quick look at this file.
As you can see, nearly the entirety of this file is commented out,
and this is intentional.
Other than the section headings,
Ansible is providing you with a glimpse of some of the default
values it has chosen for its configuration.
I have quickly done a search for anything in the inventory.
You only need to supply an overriding value when you wish to change
from the default settings in this configuration file.
So as you can see, the ansible.cfg has several sections,
and each of these sections are defined as key‑value pairs.
Section titles are enclosed in square brackets and the key
value pairs are set as key equals value.
The SSH configuration is also an important section.
You may want to tune SSH for a particular host or optimize SSH
connectivity to support larger environments.
If you want to see a straightforward display of all of the possible
configuration parameters along with their current values,
you can run the following ansible‑config list command.
One of the configuration options here is Ansible pipelining.
That is used to optimize Ansible connectivity.
Now, let us build out a very basic inventory.
The hosts I'm building into this inventory are the same hosts that you
saw at the start of the demo in the etc host file.
With the square brackets,
we can create groups that can hold multiple managed assets in the same group.
I have one host, ubuntu, and I'm going to put this in a group called ubuntu.
Then for my other two hosts, rocky1 and rocky2,
I'm going to put this in a group called ansible.
As a best practice,
it's much better to group hosts than to have a bunch of
individual hosts in your inventory.
Now with the inventory list command,
we can check all of our hosts that have been configured
in this very basic inventory file.
As you can see, we immediately have a warning.
We have found both a group and host with the same name,
ubuntu,
so as a best practice we really should not have the same host
name and group name in the inventory file.
But apart from that warning,
we have a hostvars section which is used for host
variables that are set in the inventory.
Variables help us separate site‑specific information,
and variables can be set in various places.
The inventory is one of them.
You can see we also have a default group of all and
another group called ungrouped,
and then we have the two groups with the hosts that I
explicitly configured in our inventory file.
So let me quickly go back and change the name of the
ubuntu group from ubuntu to ubuntu1.
So I just mentioned variables, and there are different types of variables,
and they can be set in a lot of places in Ansible. And in this
inventory, we're going to set some behavior in variables,
and this can be used to tune or to change the host
configuration that we're setting the variables on.
Here I'm going to set some non‑default configurations to our rocky1
host. Here I'm going to set the port and also the host IP address.
So once we enter the list command,
you can see the new settings under the hostvars section,
which specifies any variables you set explicitly on the host in the
inventory, and this concludes our first demo.