Okay friends, so how about we get hands on and begin by first talking about
host variables.
So no surprises when we're talking about host variables, we're just talking
about defining
those variable values at the host level. That means the individual hosts within
our inventory,
we know we happen to have a blue box, as well as a yellow box device in our
inventory.
So by specifying host variables for yellow box, they will only apply to this
device,
and conversely any host variables assigned to blue box will only apply to blue WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and conversely any host variables assigned to blue box will only apply to blue
box.
That is the whole goal here. Now just to be very clear here, host variables can
actually be defined
within the answerable inventory themselves, basically within the actual INI
file that we use to
specify or manage devices, or if you happen to be using the YAML format, the Y
AML format of the
inventory. Now the key thing to note here is that whilst many years ago this
was very, very common,
now this is a deprecated practice and it is really not recommended. As we're
going to learn,
a much better way to handle this process is to have designated files for these WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
a much better way to handle this process is to have designated files for these
host variables as
opposed to bundling them in with the rest of the inventory information. The
reason why is that
this keeps things less cluttered and much better organized. Now as we're also
going to learn,
and we'll talk about in the next video, we can also have something known as
group variables.
You can imagine what these are going to do, it's going to be the same type of
thing, it's going to
allow us to assign variables but not at a host level but rather the group that
the host belongs to.
Now as we're going to learn when we're defining these particular files for WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now as we're going to learn when we're defining these particular files for
hosts,
they're going to have to be specified in the YAML format and the exact same is
true for our group
variables. Quite simply for our host, we're actually going to have a very
particular directory
known as host_verse. This is a directory that we'll have to create on our own
Linux
system but by virtue of it being present in the working directory where we
happen to be
managing Ansible, the Ansible system is going to automatically seek to find
this
particular directory. So make sure that we do actually have this particular WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
particular directory. So make sure that we do actually have this particular
name Ansible,
it's actually looking for this as a convention. Now inside of this particular
directory,
we're going to have a whole bunch of YAML files like I said, that's going to be
the format.
And we're actually going to name each file after the device name within the
inventory. So we know
we have a device in the inventory called Bluebox, so we'll have a Bluebox.YAML
file and we're also
going to have a yellowbox.YAML file, both of which, like I said, within the
host_verse directory
and Ansible is going to be able to figure out that the information in this WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and Ansible is going to be able to figure out that the information in this
particular file
should correspond to the device which has the IP address correlating to the
yellowbox name
and similarly the same thing should happen for Bluebox. Ansible is going to
handle all of this
really, really handy. So let's go and create this directory structure then. So
this here is my current
directory, we can see this is where my Ansible inventory is, the configuration
file where the
playbooks are going to be running from. So within here, I'm just going to say
mkdir host_verse.
Now like I said in my inventory, here are the actual devices. So inside of host WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now like I said in my inventory, here are the actual devices. So inside of host
_verse, what I'm
going to do is I'm going to create a file called Bluebox.YAML as well as yellow
box.YAML. Perfect
if I've done ls, here we are right here. Now we're going to have to populate
these particular files
with the values that we want. So let's try this and let's go into Bluebox first
and I'll go into
insert mode and have my three hyphens at the top. And all I'm going to do is
just like we talked
about in the previous scale, we have these key value pairs. This is exactly WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
about in the previous scale, we have these key value pairs. This is exactly
what I'm going to do.
So the name of the key that I choose to define here can be anything I so wish,
but that is going to be the variable that is referenced within the actual
playbook.
Let me just call this test var okay. Doesn't actually matter. So long as we put
a column,
we make it a key. And now within here, all I'm going to do is say bluebox and
inside of the
brackets, I'm going to give the IP address of the bluebox, which is 134, I
believe it ends in.
We just double check that. I'll open up a new terminal, let's just say cat, it WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
We just double check that. I'll open up a new terminal, let's just say cat, it
's say hosts.
Yep, bluebox has that IP address here and we'll also do the same for yellowbox.
So let's just
write this out. And if we go into yellowbox, the key thing here though, is we
want to be using the
same key as a variable name. So we'll call this test var once again. The only
difference is we're
going to have a different value that is referenced. So this thing I will say
yellowbox and into the
brackets, I'll say 192, 168, 160 dot 135. So basically, if we happen to
reference the name
test var, we actually access a key for the yellow box that will render this WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
test var, we actually access a key for the yellow box that will render this
particular value. Whereas
by calling the exact same key, this time for the blue box, the value is going
to be rendered as
blue box and then the blue box IP address. So let's tag together by creating a
little playbook here.
Let's go back a directory, turn LS and let's just create a playbook just called
, let's just say
var play call anything. So which one's a game? So we're going to insert more
tavern, three
hyphens at the top and I'll just give this play a name, just say playbook for
testing hosts first.
The host I'm going to target is going to be the all group, which means both WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
The host I'm going to target is going to be the all group, which means both
devices are going to be
targeted here. I will have my tasks here defined. This is going to be an array
object. So we start
with her hyphen and then the name of this task, just say rights info to a file.
Now, all I'm going to
do here is I'm going to use the answerable built in shell module. So what I
will do here is I will
say echo. So I could say something like say hello there and redirect that into
a file. Let's
maybe say in the 10th directory, and we'll just call the file var test dot txt.
The problem here
is that for every device for targeting that is the all group, both boxes are WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
is that for every device for targeting that is the all group, both boxes are
going to get the exact
same content here. Now, what if I wanted to have different content written into
the file
dependent on the actual device for targeting the way we do this, as you guessed
it, we use a variable
value. So what I can do here is as opposed to hard coding some type of value,
what I'm going to do
is use my double curly braces. Okay, now inside of here, what I'm going to do
is just give the name
of the actual variable. And the name itself is going to be test var. So if I
just say test var
right here, when we execute for blue box, we call in the variable within the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
right here, when we execute for blue box, we call in the variable within the
blue box host var file,
which is test var rendering this value. And when we're executing for yellow box
, we'll look into
host yellow box dot yml, grab the test var variable, and we render this
particular value a different
value entirely. So let's see if actually it works then. So I'll press writing
quits, and all I will
say is answerable playbook var play and to enter, and we can see our task right
info to a file,
we have two changes, both of which happen to be successful. So let's go to the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
we have two changes, both of which happen to be successful. So let's go to the
device here.
So we're on blue box, let's go into the temp directory, do an LS. Here we can
see the vertex
file. If I happen to cat vertex, look at the actual information we've written
in here,
blue box, and then the IP address. However, if I go to my yellow box here, cd
into temp,
do an LS, there is a fail var test. Let's cat that fail and look at this right
here, we have a
completely different value that has been populated using answerable. So now
that we've seen how to
use host variables, let's dig our teeth into the creation of group variables. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
use host variables, let's dig our teeth into the creation of group variables.