WEBVTT

1
00:00.000 --> 00:04.000
So friends, now that we have an idea of how to create our host variables,

2
00:04.000 --> 00:08.000
let's now dive in to the intricacies of creating our group variables.

3
00:08.000 --> 00:11.440
Now we already mentioned that with respect to the host files, we would have to

4
00:11.440 --> 00:14.000
create a directory called host_vars

5
00:14.000 --> 00:19.070
and for the creation of group variables, we will have to specify a group_vars

6
00:19.070 --> 00:20.000
directory.

7
00:20.000 --> 00:25.030
Now inside of this directory, we're once again going to create files with the .

8
00:25.030 --> 00:26.000
YML extension

9
00:26.000 --> 00:30.310
and the name of the files, this time as opposed to correlating to the name of
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

9
00:26.000 --> 00:30.310
and the name of the files, this time as opposed to correlating to the name of

10
00:30.310 --> 00:32.000
the individual host in the inventory,

11
00:32.000 --> 00:36.000
we're going to correlate to all of the groups within the inventory.

12
00:36.000 --> 00:39.790
Now right now, the only group we have is the all group, so we're going to have

13
00:39.790 --> 00:41.000
a file called all.yaml,

14
00:41.000 --> 00:45.000
but if in the inventory, perhaps you may be creating a USA group.

15
00:45.000 --> 00:49.980
Once again, inside of the group_vars directory, you could create a USA.YML file

16
00:49.980 --> 00:51.000
as well.

17
00:51.000 --> 00:55.000
So in the inventory, if we go in, like I say, we could create this USA

18
00:55.000 --> 00:57.000
and maybe see the blue box in the USA.

19
00:57.000 --> 01:00.560
By virtue of this being present here within the square brackets in the INI
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

19
00:57.000 --> 01:00.560
By virtue of this being present here within the square brackets in the INI

20
01:00.560 --> 01:01.000
format,

21
01:01.000 --> 01:07.000
Ansible will look for a USA.YML file within the group_vars directory.

22
01:07.000 --> 01:08.000
This is how this is going to operate.

23
01:08.000 --> 01:11.810
So for now, what I will do is I will just delete this because we don't actually

24
01:11.810 --> 01:12.000
need this right now.

25
01:12.000 --> 01:14.000
Let's just leave the all group.

26
01:14.000 --> 01:17.030
And the first thing I really want to highlight here about the group variables

27
01:17.030 --> 01:20.000
is that we have to understand the order of priority here.

28
01:20.000 --> 01:23.680
Now by default, Ansible is going to look for an all group that's going to

29
01:23.680 --> 01:27.000
comprise of all of the hosts in the inventory.

30
01:27.000 --> 01:29.000
We have defined this in our inventory already.

31
01:29.000 --> 01:34.240
And this particular group with this naming convention all is at the lowest
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

31
01:29.000 --> 01:34.240
And this particular group with this naming convention all is at the lowest

32
01:34.240 --> 01:35.000
priority.

33
01:35.000 --> 01:39.060
Basically, because it is the widest group, it includes all of the devices

34
01:39.060 --> 01:40.000
within the inventory.

35
01:40.000 --> 01:43.000
This is the least specific group.

36
01:43.000 --> 01:46.260
To help you understand this concept, let's actually consult the Ansible

37
01:46.260 --> 01:47.000
documentation.

38
01:47.000 --> 01:50.640
If you just happened to search for organizing host and group_vars, let's go to

39
01:50.640 --> 01:52.000
this particular link,

40
01:52.000 --> 01:53.000
how to build your inventory.

41
01:53.000 --> 01:57.000
And if we just scroll on down, check this part right here.

42
01:57.000 --> 02:00.000
We're talking about how variables happen to be merged.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

43
02:00.000 --> 02:04.000
Now right now, this is going to be the order of operation.

44
02:04.000 --> 02:06.480
Pretty much we're looking at the lowest priority all the way to the highest

45
02:06.480 --> 02:07.000
priority.

46
02:07.000 --> 02:12.760
The way to remember this is that the more specific a variable is the higher the

47
02:12.760 --> 02:14.000
priority is.

48
02:14.000 --> 02:18.510
What do I mean by say that we'll think about it in some cases, you may have

49
02:18.510 --> 02:20.000
variables defined

