WEBVTT

1
00:00.000 --> 00:03.670
So friends, in the previous video we had just configured our Ansible Config

2
00:03.670 --> 00:04.400
uration file

3
00:04.400 --> 00:08.370
as well as our Ansible Inventory. What we now want to do is to visit the

4
00:08.370 --> 00:10.000
concept of variables.

5
00:10.000 --> 00:14.480
So with respect to automation, machines can quite easily invoke repetition.

6
00:14.480 --> 00:19.760
Say for example, we wanted all of our devices to have an NTP server of 8.8.8.8.

7
00:19.760 --> 00:23.920
Now if that were the case for every single device, well, that would be a really

8
00:23.920 --> 00:24.880
easy problem

9
00:24.880 --> 00:28.800
for the automation solution to solve. It would just connect to all of these

10
00:28.800 --> 00:29.600
devices

11
00:29.600 --> 00:33.680
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

11
00:29.600 --> 00:33.680
and just type the exact same commands in the case of Cisco iOS syntax.

12
00:33.680 --> 00:37.890
If I go to one of my devices here, all Ansible would have to do would be to

13
00:37.890 --> 00:38.640
type the command

14
00:38.640 --> 00:44.560
enable and then type the command configr terminal and then NTP server and then

15
00:44.560 --> 00:45.680
the value of the server

16
00:45.680 --> 00:51.010
itself, which in this case here would be 8.8.8.8. These exact commands would

17
00:51.010 --> 00:52.400
have no variation,

18
00:52.400 --> 00:58.810
no matter which device we connected to. But as we know in the real world, this

19
00:58.810 --> 00:59.440
is not

20
00:59.440 --> 01:03.510
quite a reality. Very often, instead, you're going to have different
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

20
00:59.440 --> 01:03.510
quite a reality. Very often, instead, you're going to have different

21
01:03.510 --> 01:05.840
configurations on different devices.

22
01:05.840 --> 01:11.040
Quite simply, there is going to be some type of variation in the actual value

23
01:11.040 --> 01:12.160
that you're passing in.

24
01:12.160 --> 01:15.850
Classical example, let's say we were using automation to configure IP

25
01:15.850 --> 01:16.560
addressing,

26
01:16.560 --> 01:20.100
but the IP addresses on this device are going to be different from the IP

27
01:20.100 --> 01:21.440
address on this one,

28
01:21.440 --> 01:25.360
as well as the IP address of this and this. So the way we solve for this in the

29
01:25.360 --> 01:26.320
world of automation

30
01:26.320 --> 01:31.030
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

30
01:26.320 --> 01:31.030
is to make use of variable values. So let me tell you how we solve it. Let's

31
01:31.030 --> 01:31.680
say once again,

32
01:31.680 --> 01:36.480
to use the same example, we want to effectively push out some NTP configuration

33
01:36.480 --> 01:37.520
. What we want to do

34
01:37.520 --> 01:43.320
here is we want to identify which part of the configurations actually will vary

35
01:43.320 --> 01:43.840
and which parts

36
01:43.840 --> 01:47.430
will be static. Well, in our network, all four devices happen to be Cisco

37
01:47.430 --> 01:48.640
devices. So this part

38
01:48.640 --> 01:53.410
here is going to stay the same regardless of which device. Ansible this part,

39
01:53.410 --> 01:54.800
as well, this

40
01:54.800 --> 02:00.420
particular portion right here, NTP space server, however, the actual value we
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

40
01:54.800 --> 02:00.420
particular portion right here, NTP space server, however, the actual value we

41
02:00.420 --> 02:01.840
populate in here,

42
02:01.840 --> 02:06.310
this can be variable. It might change depending on the device. We're connecting

43
02:06.310 --> 02:06.960
to it might

44
02:06.960 --> 02:10.850
want a different IP address, for example. Whereas the syntax before it, this is

45
02:10.850 --> 02:12.480
always going to stay the same.

46
02:12.480 --> 02:17.740
So let's maybe say one device, we get NTP server 8.8.8.8 and another one was

47
02:17.740 --> 02:20.240
going to get NTP server 2.2.2.2.2,

48
02:20.240 --> 02:25.090
the only part that is varying is this little portion here. So essentially, we

49
02:25.090 --> 02:26.480
want to reference

50
02:26.480 --> 02:30.820
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

50
02:26.480 --> 02:30.820
these values and associate them with our devices. And then we can have Ansible

51
02:30.820 --> 02:32.240
substitute these values

