So friends, now that we have a better understanding about YAML, what we now
want to do is to actually
get hands on with Ansible.
Now here's the thing, the very first thing we're going to be doing with Ansible
is not
going to involve YAML or Playbooks.
We're going to be looking at something called ad hoc commands to effectively
manage these
two machines we have right here on the fly directly via the command line.
So with that said, let's just do it.
So like I say, I have my two virtual machines.
I have my yellow box and my blue box right here, let me just minimize these WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
I have my yellow box and my blue box right here, let me just minimize these
from now.
And right here I have my what is going to be control node for Ansible.
So right now within my directory structure, I have my inventory as we know, let
's just
denote my blue box and my yellow box.
And these are correlating to the IP addresses within the XA host configuration
file.
We can see here I have mapped blue box to the IP address of 192.168.134, which
is the
first machine we're managing, as well as we have the name yellow box correl
ating to
the dot 135 address.
So we have these two devices map within our inventory. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So we have these two devices map within our inventory.
We can refer to both devices at once by just referencing the all group, which
comprises
all the devices we're managing, which is only two after all.
And we have our Ansible CFG file.
So inside of here, we specify where our inventory is.
The user we're going to log in as is going to be Ansible.
And we have some basic information about how we elevate the privileges.
We talked about this in the previous skill.
Now right now I am actually not going to go and write a playbook.
Like I said, we're going to use these ad hoc commands. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Like I said, we're going to use these ad hoc commands.
Now ad hoc is simply a word that actually comes from Latin.
And it just simply means for this.
And this actually kind of tells you the purpose and use case for using ad hoc
commands.
It's kind of like a off the cuff thing you would do for a one off type of thing
.
These are absolutely not meant to replace the use of playbooks.
It should be used for more repeatable, reliable and well tested automation.
But sometimes perhaps maybe you don't have a particular playbook already
created.
And you just want to make a quick change to a few devices in the network. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And you just want to make a quick change to a few devices in the network.
Good way we can instruct Ansible to do this is just quickly via an ad hoc
command.
So before we actually run a command, let's go and check the Ansible
documentation.
So if you just search for Ansible ad hoc commands, we can see we have this
documentation
right here.
Let's click the link.
Now it says right here an Ansible ad hoc command is a command line tool like I
say
it's not a playbook.
And it's used to automate a single task on one or more managed nodes.
Basically you can specify which set of devices within your inventory you want
to target.
And you can effectively induce a single task on all of those devices. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And you can effectively induce a single task on all of those devices.
So we see the actual use case for ad hoc commands.
Like I say, they can be quickly used to reboot servers, copy files, that type
of thing.
So let's actually check out some of the syntax we can see here.
So let's scroll on down.
Let's look at this example and the documentation right here.
It says Ansible Atlanta dash a and then it says a spin reboot.
What's going on here?
The first commands we see here is the word Ansible that is just invoking Ans
ible to run
an ad hoc command.
The next one here says Atlanta.
Now in this case here Atlanta will be a group in the user's inventory. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now in this case here Atlanta will be a group in the user's inventory.
In our case, all we have is an all group.
But like I say, as we know within the inventory, we can group things up by
location or via
function.
In this case here, they are apparently grouping devices via the location.
So in this case here Atlanta, Georgia.
So all devices in Atlanta, we're going to specify a particular argument and
this is
effectively the command that we want to send.
So pretty much we want to call the a spin reboot binary, which is going to
reboot all
of the devices within the Atlanta group. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
of the devices within the Atlanta group.
Simply just typing this command on the command line could be potentially reb
ooting thousands
of devices in Atlanta all at the same time.
Now notice here we have this option dash f for forks.
This is actually really important as to how Ansible operates.
We can see here by default Ansible is only going to use five simultaneous
processes pretty
much it will connect to five devices at the same time.
Then once that is done, it will spin up a new process, connect to another five
and then
another five and then another five.
If you only happen to have five or 10 devices, this is not really much of an WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
If you only happen to have five or 10 devices, this is not really much of an
issue.
But if you happen to have thousands of devices, this can take a long time.
So you can effectively touch your computer more.
You test the power of the CPU and you can increase the amount of parallel
connections
at once.
This is called forking.
So imagine if you happen to have 100 devices, if you only connect to two
devices at a time
that's going to take a long time.
Whereas if you connect to 50 devices at the same time, well, it's only going to
take Ansible
two runs to effectively automate all of the devices in the network. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
two runs to effectively automate all of the devices in the network.
But be aware of course that will stress your CPU.
Now again, we can see some more information right here by default.
You can see that Ansible is going to be running from your user account.
If you want to specify a different user name from the command line, you can use
a dash
u flag.
Similarly, we can use the dash M flag, which is going to allow to specify
different modules
we want to use Ansible actually has a whole ecosystem of modules for particular
tasks.
And we'll get to look at that in more detail in an upcoming scale.
But perhaps you want to maybe copy particular files. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
But perhaps you want to maybe copy particular files.
You could use a copy module specified with the dash M flag.
You just want to run some regular shell commands.
You can use the shell module.
There are a whole bunch of different ones available to us.
So check this out.
Perhaps I want to create a new directory on all of my devices as opposed to
logging in
via SSH one by one and doing this manually.
What I can do is just say Ansible and then all to target the all group.
I'll then say dash M to specify the module.
And in this case here, I want to call the shell module. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And in this case here, I want to call the shell module.
I'm going to be typing a regular shell command and the argument I want to pass
in via the dash
A flag is simply within inverted commas.
MKDAR.
Let's just say forward slash TMP forward slash Ansible test.
So if I hit enter now, we can see here we get somebody back.
It says yellow box changed.
And it also says blue box changed.
This indicates that we have successfully made a change on both of those devices
.
So let's go and test this in.
So let's crack open a terminal window and I will just cd enter the temp WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So let's crack open a terminal window and I will just cd enter the temp
directory if
I do an LS.
Look at this right here.
We actually do have a directory called Ansible test now right now and say to
that directory,
we don't have any content because we didn't create any content.
If we so wished we could also create a file within this directory or perhaps
even copy
a file from one directory to another that type of thing.
Similarly, if we go over to our other machine, the yellow box, which is full
screen this
go to activities terminal and we just cd enter the temp directory as well doing
LS.
Notice we have also created a new directory here as well. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Notice we have also created a new directory here as well.
Let's maybe say I wanted to say Ansible all and I will use the shell module
once again.
It will specify the argument is going to be and this time I will say IP adder
to get
the IP address information and I'm going to grep for 192.
If I hit enter, we can quickly see within the yellow box, we have the IP
address 192.168.135.
And in the blue box, we have the address 192.168.160.134, easily accessing all
the IP address information, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
the IP address information,
all the devices within our inventory.
Now as we're going to explore, there are a whole bunch of different Ansible
modules.
Let's maybe say we could use the user module, which is designed to create brand
new users
on the system.
We can say -a for the arguments and all I will say is name is equal to and then
give
a new username, let's just say test user and I will say the state is equal to
present.
If I hit enter, check this out right here.
We can see on the yellow box, we have a change and look at this information
here.
We have this new user.
The name is test user, they have a home directory, they have a UID. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
The name is test user, they have a home directory, they have a UID.
Similarly, if we go up to the blue box, also in the blue box, we have created
this user,
they also have a UID.
And once again, if I so wished, I can maybe cat the Etsy group file right here
and look
at this right here.
If we scroll on up, we can see all of the group information for yellow box and
at the
very bottom, we have the new user we just created and if we scroll up to the
top, we
can see the information for the blue box and just moving on down a little bit.
We can also see the test user exists on the blue box too. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
We can also see the test user exists on the blue box too.
Now just one thing to note, sometimes when using these commands, you might get
erroneous
information from Ansible.
Quite simply, you might be told that a task has failed, even though the task
has actually
succeeded, let me show you what I mean.
So let me go and pull up these devices right here.
So clearly we can see on the right hand side, we have a blue box and yellow box
.
What I'm going to say here is Ansible, all and the module is going to be the
shell module
and the argument is going to simply be Espin, a reboot and if I hit enter, look
on the
right hand side, they actually are rebooting but notice the information we're WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
right hand side, they actually are rebooting but notice the information we're
getting
back here, apparently it says the blue box is unreachable and the yellow box is
unreachable
failed to connect to the host via SSH.
However, we can clearly see that the command that we sent, the reboot command,
did indeed
reboot both of these devices.
So in this case here, because the actual SSH connection was disturbed due to
the fact
that we did reboot from an ad hoc command, Ansible falsely interpreted that as
a failure
in the connection, even though the task itself was indeed successful, just WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
in the connection, even though the task itself was indeed successful, just
something to be
aware of when running these ad hoc commands.