50
02:20.000 --> 02:22.000
at different levels within the inventory.

51
02:22.000 --> 02:27.000
So let's maybe say in the all.yaml file, you happen to have the test for key.

52
02:27.000 --> 02:29.000
Well, who's that going to apply to?

53
02:29.000 --> 02:32.520
It's going to apply to all of the devices in the inventory because that is what
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

53
02:29.000 --> 02:32.520
It's going to apply to all of the devices in the inventory because that is what

54
02:32.520 --> 02:33.000
the all group is.

55
02:33.000 --> 02:38.600
Whereas if you happen to define the same variable, test for in this case, in a

56
02:38.600 --> 02:40.000
general parent group,

57
02:40.000 --> 02:45.380
this will actually override the value for all devices in that group as opposed

58
02:45.380 --> 02:46.000
to those devices

59
02:46.000 --> 02:49.000
getting that value as defined in the all.yaml file.

60
02:49.000 --> 02:53.580
Similarly, moving on down, if we happen to have a child group of a particular

61
02:53.580 --> 02:54.000
parent,

62
02:54.000 --> 02:59.530
if the same variable is defined there, once again, test for any members of the

63
02:59.530 --> 03:00.000
child group
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

64
03:00.000 --> 03:04.630
is going to get that particular variable value overriding what was defined in

65
03:04.630 --> 03:05.000
the parent.

66
03:05.000 --> 03:09.000
As I say, as we get more and more specific, we have a higher priority.

67
03:09.000 --> 03:13.340
And at the highest level of priority, if there is a clash between any of the

68
03:13.340 --> 03:14.000
group variables

69
03:14.000 --> 03:18.180
and a particular host variable, if that variable is defined at the host level,

70
03:18.180 --> 03:19.000
once again,

71
03:19.000 --> 03:24.000
such as test for the device will take the value as defined in its host variable

72
03:24.000 --> 03:24.000
file

73
03:24.000 --> 03:26.000
because it is the most specific.

74
03:26.000 --> 03:29.710
Now right now, because we have a very, very simple inventory, we just have one

75
03:29.710 --> 03:31.000
group called all.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

75
03:29.710 --> 03:31.000
group called all.

76
03:31.000 --> 03:34.360
Let's look at the answerable documentation to see information whereby we have a

77
03:34.360 --> 03:36.000
more detailed inventory.

78
03:36.000 --> 03:37.000
So let's just scroll up here.

79
03:37.000 --> 03:41.000
Here we can see an inventory in yaml format, which is absolutely valid.

80
03:41.000 --> 03:45.000
And above, we can also see the same inventory as specified in i and i.

81
03:45.000 --> 03:51.140
So let's imagine for now, within our all.yaml file, we simply had a variable

82
03:51.140 --> 03:55.000
simply defined as DNS_server, okay?

83
03:55.000 --> 03:58.000
And the value of that was 1.1.1.1.

84
03:58.000 --> 04:02.370
Now, virtue of this being in the all group, this means if we happen to
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

84
03:58.000 --> 04:02.370
Now, virtue of this being in the all group, this means if we happen to

85
04:02.370 --> 04:04.000
reference the double-color braces

86
04:04.000 --> 04:09.370
with the DNS server variable, every device in the inventory is going to get the

87
04:09.370 --> 04:10.000
value 1.1.1

88
04:10.000 --> 04:12.000
when it comes to a DNS server.

89
04:12.000 --> 04:16.280
However, some devices here say, for example, in the Atlanta group, this is

90
04:16.280 --> 04:19.000
going to comprise of host 1 and host 2.

91
04:19.000 --> 04:24.000
Let's say in the Atlanta.yaml file, we have the exact same variable to find,

92
04:24.000 --> 04:26.000
DNS_server,

93
04:26.000 --> 04:30.000
but the value here is 2.2.2.2.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

94
04:30.000 --> 04:34.480
That means the devices host 1 and host 2, when it comes to render their

95
04:34.480 --> 04:35.000
variable value,

96
04:35.000 --> 04:39.200
they are not going to get all the ones, they are going to get all the tools,

97
04:39.200 --> 04:43.000
and all the other devices will still get 1.1.1.1.1.

98
04:43.000 --> 04:48.000
Now, let's maybe say that host 2, however, within the host var directory,

99
04:48.000 --> 04:51.000
inside of the host 2.yaml file,