52
02:32.240 --> 02:36.310
in at the correct part of the configuration. Now, just to be clear here, with

53
02:36.310 --> 02:37.520
respect to Ansible,

54
02:37.520 --> 02:42.090
we have the concept of a group variable and we also have the concept of a host

55
02:42.090 --> 02:43.120
variable. Now,

56
02:43.120 --> 02:45.980
this is really, really useful for pushing out configurations, because let's

57
02:45.980 --> 02:47.920
maybe say we want to have,

58
02:47.920 --> 02:52.400
you know, as we specified in our USA group, let's imagine like we said before,

59
02:52.400 --> 02:52.960
they're going to have

60
02:52.960 --> 02:57.820
the NTP server configuration of 1.1.1.1. Whereas in the UK, they're going to

61
02:57.820 --> 02:58.880
have the NTP server

62
02:58.880 --> 03:03.440
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

62
02:58.880 --> 03:03.440
configuration of 2.2.2.2. Here's how we'll solve this. Within our Ansible

63
03:03.440 --> 03:04.400
directory structure,

64
03:04.400 --> 03:08.870
we're going to have particular files named after each group. So we'll have a

65
03:08.870 --> 03:10.160
file in my case here

66
03:10.160 --> 03:16.350
called USA.Yaml, as well as one called UK.Yaml. Okay. Now, all we will do in

67
03:16.350 --> 03:18.800
both of these files is to

68
03:18.800 --> 03:23.250
reference in name of a variable and associate it with a particular value. So

69
03:23.250 --> 03:24.240
what that means is that

70
03:24.240 --> 03:28.880
when Ansible is connecting to the devices in the USA, it's going to use the

71
03:28.880 --> 03:30.400
value in this particular
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

71
03:28.880 --> 03:30.400
value in this particular

72
03:30.400 --> 03:34.160
file for the configuration. Whereas when Ansible is connecting to devices in

73
03:34.160 --> 03:35.600
the UK group, i.e. these

74
03:35.600 --> 03:39.350
two devices, when we go to make that configuration, Ansible simple looks at the

75
03:39.350 --> 03:40.560
value in this particular

76
03:40.560 --> 03:45.050
file and substitutes that in upon configuration. So quite easily, we can now

77
03:45.050 --> 03:46.160
automate out slight

78
03:46.160 --> 03:50.660
differences across these groups. So that devices in the USA can get custom

79
03:50.660 --> 03:52.400
configurations as can

80
03:52.400 --> 03:56.080
devices in the UK and not just that. Let's just say, for example, we want to

81
03:56.080 --> 03:57.200
add in more devices

82
03:57.200 --> 04:01.120
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

82
03:57.200 --> 04:01.120
into the UK. So let's imagine we've got devices 5 and 6 year ago. Now, if we

83
04:01.120 --> 04:02.080
specify that they belong

84
04:02.080 --> 04:08.200
to the UK group, then if we happen to push NTP configurations to these devices,

85
04:08.200 --> 04:08.640
then Ansible is

86
04:08.640 --> 04:12.330
going to give them the values that we specify within that UK.Yaml file. But

87
04:12.330 --> 04:13.360
remember what I just

88
04:13.360 --> 04:20.050
said, we also have host variables as well. Now host variables actually override

89
04:20.050 --> 04:20.880
the group membership

90
04:20.880 --> 04:25.960
variables. So let's say, for example, that we generally want everyone in the UK

91
04:25.960 --> 04:26.960
to have an NTP

92
04:26.960 --> 04:32.420
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

92
04:26.960 --> 04:32.420
server configuration of 2.2.2.2. So we specify that in our UK.Yaml file.

93
04:32.420 --> 04:34.000
However, let's say for whatever

94
04:34.000 --> 04:37.830
reason, we would like S5 to have a different configuration. Let's maybe say

95
04:37.830 --> 04:41.600
that's going to get 2.2.2.2.3

96
04:41.600 --> 04:45.170
for whatever reason. What we would do with the quickly host variable file named

97
04:45.170 --> 04:45.760
after it's let's

98
04:45.760 --> 04:50.630
call this S5.Yaml. And instead of this particular file, we would specify the

99
04:50.630 --> 04:54.160
different value 2.2.2.2.3.

100
04:54.160 --> 04:58.000
So because of this, if we happen to connect the devices in the USA, they can be

101
04:58.000 --> 04:58.560
configured with

102
04:58.560 --> 05:03.730
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

