In this video, we are going to learn the conditions that can be applied in Ansible playbooks.
Condition execution.
Allow Ansible to take actions on its own based on certain conditions.
So this becomes a the playbook becomes a lot more logical and smarter when we apply conditions inside
of it.
Under condition, certain values must be met before executing a task.
We can use the vendor statement to make ansible automation more smart for our conditions.
So for those of you who have worked with programming languages like Shell scripting or any other programming
languages, I'm sure you're aware for conditions of use if then statement meaning if this happens,
then do this.
Similarly, we are going to use one statement within Ansible for our conditions.
Let's take a look at our example where we could have then statement applied.
So here is an example playbook.
The name is just a playbook.
Description host equal the local host and the task is going to start a service, any service for that
matter.
It doesn't really I don't really care, but it's just an example now.
Here is the condition.
Start a service only if when A equals equal to B, if it is, then service the name of service and the
state of the service.
See, that's where the condition comes in.
If the condition is not met or it's not suffice the requirement, then the task will not run.
All right.
Let's take a look at the real life example.
Here we have a playbook.
The name of the playbook is to install Apache Server, which is one of our favorite application to install
and work with.
And the host we wanted to do it is against our local host.
All right, then we have a tasks and the name of the first task is to install Apache on a Bantu server.
All right.
And the, and the Ansible module we will use for a Bantu is apt or its apt dash kit.
Once we have that module enabled for it to install will install Apache to package in a addition to our
Debian.
It's not a HTTP feed, it's Apache two.
Then it will take the same option state and we'll put it present and then comes in the condition.
The condition is you should install this Apache only if or when the ansible underscore ohs underscore
family equal equal a bun to.
Now you're probably wondering what is underscore what is ansible underscore os underscore family is
it's actually ansible built in variable.
All right.
So then the next task is install Apache on Centaur server.
All right.
For that, you have to use a Yum module and the name is http dx and the state is present.
Now again we are going to apply the condition and for this one the condition has to be as low as family
equals to be red hat.
And right here, look at this.
The when or the condition has to be vertically aligned with name and the module, and that's the only
way we'll run.
Otherwise it will give you an error message.
Now you're probably thinking, hold on, hold on.
Question how to get a list of all Ansible built in variables.
See, I have provided you the ansible ohs underscore OS underscore family.
But what if you wanted to use something else or different conditions?
Well, that's a very good question.
All the variables are gathered from the facts, and in order to get the list of the facts of a machine,
you have to get through a command line by running Ansible and the name of the host minus M is the module
option and the setup is the actual module.
This will give you the list of all the facts, and then facts and variables are gathered from the facts.
All right, let's go to our Ansible control node right here and we will go to let's log in.
All right.
We are an at sea as well playbook.
Who am I?
I am logged in as route.
All right, so I already created this playbook in my playbook directory.
So that's myself here.
And right here is HTTP bi condition.
That's the playbook that I will be using or running by.
Condition.
All right, so same thing.
Whatever I have listed here in this playbook, it's exactly the same.
Except this part I have put in Debian and Debian.
You could change a bun too if you have a bunch to anyway.
Pay attention on.
Right here is the Apache two I'm sorry on the when Ansible and the score was families Debian and ansible
OS underscore families debut now I put it in red hat at the bottom because I know this machine that
I'm on, it's Red Hat so I want it to check this task first, this condition first, then go to the
next one.
All right.
So we are doing a reinstalling HTTP.
Of course, what we have to do, we have to make sure we remove the HTTP package from the system before
we run it.
So we know for sure that it did run successfully.
All right.
So it did run.
It did remove it.
Let's clear it now.
Run, ansible dash playbook.
And the name of the playbook, which is HTTP by condition.
And let's run it.
All right.
So you see right here, install Apache Web server, which is the main name of the playbook gathering
facts, which is right here for this local host.
Now task right here, install Apache on Debian.
So a bun two.
So right here says skipping because this machine is not Debian nor a Bantu.
So that's why it's skipping.
Its went to the next task which is to install Apache on Santos change local host and there you go it
says change.
So it did work, skipped it and come to the right OS family.
Now let's verify that it did install the HTTP PD.
And it did.
Beautiful.
All right, so, see, that's how condition works.
Now, let's move to the left and you'll see.
We'll run as we'll unload close to see what are the variables we have available out of the box that
will run ansible local host dash, dash or space setup.
All right.
So this is going to take a few seconds.
When it comes back, you will notice these are all the facts and we could use that facts for our variables
to define for our conditions.
So you see Ansible underscore virtualization.
This will text underscore host ansible underscore virtualization is a type equal virtual machine.
Same thing if you go up.
These are all the variables.
Ansible underscore userspace bits right here.
You'll notice this is ansible underscore real underscore user.
All right.
These are all the variables.
Let's go more up and you'll see right here.
It says Ansible, underscore OS, underscore family is red hat.
This is that variable that I got it from this list.
You could also get the whole bunch of facts that could be used for variables and eventually for our
conditions.
All right.
Spend some time to go through them and you will know exactly how to use them.