So friends, previously we just discussed the concept of loops within Ansible
and we learned
that this was a core component of programming. What I now want to do is to talk
to you about
yet another very important subject related to programming and that is
conditional statements.
So with that said, let's just dive in. So we are really digging deep into the
components of
programming right now. The good news is, is it with respect to conditionals
much like with loops.
Whilst we are ultimately using the Python programming language under the hood WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Whilst we are ultimately using the Python programming language under the hood
via Ansible,
the way this particular concept is exposed to us as an end user of Ansible is
once again
through a greatly simplified abstraction layer. Nevertheless, it is a good idea
to understand
what exactly is going on under the hood so that we can correctly apply it
within our playbooks.
So let's talk about what a conditional statement actually is and why we would
want to use it.
Now the first thing to say is that this is a very, very important subject. So
we definitely
want to be paying attention because if you don't pay attention, then you're not WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
want to be paying attention because if you don't pay attention, then you're not
going to be able to
use them with 10 your playbooks. Okay, so confession. What I actually did there
was give you an example
of a conditional statement. Now in the world of Python programming, the way we
define a conditional
statement is using the keyword if basically what we're actually asking is a
billion question.
Now recall we talked about billions that is if something is either true or
something is false.
It is one or the other. It's a light switch, not a dimmer. So let me give you a
very simple example. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
very simple example.
If you walk into Starbucks and you pay for a coffee, then something is going to
happen.
And in this case here, what's going to happen is you're going to get a coffee.
So this part here,
this is actually the condition that we had to meet. And this is the part we're
evaluating on a
billing condition. Is this true or is this false? And if what we evaluate
becomes true, we specify
what is going to happen as a consequence. So ask yourself this, if you pay for
a coffee, and this
is true, I you really did pay for a coffee, then this happens as a consequence, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
is true, I you really did pay for a coffee, then this happens as a consequence,
you actually get a
coffee. Now conversely, if I evaluate this, and it comes to false, so basically
I say, if you pay
for a coffee, and this is false, I you did not pay for a coffee, the condition
below is actually
not going to happen, you're not going to get a coffee. Now this might seem
almost a trivial
example, but this is really how programming logic actually works. It basically
breaks down
into a series of if statements or conditional statements. Now as we're going to WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
into a series of if statements or conditional statements. Now as we're going to
learn my
perspective answer will be actually structure this a little bit differently.
What I'm doing here
is actually representing the underlying Python. So we don't have to worry about
these particular
keywords, but they are actually informative to really understand what we're
trying to do here.
So let's take another example. So let's say we happen to have multiple
conditions, we can therefore
ultimately specify multiple outcomes or multiple consequences. So let's use the
example of the
United States and the US, you have to be 21 years of age or older to buy WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
United States and the US, you have to be 21 years of age or older to buy
alcohol. Whereas if you
happen to be 18, you can vote, but you can't buy alcohol. And if you happen to
be less than 18,
you can't vote or you can't buy alcohol. So really the way we would map this
would be the question
if you happen to be 21 or over, then as a consequence, you can buy beer and you
can vote. But let's say
you don't meet that condition, we can have another one to cover it. And by the
way, just to be clear WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
here, this is not a valid Python code, as you may suggest. This is just an
example of pseudocode
to help you understand the concept. So let's say you are 18 or over, then you
cannot buy beer,
but you can vote. And if you happen to be less than 18, then we have another
condition, which is
no beer or voting. At its core, this is exactly what we're talking about. So if
you had a program
that tested this logic and someone come up who was 25 years old, if they are 21
or above, that is WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
or above, that is
the condition we're testing, or that would actually be true, 25 is greater than
21. Then as a result,
they would be allowed to buy beer and they could also vote. Whereas conversely,
if someone who was
19 years old came up and used a program, this part here would actually evaluate
to false,
because they're not 21 or over there for this part here would not be applicable
. However,
we could then jump to the next condition and ask them, are you 18 or over? Well
, in that case,
the condition is indeed true. So because of that, the consequence here, then WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
the condition is indeed true. So because of that, the consequence here, then
you cannot buy beer,
but you can vote. And then lastly, if someone came up who happened to be 15
years old, we could test
the condition here. If they are over 21, that would be false. So this part here
would not happen.
Are they 18 or over? Well, this is also false. So once again, this part here
would also not happen.
And then they go to the very last condition, if they're under 18, well, that
happened to be true.
So then we specify what is going to happen, no beer or voting for you. So those
are some examples WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
of conditional logic in the real world. But as you're going to learn, this can
be very, very
instructive and useful when dealing with automation. Take for example, we
happen to have this device
here, which happens to be a red heart machine. And similarly here, we have a
device which happened
to be a Debian based machine. Let's maybe say it's Ubuntu. If we are managing
both of these
devices via Ansible, well, the way we actually interact with each device is
going to be a little
bit different depending on the operating system. Say for example, you want to
perform an update of WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
perform an update of
the system, but on a red heart based machine, the package management utility is
going to be DNF.
So basically, if you are a red heart based machine, then you should use DNF.
However,
if you happen to be a Debian based machine, then you should use the apt package
manager.
This is an example of using conditional logic to manage the differences across
devices within
our inventory, because say for example, if I happen to be on a red heart based
machine,
which as we can see here, we are on Red Hat Enterprise Linux. If I try to use WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
which as we can see here, we are on Red Hat Enterprise Linux. If I try to use
the Debian
package manager in Sudo, apt updates and to answer, we can see here the command
is not found. So if
we happen to push out this command automatically to all devices that are
inventory, the ones that
do not happen to be Debian based are not going to operate. The devices such as
this one here,
the rail device, for those devices, we would have to use Sudo DNF update. And
if we hit enter,
suddenly we can update our package manager. And it's going to be using this
conditional logic
that's going to allow us to control this behavior. Now, just like I said, when WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
that's going to allow us to control this behavior. Now, just like I said, when
we use the F keyword,
this is actually a Python construct. When it comes to utilizing Ansible via the
abstraction layer,
we actually use a different keyword. Let's find out what that is in the very
next video.