So in this demo, I've made some changes to my Ansible configuration file to
make automation in Ansible more streamlined. We will also continue our
journey with ad hoc commands and use different modules, for example, the
copy module. Ansible is all about state management, and I will also look at
the different ways Ansible handles the state of present and absent for a
variety of use cases.
So here is my sample config file. In the default section, we can
identify the default options. For the inventory, it'll be looking
for a local file called inventory.
We also have the remote user set to pluralsight1.
Then we have privilege_escalation.
So with the become = True, we don't need to use the b anymore. So we're
not going to be asked for become as pass, as we have set up
password‑less privilege escalation. Notice we have host_key_checking set
to false, and this is enabled by default.
If a host is reinstalled and has a different key in the known host, this
will result in an error message until corrected. So under
privilege_escalation, we also have the become parameter, which controls
whether you will automatically use privilege_escalation.
The default is no or false, and I've set this to true. The become_user controls
what users are in managed host that Ansible should become to. Here we're using
root. The become_method will determine and control how Ansible will switch or
become the escalated user. Sudo is by default. Then we have the become_ask_pass.
This controls whether to prompt for passwords for your become method. The
default is no or false.
Next, we enter the ping command, and here we're going to use a command module.
Here you can see we have a pong result, which is a success.
But do you notice anything different to this command than we did in
the last demo? Here we don't need to specify the u with the root, as
we've made this more streamlined under the privilege_escalation
section in the Ansible configuration file that we just looked at at
the start of this demo.
Next, we will examine the user module, and to fully understand all of its
configuration parameters, we can enter the documentation for this.
I'm quickly going to look at the state and whether
the account should exist or not.
And then Ansible will take action if the state is different from what we
stated. We have absent, for example, and also present.
So here on rocky1 and rocky2, we can check the state for a user called Anna.
So I have not created a user called Anna in the background, and
we didn't do this in the previous demos. We created a user called
pluralsight1 in the previous demos.
So notice with this command, I'm calling a state a present, and as there
is no user of Anna on the Rocky machines and I've called and I've
specified this as present, then Ansible will take action. Here we have a
changed status of true, which means the user has been created for us. So
we can also do this with passwords and set passwords for specific users.
Notice now that we have two users. We have user anna that we've just
created, and we also have an anna1 that I've created in the background for
us.
Next, let's demonstrate the state of absent.
So here I will remove the user of anna1 that I created in the
background, and I can do this by changing the state to absent.
We can test this by running a tail on the /etc/psswd file, and
we'll notice that the user of anna1 is gone.
Next, I will demonstrate the copy module.
So here I can use the copy module, and I can specify 'content="hello viewers"
and set this to a destination on all hosts in the inventory.
So here you will notice that I haven't specified the inventory with the ‑i,
as we have streamlined this configuration. So we previously used the absent
and present arguments for users and passwords, but this is not limited to
just users and passwords. We can use this for a variety of use cases. For
example, for packages.
So here we're going to be looking for the state of httpd.
So you will notice on rocky1 and rocky2, we already have
httpd installed, so we have a change status of false. But
Ubuntu was thrown back in error.
Do you know why this is? Ubuntu uses Apache and not httpd.
So if I change this around and I set my parameters to look
for Apache and check this for presents,
we have the reverse on the Rocky and Ubuntu machines.