102
04:58.560 --> 05:03.730
1.1.1.1.1. That applies to both of those devices right here. Whereas in the UK,

103
05:03.730 --> 05:04.800
all devices are going

104
05:04.800 --> 05:11.180
to get the configuration of 2.2.2.2 except ones which have the same value redef

105
05:11.180 --> 05:13.280
ined in their host variables,

106
05:13.280 --> 05:16.980
which is what we see in this case here of device number five, which gets the

107
05:16.980 --> 05:17.920
slightly modified

108
05:17.920 --> 05:22.490
value of 2.2.2.2.3. So this might be a little bit confusing at first. So the

109
05:22.490 --> 05:23.520
best thing to do is to

110
05:23.520 --> 05:27.950
write some variables and then we can see things in action. So let's go back to

111
05:27.950 --> 05:29.440
our terminal right here.

112
05:29.440 --> 05:32.730
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

112
05:29.440 --> 05:32.730
And what I'm going to do is I'm going to create a directory, let's just say MK

113
05:32.730 --> 05:34.240
DR to make a new directory.

114
05:34.240 --> 05:38.670
And I'm going to create a directory called groupVars as well as a directory

115
05:38.670 --> 05:40.080
called hostVars. Now this

116
05:40.080 --> 05:44.140
happens to be an Ansible Convention. Ansible will automatically look for these

117
05:44.140 --> 05:45.760
particular directories

118
05:45.760 --> 05:50.560
and seek to suck in the files with themal directories. Now inside, if I go and

119
05:50.560 --> 05:51.440
change into the group

120
05:51.440 --> 05:55.950
Vars directory, what I will do is I will create a file for every single group

121
05:55.950 --> 05:58.160
that I have in my inventory.

122
05:58.160 --> 06:02.370
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

122
05:58.160 --> 06:02.370
So I've got a group called Cisco. So I'll create a file called Cisco.Yaml. I've

123
06:02.370 --> 06:03.200
got a group called

124
06:03.200 --> 06:07.680
USA. So I'll create one USA.Yaml and I've got a group called UK. So I'll have

125
06:07.680 --> 06:09.600
one called UK.Yaml.

126
06:09.600 --> 06:14.680
Now similarly, if I go into the hostVars folder, remember in my inventory, I've

127
06:14.680 --> 06:15.680
got four different

128
06:15.680 --> 06:20.130
hosts. What I will similarly do is create a host file for every single host. I

129
06:20.130 --> 06:22.560
'll say touch S1.Yaml.

130
06:22.560 --> 06:31.600
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

130
06:22.560 --> 06:31.600
Touch S2.Yaml. Touch S3.Yaml and then finally create one for S4 as well.

131
06:31.600 --> 06:35.760
Now as for our variables, because our Cisco devices all have the same

132
06:35.760 --> 06:37.200
configurations with respect to

133
06:37.200 --> 06:41.270
the login details, I'm going to define those values in there. And because of

134
06:41.270 --> 06:42.320
those four devices,

135
06:42.320 --> 06:46.060
membership within the inventory, Ansible will seek to use these connection

136
06:46.060 --> 06:47.360
details when talking to

137
06:47.360 --> 06:51.360
those devices. So what I will do is go and open these up. So let's go into the

138
06:51.360 --> 06:52.400
group Vars folder

139
06:52.400 --> 06:56.880
and I'll go into Cisco.Yaml. Now there are some special variable names that Ans

140
06:56.880 --> 06:57.440
ible will seek to

141
06:57.440 --> 07:01.990
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

141
06:57.440 --> 07:01.990
use. One is known as Ansible_User. This is how we can define our login user

142
07:01.990 --> 07:03.360
name. So for all

143
07:03.360 --> 07:07.220
devices, everyone that belongs to the Cisco group, we have a login user name of

144
07:07.220 --> 07:09.360
Admin. And similarly,

145
07:09.360 --> 07:13.040
we've got a special keyword called Ansible_Password, whereby we specify the

146
07:13.040 --> 07:14.960
password. All four devices

147
07:14.960 --> 07:21.440
all have the same login password of AdminPass. Now specify my Ansible network

148
07:21.440 --> 07:22.320
OS that again,

149
07:22.320 --> 07:25.840
another specialized keyword. This tells Ansible which type of device we're

150
07:25.840 --> 07:26.960
talking to. So in this

151
07:26.960 --> 07:32.300
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

151
07:26.960 --> 07:32.300
case here, if I say Cisco.ioes.ioes, this is Ansible_Speak for basically the

