So friends, throughout this skill we have discussed two different programming
concepts.
The first one was loops and the second one was conditional statements.
Now we don't have to understand one in isolation or implement one in isolation
of the other.
We can in fact use loops with conditional statements.
So in this video, that is what we're going to do.
So let me create a new file once again, let's call this loop condition.YML.
Again, I'm super creative as you know, it's going to insert mode, have our free
high
buttons at the top and let's give this playbook a name. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
buttons at the top and let's give this playbook a name.
Let's just say play to test loops with conditions.
Perfect. So the host we are going to target is going to be the old group and
all I will do
is define my tasks. So let's have my task keyword followed by my array object
and then
we'll specify the name in this task. So the name here is going to be creating
multiple files
on UK devices. Now a game, we only have one device, but we can imagine the UK
group is a
lot larger than it actually is. Let's say we are not just managing two devices,
but rather WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
but rather
500 devices in our network, makes it more fun that way after all.
And the module we're going to use is going to be the Ansible built in shell
module.
So all I want to do here is to use the touch command.
And what I want to do is I want to create a file called, let's maybe say UK one
UK two
and UK three if you happen to be in the UK group.
So I don't want to have to type UK one dot txt here because that would hard
code the value.
That would mean we would have to continually retype more tasks defining the
shell module
so on so forth, definitely not efficient. So what I will do is I will say touch WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
so on so forth, definitely not efficient. So what I will do is I will say touch
and then I'll have
my double color braces here. Now what am I going to actually have within the
parentheses? Well,
we're going to be looping through some particular details. So let's go out
there with them.
Use the loop keywords and we'll specify as an array all of the elements you
want to loop through.
So let's just say UK one dot txt. The next one is UK two dot txt and the third
one can be UK three
dot txt. So if you want to access each element within the loop, how do we do
that? Well, we use
the item keyboard as we saw before. Okay, let's create a new task then we'll WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
the item keyboard as we saw before. Okay, let's create a new task then we'll
create a name.
I must specify the name as creates multiple files on USA devices. So it really
similar to before,
we'll use the Ansible built in shell module and what we're going to do similar
to as we saw before,
we're going to use the touch command to create brand new files. But once again,
we're going to be using a variable and we're going to be leveraging the power
of lips. So we'll
use the keyboard item use a lip keyword again, specifying array this time,
though, the values
are going to be USA one dot txt, USA two dot txt. And let's maybe add another WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
are going to be USA one dot txt, USA two dot txt. And let's maybe add another
one just called
America dot txt doesn't actually matter. Now, of course, what's going to happen
is right now,
this is going to run for all devices, we do not want that. We want to add a
conditional with the
loop element. So at the same level of indentation as the loop, what I'm going
to do is I'm going to
invoke the when keyword here. So I'll just say when and have my colon to make a
key. And as we saw
before, I'm going to have my double quotation marks. And inside of that, I will WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
before, I'm going to have my double quotation marks. And inside of that, I will
have my single
quotation marks to specify what it is I'm targeting. I'm going to specify the
UK group once again
in lowercase. Let's not make that same mistake again. And then I will say in
group underscore
names calling that magic variable once again. And the same thing is going to be
true with a slight
modification for the next task, we'll use the when keyword, have a colon,
double quotation marks,
and our single quotation marks within this time will specify USA in the magic
variable group
underscore names. So with that now done, what I can do is press escape, writing WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
underscore names. So with that now done, what I can do is press escape, writing
quit to save the
file. Let's hit enter. And if I just say answer will have in playbook loop
condition dot yml
and to enter answer, I'm like security. And we can see the outputs are let's
scroll up a little bit.
So as we can see here in the output for task one, create multiple files on the
UK device,
we can see here for the yellow box, which is a USA device. This particular task
has been skipped.
You see that however, when it comes to the blue box, it does actually specify WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
You see that however, when it comes to the blue box, it does actually specify
that this has changed
and it's changed for the item UK one dot txt UK two dot txt as well as UK three
dot txt.
Once again, though, at the next task, the inverse is true. When you want to
create multiple files
on USA devices, the blue box has been skipped for all three items USA one USA
two and America
dot txt, whereas for the yellow box, which does belong to the USA group for
this particular task,
we have actually changed the device again for the item USA one USA two and WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
we have actually changed the device again for the item USA one USA two and
America dot txt.
So let's go to our respective devices starting with the yellow box. Now, be
aware, because I
didn't specify a full absolute path file has been written into the home
directory of the user.
Now right now, I am the nugget user, as we can see here, really whom I command.
However,
because Ansible is executing as the user Ansible, it's going to be in that
particular directory.
Now, if I go into the home directory here, if I try to access Ansible, notice
the permission is
denied. So right now, all I will say is Sue Ansible for substitute user. If I WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
denied. So right now, all I will say is Sue Ansible for substitute user. If I
insert, I'll type in
the answerable password, which is just the word Ansible. Now I am the answer
able user. If I do
an LS, we should be able to go into the answerable directory. And if I do an LS
here, as we can see
here, we do have these particular files, let's do the same thing on the blue
box. So once again,
on the blue box, I am the nugget user, let's substitute user to Ansible, type
in the password
of Ansible. If I now say who am I, I am Ansible. If I go to cd home and enter
the Ansible directory
to an LS, as we can see here, we actually have the UK files UK one UK two and WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
to an LS, as we can see here, we actually have the UK files UK one UK two and
UK three. So now that
we've seen how to use loops as well as conditional statements, as well as loops
with conditional
statements, let's now validate what we've learned within this scale.