So friends, in the previous video we had just configured our Ansible Config
uration file
as well as our Ansible Inventory. What we now want to do is to visit the
concept of variables.
So with respect to automation, machines can quite easily invoke repetition.
Say for example, we wanted all of our devices to have an NTP server of 8.8.8.8.
Now if that were the case for every single device, well, that would be a really
easy problem
for the automation solution to solve. It would just connect to all of these
devices
and just type the exact same commands in the case of Cisco iOS syntax. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and just type the exact same commands in the case of Cisco iOS syntax.
If I go to one of my devices here, all Ansible would have to do would be to
type the command
enable and then type the command configr terminal and then NTP server and then
the value of the server
itself, which in this case here would be 8.8.8.8. These exact commands would
have no variation,
no matter which device we connected to. But as we know in the real world, this
is not
quite a reality. Very often, instead, you're going to have different WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
quite a reality. Very often, instead, you're going to have different
configurations on different devices.
Quite simply, there is going to be some type of variation in the actual value
that you're passing in.
Classical example, let's say we were using automation to configure IP
addressing,
but the IP addresses on this device are going to be different from the IP
address on this one,
as well as the IP address of this and this. So the way we solve for this in the
world of automation
is to make use of variable values. So let me tell you how we solve it. Let's WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
is to make use of variable values. So let me tell you how we solve it. Let's
say once again,
to use the same example, we want to effectively push out some NTP configuration
. What we want to do
here is we want to identify which part of the configurations actually will vary
and which parts
will be static. Well, in our network, all four devices happen to be Cisco
devices. So this part
here is going to stay the same regardless of which device. Ansible this part,
as well, this
particular portion right here, NTP space server, however, the actual value we WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
particular portion right here, NTP space server, however, the actual value we
populate in here,
this can be variable. It might change depending on the device. We're connecting
to it might
want a different IP address, for example. Whereas the syntax before it, this is
always going to stay the same.
So let's maybe say one device, we get NTP server 8.8.8.8 and another one was
going to get NTP server 2.2.2.2.2,
the only part that is varying is this little portion here. So essentially, we
want to reference
these values and associate them with our devices. And then we can have Ansible WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
these values and associate them with our devices. And then we can have Ansible
substitute these values
in at the correct part of the configuration. Now, just to be clear here, with
respect to Ansible,
we have the concept of a group variable and we also have the concept of a host
variable. Now,
this is really, really useful for pushing out configurations, because let's
maybe say we want to have,
you know, as we specified in our USA group, let's imagine like we said before,
they're going to have
the NTP server configuration of 1.1.1.1. Whereas in the UK, they're going to
have the NTP server
configuration of 2.2.2.2. Here's how we'll solve this. Within our Ansible WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
configuration of 2.2.2.2. Here's how we'll solve this. Within our Ansible
directory structure,
we're going to have particular files named after each group. So we'll have a
file in my case here
called USA.Yaml, as well as one called UK.Yaml. Okay. Now, all we will do in
both of these files is to
reference in name of a variable and associate it with a particular value. So
what that means is that
when Ansible is connecting to the devices in the USA, it's going to use the
value in this particular WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
value in this particular
file for the configuration. Whereas when Ansible is connecting to devices in
the UK group, i.e. these
two devices, when we go to make that configuration, Ansible simple looks at the
value in this particular
file and substitutes that in upon configuration. So quite easily, we can now
automate out slight
differences across these groups. So that devices in the USA can get custom
configurations as can
devices in the UK and not just that. Let's just say, for example, we want to
add in more devices
into the UK. So let's imagine we've got devices 5 and 6 year ago. Now, if we WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
into the UK. So let's imagine we've got devices 5 and 6 year ago. Now, if we
specify that they belong
to the UK group, then if we happen to push NTP configurations to these devices,
then Ansible is
going to give them the values that we specify within that UK.Yaml file. But
remember what I just
said, we also have host variables as well. Now host variables actually override
the group membership
variables. So let's say, for example, that we generally want everyone in the UK
to have an NTP
server configuration of 2.2.2.2. So we specify that in our UK.Yaml file. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
server configuration of 2.2.2.2. So we specify that in our UK.Yaml file.
However, let's say for whatever
reason, we would like S5 to have a different configuration. Let's maybe say
that's going to get 2.2.2.2.3
for whatever reason. What we would do with the quickly host variable file named
after it's let's
call this S5.Yaml. And instead of this particular file, we would specify the
different value 2.2.2.2.3.
So because of this, if we happen to connect the devices in the USA, they can be
configured with
1.1.1.1.1. That applies to both of those devices right here. Whereas in the UK, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
1.1.1.1.1. That applies to both of those devices right here. Whereas in the UK,
all devices are going
to get the configuration of 2.2.2.2 except ones which have the same value redef
ined in their host variables,
which is what we see in this case here of device number five, which gets the
slightly modified
value of 2.2.2.2.3. So this might be a little bit confusing at first. So the
best thing to do is to
write some variables and then we can see things in action. So let's go back to
our terminal right here.
And what I'm going to do is I'm going to create a directory, let's just say MK WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And what I'm going to do is I'm going to create a directory, let's just say MK
DR to make a new directory.
And I'm going to create a directory called groupVars as well as a directory
called hostVars. Now this
happens to be an Ansible Convention. Ansible will automatically look for these
particular directories
and seek to suck in the files with themal directories. Now inside, if I go and
change into the group
Vars directory, what I will do is I will create a file for every single group
that I have in my inventory.
So I've got a group called Cisco. So I'll create a file called Cisco.Yaml. I've WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So I've got a group called Cisco. So I'll create a file called Cisco.Yaml. I've
got a group called
USA. So I'll create one USA.Yaml and I've got a group called UK. So I'll have
one called UK.Yaml.
Now similarly, if I go into the hostVars folder, remember in my inventory, I've
got four different
hosts. What I will similarly do is create a host file for every single host. I
'll say touch S1.Yaml.
Touch S2.Yaml. Touch S3.Yaml and then finally create one for S4 as well. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Touch S2.Yaml. Touch S3.Yaml and then finally create one for S4 as well.
Now as for our variables, because our Cisco devices all have the same
configurations with respect to
the login details, I'm going to define those values in there. And because of
those four devices,
membership within the inventory, Ansible will seek to use these connection
details when talking to
those devices. So what I will do is go and open these up. So let's go into the
group Vars folder
and I'll go into Cisco.Yaml. Now there are some special variable names that Ans
ible will seek to
use. One is known as Ansible_User. This is how we can define our login user WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
use. One is known as Ansible_User. This is how we can define our login user
name. So for all
devices, everyone that belongs to the Cisco group, we have a login user name of
Admin. And similarly,
we've got a special keyword called Ansible_Password, whereby we specify the
password. All four devices
all have the same login password of AdminPass. Now specify my Ansible network
OS that again,
another specialized keyword. This tells Ansible which type of device we're
talking to. So in this
case here, if I say Cisco.ioes.ioes, this is Ansible_Speak for basically the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
case here, if I say Cisco.ioes.ioes, this is Ansible_Speak for basically the
same for connecting to an
old school Cisco CLI device. And I'll tell Ansible the type of connection that
's going to be Ansible.NET
commem.network CLI. Pretty much we're going to be using the regular CLI. So
these variables will be
associated with all devices in the Cisco group, which as we can see are all
four of our devices.
And for my configuration, let's go with my NTP configuration. Let's open this
up. Let's now go
into group Vars once again and we'll go into the USA group. So let's just
follow the same convention. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
follow the same convention.
Now in my case here, I can call my variable anything I so wish. It's going to
be a custom variable
that I have created. So let's just call this NTP_config and all I will do now
is I will associate a value. So
in the USA, I want everyone to get the configuration of 1.1.1.1.1. So I'll just
pop that in there. Whereas
if you happen to be in the UK, let's open your, what I want to do is to use the
exact same key. So let's
just copy this. So we reference the same thing. I'll paste this in and all I WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
just copy this. So we reference the same thing. I'll paste this in and all I
will do is I'll change
the corresponding value. So UK1 will get 2.2.2.2 as we can see right here. Now
like I say, if I so wish,
if I wanted to override the group membership configuration for any of my
devices, then what I could do
is specify the same key at the host variable level. Let's maybe say S2 should
have a very particular
configuration what I could do is therefore go in to the host Vars directly this
time. So going to
here and going to S2. Once again, I'm going to use the exact same key NTP_con WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
here and going to S2. Once again, I'm going to use the exact same key NTP_con
fig and this time we
could give it a different configuration entirely. So let's maybe go with 8.8.8.
8.8. So quite simply,
when Ansible goes to executes, if we attempt to push out configurations related
to NTP,
well, the bottom two devices should get their values from their UK membership,
which I believe is
an NTP configuration of 2.2.2.2. And in the case here, we've got two devices in
the USA group. So we'll
look into the USA file to get that value. And that's going to be 1.1.1.1.1. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
look into the USA file to get that value. And that's going to be 1.1.1.1.1.
However, before Ansible
executes, it's going to notice that for this particular device, there is a more
specific value
in its host variable file and it's not going to use this value, it will
therefore use the value of
8.8.8.8 for this one device specifically. So now we have our configuration file
or inventory as
well as our variables configured. Let's now put it all together by pushing
configurations to our
devices utilizing Ansible.