152
07:32.300 --> 07:33.120
same for connecting to an

153
07:33.120 --> 07:36.560
old school Cisco CLI device. And I'll tell Ansible the type of connection that

154
07:36.560 --> 07:38.240
's going to be Ansible.NET

155
07:38.240 --> 07:44.090
commem.network CLI. Pretty much we're going to be using the regular CLI. So

156
07:44.090 --> 07:45.440
these variables will be

157
07:45.440 --> 07:49.560
associated with all devices in the Cisco group, which as we can see are all

158
07:49.560 --> 07:50.720
four of our devices.

159
07:50.720 --> 07:54.960
And for my configuration, let's go with my NTP configuration. Let's open this

160
07:54.960 --> 07:55.920
up. Let's now go

161
07:55.920 --> 07:59.730
into group Vars once again and we'll go into the USA group. So let's just

162
07:59.730 --> 08:01.440
follow the same convention.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

162
07:59.730 --> 08:01.440
follow the same convention.

163
08:01.440 --> 08:05.320
Now in my case here, I can call my variable anything I so wish. It's going to

164
08:05.320 --> 08:06.480
be a custom variable

165
08:06.480 --> 08:12.020
that I have created. So let's just call this NTP_config and all I will do now

166
08:12.020 --> 08:13.760
is I will associate a value. So

167
08:13.760 --> 08:19.310
in the USA, I want everyone to get the configuration of 1.1.1.1.1. So I'll just

168
08:19.310 --> 08:21.280
pop that in there. Whereas

169
08:21.280 --> 08:26.350
if you happen to be in the UK, let's open your, what I want to do is to use the

170
08:26.350 --> 08:28.320
exact same key. So let's

171
08:28.320 --> 08:31.960
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

171
08:28.320 --> 08:31.960
just copy this. So we reference the same thing. I'll paste this in and all I

172
08:31.960 --> 08:32.960
will do is I'll change

173
08:32.960 --> 08:38.200
the corresponding value. So UK1 will get 2.2.2.2 as we can see right here. Now

174
08:38.200 --> 08:39.360
like I say, if I so wish,

175
08:39.360 --> 08:43.190
if I wanted to override the group membership configuration for any of my

176
08:43.190 --> 08:44.640
devices, then what I could do

177
08:44.640 --> 08:49.630
is specify the same key at the host variable level. Let's maybe say S2 should

178
08:49.630 --> 08:50.800
have a very particular

179
08:50.800 --> 08:55.050
configuration what I could do is therefore go in to the host Vars directly this

180
08:55.050 --> 08:56.080
time. So going to

181
08:56.080 --> 09:01.330
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

181
08:56.080 --> 09:01.330
here and going to S2. Once again, I'm going to use the exact same key NTP_con

182
09:01.330 --> 09:02.320
fig and this time we

183
09:02.320 --> 09:06.470
could give it a different configuration entirely. So let's maybe go with 8.8.8.

184
09:06.470 --> 09:07.920
8.8. So quite simply,

185
09:07.920 --> 09:12.470
when Ansible goes to executes, if we attempt to push out configurations related

186
09:12.470 --> 09:12.880
to NTP,

187
09:12.880 --> 09:17.480
well, the bottom two devices should get their values from their UK membership,

188
09:17.480 --> 09:18.480
which I believe is

189
09:18.480 --> 09:23.430
an NTP configuration of 2.2.2.2. And in the case here, we've got two devices in

190
09:23.430 --> 09:24.560
the USA group. So we'll

191
09:24.560 --> 09:30.240
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

191
09:24.560 --> 09:30.240
look into the USA file to get that value. And that's going to be 1.1.1.1.1.

192
09:30.240 --> 09:31.760
However, before Ansible

193
09:31.760 --> 09:36.330
executes, it's going to notice that for this particular device, there is a more

194
09:36.330 --> 09:37.600
specific value

195
09:37.600 --> 09:41.400
in its host variable file and it's not going to use this value, it will

196
09:41.400 --> 09:42.880
therefore use the value of

197
09:42.880 --> 09:48.880
8.8.8.8 for this one device specifically. So now we have our configuration file

198
09:48.880 --> 09:49.840
or inventory as

199
09:49.840 --> 09:53.320
well as our variables configured. Let's now put it all together by pushing

200
09:53.320 --> 09:54.400
configurations to our

201
09:54.400 --> 09:56.400
devices utilizing Ansible.
