So friends, previously we learned how we could create our host variables as
well as group variables.
Now the actual data we populated within these files was really quite simple.
What if the data was a lot more complex, i.e. what if it was nested data?
How do we actually handle that? Well, let's find out and get to it.
So let's say for example going to the host first directory and what I will do
is I will go into
blue box. Inside of here I'm going to add in more data. So let's maybe go with
a key called,
or I don't know, let's just say Linux, okay. And then we'll specify the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
or I don't know, let's just say Linux, okay. And then we'll specify the
operating system
and we just say red hat enterprise Linux. Then we'll specify the version which
in my case here
is going to be version 9.2 and let's just create another key called random
because I'm running out
of ideas. Let's just say domain name is www.bluebox.com whatever, doesn't
actually matter and we'll just
say the nickname is simply blue box. Okay, so a little bit of nested data we
saw when we wanted
to retrieve information relating to the test file right here. All we did was
create parentheses and WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
create parentheses and
pop this particular value in. Now, what if we wanted to access maybe say this
value here.
Let's try this out. Let's just create a little playbook here called nested.YML
create three
hyphens, give it a name of parse nested data. The host I'm going to target in
this case here is
just simply going to be the blue box and the tasks. Now the task here I'm going
to give a name and
the name is going to be print out some var and for care. Now the module I'm
going to use here is
going to be the debug module. This is a module we can use to basically print WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
going to be the debug module. This is a module we can use to basically print
out messages. So I'll
just say the message I want to print out here is going to parse out this
particular detail here,
the version key. Now notice if I just call the key right here, just say I am
running rail version
and then I do my double curly braces and I call the version key. Watch what
happens when I run
this. So I'll say write and quit. So Ansible playbook nested. Look at this
output right here.
We have a fatal result and it says here this task includes an option with an
undefined variable. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
undefined variable.
The error was version is undefined. So we can't just go into the variable file
and just call the key
here. The reason being is because this key is actually nested within another
key. Notice we have
this Linux key first, which is actually the parent to version. So pretty much
what I have to do is
I have to walk the path starting from the outermost level into the information
that I actually want.
So notice that the outermost level here is indeed a Linux. Okay. So that is the
key we're going to
call first. So if I just remove version and replace it with Linux and I just WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
call first. So if I just remove version and replace it with Linux and I just
write and save and to
enter, let's read run the playbook. Okay. So we didn't actually get an error.
But if you notice
what we actually are printing out, it isn't the information that we really want
. It says you
the message is I am running rail version. And what we actually have here is
basically the structured
data that is nested within that particular key. You see that we have our square
brackets, then we
have operating system and then the corresponding value right at enterprise
Linux. Then we have
the version key followed up with the actual value 9.2. This here is the value WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
the version key followed up with the actual value 9.2. This here is the value
we want to get. So
pretty much we want to use something known as dot notation to actually access
this data program
automatically. So realistically, we're going to specify the Linux key within
our curly braces.
But to get the information within the version key, I'm going to say dot and
then give the next key,
which in this case here is version that should actually just return this data
here as opposed to WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
returning all of the data contained within the Linux key. So let's check it out
. Go back in,
make a slight adjustment, go into insert mode and we'll just change this to
Linux dot version.
If I write in quits and I read on the playbook, boom, check it out. I am
running rail version
and then we actually parsed out the version information 9.2. Similarly, if I
wanted to parse
out something different, something even more nested, such as, oh, I don't know,
maybe the domain name
here, what I'm going to do is to parse out the Linux key, which gives us access
to all this data. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
to all this data.
Then I can parse out the random key, which gives us access to all of this data
and then parse out
the domain name key, which gives us precisely this piece of data. So if I just
go into the playbook,
once again, sort of parse out the rail version, while also adding the domain
name information,
which say my domain is double curly braces once again, and we'll just call
Linux random domain
underscore name. Okay, so let's go and try this now. We run the playbook hit
enter. Look at this
right here. The resultant message is a rail version 9.2. My domain is www dot WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
right here. The resultant message is a rail version 9.2. My domain is www dot
blue box dot com.
Everything has been parsed correctly, even parsing things at different levels
of indentation.
Now, let me just briefly make a modification one more time. So I'll go into
blue box once again,
and let's maybe add on a users key. Okay. Now, because we have multiple users,
I'm going to have an array here, which is going to be denoted via a hyphen to
the first user is
going to be nugget user. And the second user, well, that's going to be the
answerable user account WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
answerable user account
we've already created. But for fun, let's just add in some more information,
let's just say John
and Simone. How are we actually going to access, say, for example, the John
data right here,
we know we should reference the Linux outer key and then inside of that, the
user's key,
but there isn't corresponding keys for each individual entry here, because we
're dealing with
an array. So let's actually see this in action. Now, I have a user called an N
my double curly braces,
and I'll say Linux users on my system. Now, let me just briefly save this for WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and I'll say Linux users on my system. Now, let me just briefly save this for
now, run it again.
So we can see here what we've parsed out is effectively an array object, says I
have a user
called and then we see the entire list of users nugget user and sable john sam
ona on my system.
But let's say we want to just get out john. Now, the crucial point here is that
we do not access
each value via a key. Instead, in an array, we access it via its index position
. Now, the
position is going to start from position number zero. So in this case here,
this will be position
zero and then we just move on up. So nugget user is zero. Ansible is one john WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
zero and then we just move on up. So nugget user is zero. Ansible is one john
is two,
samona is three and we have no more elements within the array. So if I want to
target john,
that is going to be index position two, therefore in my answerable playbook,
what I'm going to do
is after I call the Linux and then the user's key, this gives me access to my
array object and
then within square brackets, I'm going to specify the actual position within
the array I want to
access. So in this case here, I'm going to access position two. If I just save
this,
clear the screen, we run the playbook and look at the result that says here, I WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
clear the screen, we run the playbook and look at the result that says here, I
have a user called
john on my system, we were able to actually parse out that very particular
piece of information.
And if we just make the modification, change it to let's maybe say it less
position zero,
what that should now return is the first element within the array, which if we
're under playbook,
as we can see here, now passes out the value nugget user. So that's how we can
effectively
build out complex data within our variable files and access them via the
corresponding key
or in the case of an array via the corresponding index position.