100
04:51.000 --> 04:57.630
if that actually has the same key defined once again, DNS_server, with the

101
04:57.630 --> 05:00.000
value 3.3.3.3.3,
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

102
05:00.000 --> 05:05.440
this particular host is going to get the 3.3.3 value because its host variable

103
05:05.440 --> 05:08.000
value will override its group membership.

104
05:08.000 --> 05:11.000
Now, one last thing to say, in fact, let's just look at this in the yaml format

105
05:11.000 --> 05:14.000
because it is actually easier to look at in terms of visually,

106
05:14.000 --> 05:17.000
when I'm talking about a parent group versus a child group.

107
05:17.000 --> 05:21.480
As we can see here, we have a USA group, an example here, and we have a child

108
05:21.480 --> 05:23.000
group called Southeast,

109
05:23.000 --> 05:26.000
as well as Northeast, Northwest, and Southwest.

110
05:26.000 --> 05:29.780
Basically, if there's a clash between the files in Southeast versus the USA,

111
05:29.780 --> 05:31.000
Southeast is a child,
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

111
05:29.780 --> 05:31.000
Southeast is a child,

112
05:31.000 --> 05:35.870
so the Southeast.yaml file, the value in that particular file is going to

113
05:35.870 --> 05:38.000
override the USA.yaml file.

114
05:38.000 --> 05:42.040
Going further again, if the same variable is defined in, say, for example,

115
05:42.040 --> 05:44.000
rally.yaml,

116
05:44.000 --> 05:48.560
we can see rally is indeed a child of Southeast, so rally would override

117
05:48.560 --> 05:50.000
Southeast as well.

118
05:50.000 --> 05:54.250
Therefore, the host belonging to this particular group, in this case here, host

119
05:54.250 --> 05:55.000
to and host 3,

120
05:55.000 --> 06:00.530
they would get the value from rally.yaml unless, like I say, they happen to
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

120
05:55.000 --> 06:00.530
they would get the value from rally.yaml unless, like I say, they happen to

121
06:00.530 --> 06:03.000
have it defined personally in their own host variable file.

122
06:03.000 --> 06:05.000
That is the order of operation.

123
06:05.000 --> 06:09.720
Now, we can see here, in this case here, we have Atlanta and rally, and they're

124
06:09.720 --> 06:12.000
both at the same indentation level.

125
06:12.000 --> 06:13.000
One is not a child of the other.

126
06:13.000 --> 06:17.090
Now, in this case here, we actually don't have any hosts that belong to both of

127
06:17.090 --> 06:18.000
these groups,

128
06:18.000 --> 06:22.550
but you can conceptualize a world, in which case a device belongs to multiple

129
06:22.550 --> 06:24.000
groups at the same indentation level.

130
06:24.000 --> 06:29.330
The way Ansible is going to resolve this behavior is it's going to effectively

131
06:29.330 --> 06:32.000
flatten and merge these files,
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

131
06:29.330 --> 06:32.000
flatten and merge these files,

132
06:32.000 --> 06:36.210
and it's going to do this on the basis of ASCII order with the last group

133
06:36.210 --> 06:37.000
winning.

134
06:37.000 --> 06:40.440
So, let's imagine we have a group called a group, and another group called a

135
06:40.440 --> 06:41.000
group.

136
06:41.000 --> 06:45.310
They are at the same level of indentation, and in the group-vars directory, we

137
06:45.310 --> 06:47.000
're going to have an a-group.yaml file,

138
06:47.000 --> 06:49.000
as well as a b-group.yaml file.

139
06:49.000 --> 06:53.270
If we have a clash, which one is going to win, well, because the b-group will

140
06:53.270 --> 06:56.000
be processed last, because of alphabetical order,

141
06:56.000 --> 06:59.000
the b-group here is actually going to take priority.

142
06:59.000 --> 07:02.000
That is how we're going to resolve any of these potential clashes.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

142
06:59.000 --> 07:02.000
That is how we're going to resolve any of these potential clashes.

143
07:02.000 --> 07:05.000
But for now, let's get hands on and begin creating some group variables.

144
07:05.000 --> 07:09.390
So, what we're going to do, I'm going to create this directory called group_v

145
07:09.390 --> 07:10.000
ars,

