Okay friends, so let's continue on looking at how we can actually utilise lips
with an
answerable.
This time though, we will look at some slightly more complex lips and we will
explore some
simple examples whereby we use lips to make changes on other managed devices as
opposed
to simply just printing out text on our screen.
So with that said, let's get to it and just dive on in.
So let's imagine this, okay.
What I will do is I will create a new playbook.
I'll just call this loop2.yml because as you know, I'm always very, very
creative. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
creative.
And what I will do is I'll give this playbook a name.
I'm just going to say testing some nested data because right now, what I'm
going to do is
simply print out more information like before, but I'm going to access the
information utilising
lips when the data is indeed nested.
Once we have that under a belt, we'll actually begin applying it to the devices
themselves.
So right now, I will just have this target my local host because I'm just
printing
it out.
So what I will do is I'll just define some var within the playbook.
So I'll have a key called trainers for the CBT Nuggets trainers and I'll WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So I'll have a key called trainers for the CBT Nuggets trainers and I'll
specify the
name.
So let's use the name key and we'll specify James for the first name and then
we'll have
a key called subjects and this will define some of the subjects that this
particular trainer
happens to teach.
So we can say Microsoft, James also teaches Linux as well as cybersecurity.
Now out then to within at the same level of indentation, we'll specify another
name.
We'll have a trainer called Keith.
The subjects that Keith is going to teach is going to be networking and virtual
isation
and maybe I'll add myself in.
I'll say John subjects I teach Linux automation and I can also tell you a lot WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
I'll say John subjects I teach Linux automation and I can also tell you a lot
about pizza.
True thing I'm actually an expert.
Okay, so what I'm going to do now is I'm going to create my tasks.
So at the same level of indentation as the variables here, I'm going to out
dent and choose
tasks right here.
I'll give the task a name and the name will just simply be printing out some
nested data.
So we're going to use the debug module and the message is going to be my name
is and
then we're going to happen to have the name of the trainer and then we'll say WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
then we're going to happen to have the name of the trainer and then we'll say
and I teach
and then we'll also have our subjects.
So here is the thing we actually want to look through the trainers.
Okay, that will give us access to this object upon first iteration and then the
next time
we look around, we get the next elements in the array, which is this right here
and I've
just noticed that I misspelled virtualization.
Let me just temporarily correct that.
So the next iteration, we get access to this element here and then once that's
done, we
get access to the third and final element of the array. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
get access to the third and final element of the array.
Now, the cool thing is, or rather the interesting thing is that we're going to
have to parse
out different pieces of information for each iteration.
So the example here would be the first time we look around when we see, hi, my
name is,
we actually just want to access this very particular piece of information,
which is
going to be defined via the name key.
In with respect to the subjects, similarly, we want to access just the subject
key and
get the whole list of subjects that the trainer happens to teach. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
get the whole list of subjects that the trainer happens to teach.
So the way we're actually going to access this information is we're first going
to specify
what we're going to look through.
So very important at the same level of indentation as the debug module, I'm
going to specify
loop and what is it do I actually want to loop through?
Well, I want to loop through as we just discussed, the trainers key right here.
So within my double parentheses, I will specify trainers.
So that's going to give me access to each object.
Now, if I just happen to type out item and then item again, we're going to have WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now, if I just happen to type out item and then item again, we're going to have
a bit
of a problem.
Let me just save this and show you what happens.
So if I say Ansible Playbook, loop two dot y, I'm going to enter.
So here is what we actually see here.
Now, this might be a little bit hard to look at because we have so much
information, but
let's just look at the first block here.
Okay, the first bit is outputted.
What it says here is high, my name is, and as opposed to just saying James, it
gives us
absolutely everything.
You see that it gives us the entire object included in the color braces, we see
the name
key as well as James, as well as the subjects, Microsoft, Linux, cybersecurity, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
key as well as James, as well as the subjects, Microsoft, Linux, cybersecurity,
and then it
says, and I teach name James, subjects, Microsoft, Linux, cybersecurity.
So what we want to do here is we also want to be able to access the individual
keys as
we look around.
So the way we do this, quite simple.
So the important thing is we're still going to reference that very crucial item
keyword.
The difference is we're just going to use dot notation as we discussed in an
earlier
scale to access the elements within.
So if we just say item and then say dot name to access the name and if we want WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So if we just say item and then say dot name to access the name and if we want
to access
the subjects, we say item, which gives us access to the entire element here.
And by specifying subjects, we only parse out this piece of the data as opposed
, including
the name key and corresponding value.
Now what I will do here is I'll say item dot subjects to call that key.
And if I now save this to escape wq to write and quit, let's clear the screen,
arrow up
and to enter.
Okay.
So now we get a response, we see the message is much shorter.
So we can see here, if we look at this one, it says, hi, my name is James and I WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So we can see here, if we look at this one, it says, hi, my name is James and I
teach
Microsoft, Linux, and cybersecurity.
That sounds good.
Next iteration, it says, hi, my name is Keith and I teach networking and
virtualization.
And finally, it says, hi, my name is John and I teach Linux automation and
pizza, of
course.
So as we can see here, this is very, very similar to what we talked about when
we discussed
parsing out data within YAML.
The key distinction here is that we have to be very careful about what we're
actually
choosing to loop through.
In this case here, the trainer's key.
And remember that we're still going to reference the item keyword. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And remember that we're still going to reference the item keyword.
But when we want to get more specific pieces of data from each iteration, we
can still
use with item keyword that dot notation.
Okay.
So we'll go to the screen and what I will do is I will create a new playbook
just called
loop three dot YML.
So let's go into insert mode, have my three hyphens at the top and we'll just
give this
playbook a name and let's just say, creating some directories with loops.
Okay.
So what are we going to target?
We're going to target the all group, which will be my yellow box and blue box
machines WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
machines
that I'm managing and the tasks I'm going to create, let's just say tasks and
then I'll
have a name for the task or just a creating a directory.
What we want to do right now is we want to use the Ansible built in file module
.
Now, if we recall from a previous scale that is going to have one very
particular required
parameter and that is the path parameter as per the documentation.
And now within my quotation marks, I specify the file path that I want to use
here.
So let's maybe say I want to go into the temp directory and I wanted to create
a file WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
a file
called, oh, I don't know, john one.
Okay.
We could do such a thing.
Okay.
Now what I would do is I specify the state is going to be a directory because
that's
where it is.
It's not going to be a file.
Now, as it transpires at the moment, the way this is set up is all we're going
to do
is we're going to target both of our machines via the all keyword, i.e. the all
group.
And for both machines inside of the temp directory, we're going to have a
directory called john
one.
What we want to do though is we want to create multiple different directories
also, for example, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
also, for example,
within the temp directory.
So let's think about it.
If it's inside of the temp directory for each directory that we want to create,
we can
just leave this as a test as a hard coded value, because that is not going to
change.
The thing that is going to change is going to be the name of the directory
itself.
This is the part that's going to have the variation, i.e. this is the part that
's going
to be the variable value.
So the first thing we're going to do here is we're going to swap out the hard
coded
configuration.
So let's just remove this and I'll have my double color braces. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So let's just remove this and I'll have my double color braces.
Now what I'm going to have to do is I'm going to have to specify what it is I
want to look
through.
Now at the same level of indentation as the file module, and the same way we
talked about
it as the same level of indentation via the debug module, what I'm going to do
is to
use that loop keyword once again.
Now we're going to have multiple different things that we're going to look
through, multiple
different items that is.
So how are we going to define this?
We're going to define it as an array.
So we'll have our hyphen and the first directory is going to be john one. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So we'll have our hyphen and the first directory is going to be john one.
The next directory is going to be john two and the third directory is going to
be a pizza
three because pizza is awesome.
Oh, that is going to be the plan.
Now just to be clear here, we're not creating a nested directory with all of
these elements.
For that, we would not need a look.
We could just do it in one fell swoop.
We could just say temp forward slash john one forward slash john two forward
slash pizza
three, that type of thing.
Okay.
What we are looking to do is to create three distinct directories temp john one
and then
temp john two and then temp pizza three. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
temp john two and then temp pizza three.
So these directories will all be at the same level as each other.
So think about it, if we use the item keyword, what does that actually give us?
It gives us access to each element that we happen to be looking through.
So the first element is john one.
The next one is john two.
The third is pizza three.
Do we have to use documentation here?
Absolutely not because the data is not nested by simply referencing item that
will give us
the exact data that we want to populate.
So let's go down here.
I'll press escape colon wq to write and quit. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
I'll press escape colon wq to write and quit.
Let's clear the screen.
Let's say answerable playbook, lip three dot yml and to enter and we'll begins.
And look at this right here, we have a change on the yellow box as well as a
blue box.
And we can see here in the summary, the item on the first iteration was john
one for both
devices and then the next iteration, the item happened to be john two as it
says for
both devices.
And then of course, we can see the item is pizza three for the two devices blue
box in
yellow box.
So if we go to these devices, if we just log in, type in the password, let's WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So if we go to these devices, if we just log in, type in the password, let's
open up
our terminal.
If I go into the temp directory, doing LS, look at what we actually see here.
We have the john one directory john two and pizza three.
So that means I can go into john one to a pwd.
There we have it.
If I see the out, I'm going to john two pwd.
There's the path.
And again, I can go into pizza three pwd.
There's a path or pizza three over on the blue box.
If we see the into the temp directory, do a listing as we can see here, similar
way.
We also have john one john two and pizza three.
So I'm just CD john one. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So I'm just CD john one.
There we go.
I can see the out and going to john two and CD back out and go into pizza three
.
So that's how we can access nested data within our lips, as well as how we can
actually use
lips to affect changes on our managed devices.