Okay friends, so now that we understand the general logic behind the
conditional statement,
let's now see how Ansible actually implements these conditional statements.
So let's go directly to the Ansible documentation.
So let's go to the big Google machine and if you just search for Ansible
conditionals,
you will be taken to this particular link here. So let's click on this.
So it says here in a playbook, you may want to execute different tasks or have
different
goals depending on the value of a fact, a variable or the result of a previous WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
goals depending on the value of a fact, a variable or the result of a previous
task.
Basically, at its core, all we're seeing here is we may want to do different
things
depending on certain conditions. Now we can see here that Ansible uses
something known as
Ginger2Tests and filters, we'll talk about that in a later scale. But for now,
what we are going
to focus in on is this part here, basic conditionals with when. Quite simply,
the when keyword is
going to allow us to specify when something is true, it really kind of similar
to what we talked
about in the previous video, when we asked the question if something is true. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
about in the previous video, when we asked the question if something is true.
So let's just look
at the documentation here to see some examples. Let's maybe go to conditions
based on variables.
So we can see we have a really silly but fun example here within the Ansible
docs, we have a
variable with a key called epic and the corresponding value true. We also have
a key called monumental
and the corresponding value. Yes. So let's look at the task. Now the first one
here happens to use
multiple conditions. Let's just forget about that for the moment. Let's focus
on this one here.
The name of the task is run the command if epic happens to be false. So all we WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
The name of the task is run the command if epic happens to be false. So all we
're going to do
here is to use the Ansible built in shell module to accurate the command. This
certainly is an epic
only when we have a particular condition. Now in the case here, as per the
example, we actually
have some type of inversion, we say not epic, which is pretty much just syntax
specifying
that the epic value should be false. Now right now the epic value is set to
true. And as such,
this particular condition here doesn't actually match the condition specified WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
this particular condition here doesn't actually match the condition specified
within the variable.
Therefore this particular shell command is not going to execute. So it's really
all about the use
of this when keyword and specifying a particular condition, which is going to
inform what is going
to happen with the task specifying the condition. Now just below we saw this
use of the word not
and correspondence with the condition epic. What we actually saw here was a
logical operator in
use. So the logical operators are the and operator, the or operator. And as we WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
use. So the logical operators are the and operator, the or operator. And as we
just saw there,
the not operator. Now by default, when we have multiple conditions, the logical
operator at
play that is going to be assumed as the and operator. And the way you pass this
to Ansible is in the
form of a list. So let's take this particular example that we have right here.
Okay. So the
name of the task is to shut down sent to us six systems. So this is something
we're testing for
very, very explicitly. It has to be a sent to a system. And it also has to have
version six.
See that both of these have to be true. So let's look at the actual condition WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
See that both of these have to be true. So let's look at the actual condition
here as specified
via the when keyword once again, as highlighted in the documentation, we
specify this via a list.
So here we have it right here and array object. So the conditions that have to
be true here
is the answerable distribution. And don't worry about this syntax. Ansible
facts,
we'll talk about that later on. This is just simply a way to gather information
about an
Ansible system. So in this case here, we're just testing the distribution. If
that returns the
values sent to us, that is one condition. And because this is true, we can now WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
values sent to us, that is one condition. And because this is true, we can now
test the next
condition, which is as the distribution major version equal to the value six,
if that is also true,
then as a consequence, we have passed the test, because in this case here, we
have the and operator,
this has to be true. And this has to be true. And when that happens, what we're
going to do is
send the shutdown dash T no command to any devices that match that criteria.
And essence,
shutting down all sent to us version six systems. So let's take a look at a WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
shutting down all sent to us version six systems. So let's take a look at a
simple example. Then,
okay. So like I say, a classic example to use with respect to a conditional
statement is to
highlight the differences in package management across Linux systems. We use D
NF for Red Hat
devices, and we would use apt for the Debian based devices. Now, as it trans
pires, both of the
devices in our inventory happen to be Red Hat enterprise Linux. Therefore, they
both use DNF.
So we're not going to use this as an example. What I'm going to do is I'm going
to go into my
inventory, and I'm going to add in some additional groups. So let's imagine we WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
inventory, and I'm going to add in some additional groups. So let's imagine we
have a UK group,
and imagine that the blue box is located in the United Kingdom. And similarly,
let's have a USA
group. And let's imagine that yellow box resides in the USA. Now, of course,
that says very, very
trivial. We only have two devices, one of each in each group. But you could
easily imagine if we
happen to have thousands of devices, maybe say purple box and green box. They
could be in the USA.
Whereas in the UK, we can have blue box, pink box and a whole bunch of others.
You kind of get
the drift. We don't have to be limited to one device per group. But in this WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
the drift. We don't have to be limited to one device per group. But in this
case here, we only
have two devices to manage. So let's just write out the changes. Here we can
see we have now
updated the inventory. What I want to do is I want to test it via a conditional
statement. So
let's just call this condition dot yml. If I go into insert mode, have my three
items at the top,
give my play a name, creating files based on location. So what I'm going to do
is I'm going to target
all of my devices. And I will specify the tasks that I want to happen. So the
name of the task
is going to be create UK file for UK devices. And I'm going to use the Ansible WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
is going to be create UK file for UK devices. And I'm going to use the Ansible
built in shell module.
And all I'm going to do is say echo, I am a UK device. And I'm going to
redirect that
into the 10th directory. And I'll put it into a file called location dot txt.
Now similarly,
I'm going to have another one, a rather another task. And I'll just say create
USA file for USA
devices. Similarly, we're going to use the same module Ansible built in shell.
And we're just
going to echo this time. I am a USA device and redirect that into temp location WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
going to echo this time. I am a USA device and redirect that into temp location
once again.
Now the crucial thing to note here is that we don't want this to happen for
both devices as
it's going to happen right now. So here is the condition that I'm going to test
. I'm going to use
the condition when I'm going to have the keyword UK. And I'm going to specify
that when we have UK
inside of the group underscore names. Now where on earth am I getting group
underscore names from?
That just happens to be what is known as a magic variable. Basically, this is
just one of the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
just one of the
special variables that Ansible comes shipped with that holds particular
information about the
device within the inventory. Now we'll talk more about magic variables later on
. So definitely
don't get worried thinking I would never known to use that particular magic
variable. So all I'm
doing here is looking at all the groups that the device happens to be a member
of. And if there
happens to be a group called UK in the list of groups to which they are a
member, that condition
therefore would be true. And as a consequence, we are going to run this
particular shell command.
I am a UK device and redirect into location.txt. So if you happen to have a WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
I am a UK device and redirect into location.txt. So if you happen to have a
device in the USA,
which is not in the UK group for that particular device, this condition here is
going to be false.
And when it's false, that means that the command above, i.e. echo, I am a UK
device is not going
to execute. Now we also want to do a similar thing for the below task. So we'll
add in the when keyword.
Again, we'll have our double quotation marks with single quotation marks
contained within
specifying USA once again in the group names, magic variable. So therefore, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
specifying USA once again in the group names, magic variable. So therefore,
this particular
command is only going to execute for devices that belong to the USA group. Okay
, so let's just
write and quit. If I just say answerable playbook condition dot yml and to
enter
answerable executes. And as it transpires, answerable is actually skipped
absolutely everything. So
that wasn't supposed to happen, but I know exactly why it did happen. And this
is quite good actually.
This is very, very important. We can see here for the task create UK file for WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
This is very, very important. We can see here for the task create UK file for
UK devices.
Answerable has skipped both the yellow box and the blue box. And for the next
task, create a USA
file for USA devices. Answerable once again has skipped both devices. Pretty
much answerable has
denoted that the when condition in both cases happened to be false. Now why
exactly is that?
Well, let's just look briefly at the inventory once again, if we cat the file.
Check this out. The UK group happens to be UK in lower case and the USA group
happens to be in
lower case as well. Whereas if we look at the actual conditions as specified WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
lower case as well. Whereas if we look at the actual conditions as specified
via the answerable
playbook, the condition here is USA in capital letters and UK in capital
letters. So pretty much
we don't have a capital letter UK group. So this is false for both devices and
we don't have a
capital letter USA group for both devices. So it's going to be skipped twice.
So let's just briefly fix this. And if I go into insert mode and I'll just
change this to
lower case, scroll on down and also change USA to lower case. Let's just exit
to the
scape, right and quit, clear the screen, arrow up and let's rerun the playbook. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
scape, right and quit, clear the screen, arrow up and let's rerun the playbook.
Aha, this is much better this time to look at the output here. It says for the
task creating UK
file for UK devices, it has skipped the yellow box but it changed the blue box.
That's because
the blue box happened to be a member of the UK group. Whereas when you go down
to the next task,
create a USA file for the USA devices, the inverse is true. We have skipped the
blue box,
whereas we have changed the yellow box again based on the conditions. So if we
go to say, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
go to say,
for example, the blue box, we should have a UK file as specified like a say in
our inventory,
blue box is UK, yellow box is USA. So and say the blue box, we go to the 10th
directory,
we have this file called location dot txt. If we cat this fail, it says here, I
am a UK device,
as expected. Let's go to yellow box here, we see the enter the 10 directory, do
an LS,
we have the location fail, let's cat this fail and as we can see here, for the
yellow box,
we have applied the text, I am a USA device.