146
07:10.000 --> 07:13.670
let's just cdin, and all I'm going to do is create a file named after every

147
07:13.670 --> 07:15.000
group in our inventory,

148
07:15.000 --> 07:19.000
which in this case here is only the all group, so we have all dot.yaml.

149
07:19.000 --> 07:20.000
So, let's go in here.

150
07:20.000 --> 07:24.240
Let's have a variable called dnsserver, and I will just give this the address

151
07:24.240 --> 07:26.000
of 8.8.8.8,

152
07:26.000 --> 07:30.670
and if I go back and I just copy my var playbook, and I'll just call this var
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

152
07:26.000 --> 07:30.670
and if I go back and I just copy my var playbook, and I'll just call this var

153
07:30.670 --> 07:32.000
play2.yaml,

154
07:32.000 --> 07:36.160
and if we go in and edit this particular playbook, all I'm going to do is I'm

155
07:36.160 --> 07:37.000
going to echo in

156
07:37.000 --> 07:42.000
the dnsserver variable, and I'll write it into a file called dnstest.ok.

157
07:42.000 --> 07:46.700
So, right now, if I run this particular playbook, and to enter, we can see we

158
07:46.700 --> 07:47.000
have a change

159
07:47.000 --> 07:49.000
on both of our devices, let's go and test.

160
07:49.000 --> 07:53.000
So, we're in the 10th directory, if I do an LS, we can see we have this dnstest

161
07:53.000 --> 07:53.000
file,

162
07:53.000 --> 07:58.830
if I cat open the file, and to enter, we can see the value for blue box is 8.8.

163
07:58.830 --> 07:59.000
8.

164
07:59.000 --> 08:02.750
It got that value because of its membership to the all group, and thus took the
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

164
07:59.000 --> 08:02.750
It got that value because of its membership to the all group, and thus took the

165
08:02.750 --> 08:03.000
value directly

166
08:03.000 --> 08:06.000
from the all dot.yaml file when we called the variable.

167
08:06.000 --> 08:09.000
Similarly, no surprises when we go to the yellow box.

168
08:09.000 --> 08:12.000
As we can see, the file is also here, let's just cat open.

169
08:12.000 --> 08:15.880
Once again, we get the exact same value, now the interest and thing, like I say

170
08:15.880 --> 08:17.000
, if we happen to have a clash,

171
08:17.000 --> 08:21.000
and let's go into the host var directory, say for example, and let's maybe go

172
08:21.000 --> 08:22.000
into the yellow box.

173
08:22.000 --> 08:26.000
Okay. So, what I'm going to do is, inside of here, I'm going to have the exact

174
08:26.000 --> 08:28.000
same key dns_server,

175
08:28.000 --> 08:30.000
but I'm going to give it a completely different value.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

176
08:30.000 --> 08:32.000
Let's go with 2.2.2.2.2. Okay.

177
08:32.000 --> 08:36.700
So, let's write this out. We can see when it comes to yellow box, we have 2.2.2

178
08:36.700 --> 08:39.000
.2, but it also has membership to the all group,

179
08:39.000 --> 08:43.000
which specifies the variable should be 8.8.8.8.

180
08:43.000 --> 08:46.500
As you should suspect, when it comes to this clash, which key is going to be

181
08:46.500 --> 08:48.000
called for that particular device,

182
08:48.000 --> 08:52.000
the more specific value is going to win, thus we should actually see.

183
08:52.000 --> 08:55.000
For the yellow box, the value should change to 2.2.2.2.2.

184
08:55.000 --> 08:58.000
So, let's go and check that out, then let's go back, clear the stream.

185
08:58.000 --> 09:02.000
We will rerun the same playbook, this time with the updated var files.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

185
08:58.000 --> 09:02.000
We will rerun the same playbook, this time with the updated var files.

186
09:02.000 --> 09:04.000
As we can see, we have written the change.

187
09:04.000 --> 09:09.000
We go to the blue box, cat dns_test, no surprises there, it still gets 8.8.8.

188
09:09.000 --> 09:14.390
However, when we go over to the yellow box and we cat this file again, notice

189
09:14.390 --> 09:17.000
it now gets 2.2.2.2.2.

190
09:17.000 --> 09:22.170
This here is an example of the inheritance model prioritizing the variable with

191
09:22.170 --> 09:24.000
the more specific value.
