WEBVTT

1
00:00.000 --> 00:04.530
Okay friends, so how about we get hands on and begin by first talking about

2
00:04.530 --> 00:05.520
host variables.

3
00:05.520 --> 00:08.730
So no surprises when we're talking about host variables, we're just talking

4
00:08.730 --> 00:09.360
about defining

5
00:09.360 --> 00:14.320
those variable values at the host level. That means the individual hosts within

6
00:14.320 --> 00:15.520
our inventory,

7
00:15.520 --> 00:20.010
we know we happen to have a blue box, as well as a yellow box device in our

8
00:20.010 --> 00:20.960
inventory.

9
00:20.960 --> 00:25.300
So by specifying host variables for yellow box, they will only apply to this

10
00:25.300 --> 00:25.760
device,

11
00:25.760 --> 00:30.200
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

11
00:25.760 --> 00:30.200
and conversely any host variables assigned to blue box will only apply to blue

12
00:30.200 --> 00:30.560
box.

13
00:30.560 --> 00:35.360
That is the whole goal here. Now just to be very clear here, host variables can

14
00:35.360 --> 00:36.320
actually be defined

15
00:36.320 --> 00:40.550
within the answerable inventory themselves, basically within the actual INI

16
00:40.550 --> 00:41.680
file that we use to

17
00:41.680 --> 00:45.720
specify or manage devices, or if you happen to be using the YAML format, the Y

18
00:45.720 --> 00:46.640
AML format of the

19
00:46.640 --> 00:50.130
inventory. Now the key thing to note here is that whilst many years ago this

20
00:50.130 --> 00:51.840
was very, very common,

21
00:51.840 --> 00:56.470
now this is a deprecated practice and it is really not recommended. As we're

22
00:56.470 --> 00:57.520
going to learn,

23
00:57.520 --> 01:02.950
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

23
00:57.520 --> 01:02.950
a much better way to handle this process is to have designated files for these

24
01:02.950 --> 01:03.920
host variables as

25
01:03.920 --> 01:08.560
opposed to bundling them in with the rest of the inventory information. The

26
01:08.560 --> 01:09.360
reason why is that

27
01:09.360 --> 01:13.490
this keeps things less cluttered and much better organized. Now as we're also

28
01:13.490 --> 01:14.320
going to learn,

29
01:14.320 --> 01:17.530
and we'll talk about in the next video, we can also have something known as

30
01:17.530 --> 01:18.640
group variables.

31
01:18.640 --> 01:21.130
You can imagine what these are going to do, it's going to be the same type of

32
01:21.130 --> 01:21.840
thing, it's going to

33
01:21.840 --> 01:26.410
allow us to assign variables but not at a host level but rather the group that

34
01:26.410 --> 01:27.920
the host belongs to.

35
01:27.920 --> 01:31.440
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

35
01:27.920 --> 01:31.440
Now as we're going to learn when we're defining these particular files for

36
01:31.440 --> 01:32.000
hosts,

37
01:32.000 --> 01:36.240
they're going to have to be specified in the YAML format and the exact same is

38
01:36.240 --> 01:37.360
true for our group

39
01:37.360 --> 01:41.420
variables. Quite simply for our host, we're actually going to have a very

40
01:41.420 --> 01:42.800
particular directory

41
01:42.800 --> 01:48.000
known as host_verse. This is a directory that we'll have to create on our own

42
01:48.000 --> 01:48.560
Linux

43
01:48.560 --> 01:52.410
system but by virtue of it being present in the working directory where we

44
01:52.410 --> 01:53.440
happen to be

45
01:53.440 --> 01:57.580
managing Ansible, the Ansible system is going to automatically seek to find

46
01:57.580 --> 01:57.920
this

47
01:57.920 --> 02:01.720
particular directory. So make sure that we do actually have this particular
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

47
01:57.920 --> 02:01.720
particular directory. So make sure that we do actually have this particular

48
02:01.720 --> 02:02.400
name Ansible,

49
02:02.400 --> 02:06.990
it's actually looking for this as a convention. Now inside of this particular

50
02:06.990 --> 02:07.600
directory,

51
02:07.600 --> 02:11.130
we're going to have a whole bunch of YAML files like I said, that's going to be

52
02:11.130 --> 02:11.760
the format.

53
02:11.760 --> 02:16.640
And we're actually going to name each file after the device name within the

54
02:16.640 --> 02:17.520
inventory. So we know

55
02:17.520 --> 02:22.000
we have a device in the inventory called Bluebox, so we'll have a Bluebox.YAML

56
02:22.000 --> 02:23.040
file and we're also

57
02:23.040 --> 02:27.730
going to have a yellowbox.YAML file, both of which, like I said, within the

58
02:27.730 --> 02:29.200
host_verse directory

59
02:29.200 --> 02:33.240
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

59
02:29.200 --> 02:33.240
and Ansible is going to be able to figure out that the information in this

60
02:33.240 --> 02:34.240
particular file

61
02:34.240 --> 02:38.090
should correspond to the device which has the IP address correlating to the

62
02:38.090 --> 02:39.040
yellowbox name

63
02:39.040 --> 02:43.140
and similarly the same thing should happen for Bluebox. Ansible is going to

64
02:43.140 --> 02:44.080
handle all of this

65
02:44.080 --> 02:48.280
really, really handy. So let's go and create this directory structure then. So

66
02:48.280 --> 02:49.120
this here is my current

67
02:49.120 --> 02:52.400
directory, we can see this is where my Ansible inventory is, the configuration

68
02:52.400 --> 02:53.040
file where the

69
02:53.040 --> 02:57.100
playbooks are going to be running from. So within here, I'm just going to say

70
02:57.100 --> 02:59.520
mkdir host_verse.

71
02:59.520 --> 03:04.170
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

71
02:59.520 --> 03:04.170
Now like I said in my inventory, here are the actual devices. So inside of host

72
03:04.170 --> 03:04.960
_verse, what I'm

73
03:04.960 --> 03:10.550
going to do is I'm going to create a file called Bluebox.YAML as well as yellow

74
03:10.550 --> 03:11.840
box.YAML. Perfect

75
03:11.840 --> 03:15.880
if I've done ls, here we are right here. Now we're going to have to populate

76
03:15.880 --> 03:16.880
these particular files

77
03:16.880 --> 03:20.780
with the values that we want. So let's try this and let's go into Bluebox first

78
03:20.780 --> 03:22.960
and I'll go into

79
03:22.960 --> 03:27.210
insert mode and have my three hyphens at the top. And all I'm going to do is

80
03:27.210 --> 03:28.000
just like we talked

81
03:28.000 --> 03:31.740
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

81
03:28.000 --> 03:31.740
about in the previous scale, we have these key value pairs. This is exactly

82
03:31.740 --> 03:33.200
what I'm going to do.

83
03:33.200 --> 03:38.240
So the name of the key that I choose to define here can be anything I so wish,

84
03:38.240 --> 03:41.700
but that is going to be the variable that is referenced within the actual

85
03:41.700 --> 03:42.480
playbook.

86
03:42.480 --> 03:46.420
Let me just call this test var okay. Doesn't actually matter. So long as we put

87
03:46.420 --> 03:46.800
a column,

88
03:46.800 --> 03:51.600
we make it a key. And now within here, all I'm going to do is say bluebox and

89
03:51.600 --> 03:52.080
inside of the

90
03:52.080 --> 03:55.880
brackets, I'm going to give the IP address of the bluebox, which is 134, I

91
03:55.880 --> 03:56.800
believe it ends in.

92
03:56.800 --> 04:00.040
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

92
03:56.800 --> 04:00.040
We just double check that. I'll open up a new terminal, let's just say cat, it

93
04:00.040 --> 04:00.960
's say hosts.

94
04:00.960 --> 04:05.590
Yep, bluebox has that IP address here and we'll also do the same for yellowbox.

95
04:05.590 --> 04:05.840
So let's just

96
04:05.840 --> 04:10.030
write this out. And if we go into yellowbox, the key thing here though, is we

97
04:10.030 --> 04:10.960
want to be using the

98
04:10.960 --> 04:15.280
same key as a variable name. So we'll call this test var once again. The only

99
04:15.280 --> 04:16.160
difference is we're

100
04:16.160 --> 04:19.470
going to have a different value that is referenced. So this thing I will say

101
04:19.470 --> 04:20.560
yellowbox and into the

102
04:20.560 --> 04:25.630
brackets, I'll say 192, 168, 160 dot 135. So basically, if we happen to

103
04:25.630 --> 04:27.200
reference the name

104
04:27.200 --> 04:31.060
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

104
04:27.200 --> 04:31.060
test var, we actually access a key for the yellow box that will render this

105
04:31.060 --> 04:32.960
particular value. Whereas

106
04:32.960 --> 04:36.980
by calling the exact same key, this time for the blue box, the value is going

107
04:36.980 --> 04:38.240
to be rendered as

108
04:38.240 --> 04:42.420
blue box and then the blue box IP address. So let's tag together by creating a

109
04:42.420 --> 04:43.520
little playbook here.

110
04:43.520 --> 04:47.750
Let's go back a directory, turn LS and let's just create a playbook just called

111
04:47.750 --> 04:48.320
, let's just say

112
04:48.320 --> 04:51.390
var play call anything. So which one's a game? So we're going to insert more

113
04:51.390 --> 04:52.000
tavern, three

114
04:52.000 --> 04:56.500
hyphens at the top and I'll just give this play a name, just say playbook for

115
04:56.500 --> 04:58.080
testing hosts first.

116
04:58.080 --> 05:00.860
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

116
04:58.080 --> 05:00.860
The host I'm going to target is going to be the all group, which means both

117
05:00.860 --> 05:02.160
devices are going to be

118
05:02.160 --> 05:06.080
targeted here. I will have my tasks here defined. This is going to be an array

119
05:06.080 --> 05:06.880
object. So we start

120
05:06.880 --> 05:12.740
with her hyphen and then the name of this task, just say rights info to a file.

121
05:12.740 --> 05:13.280
Now, all I'm going to

122
05:13.280 --> 05:16.930
do here is I'm going to use the answerable built in shell module. So what I

123
05:16.930 --> 05:17.760
will do here is I will

124
05:17.760 --> 05:22.110
say echo. So I could say something like say hello there and redirect that into

125
05:22.110 --> 05:22.880
a file. Let's

126
05:22.880 --> 05:26.950
maybe say in the 10th directory, and we'll just call the file var test dot txt.

127
05:26.950 --> 05:27.760
The problem here

128
05:27.760 --> 05:31.850
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

128
05:27.760 --> 05:31.850
is that for every device for targeting that is the all group, both boxes are

129
05:31.850 --> 05:33.120
going to get the exact

130
05:33.120 --> 05:37.050
same content here. Now, what if I wanted to have different content written into

131
05:37.050 --> 05:37.520
the file

132
05:37.520 --> 05:41.660
dependent on the actual device for targeting the way we do this, as you guessed

133
05:41.660 --> 05:42.960
it, we use a variable

134
05:42.960 --> 05:46.830
value. So what I can do here is as opposed to hard coding some type of value,

135
05:46.830 --> 05:47.600
what I'm going to do

136
05:47.600 --> 05:52.100
is use my double curly braces. Okay, now inside of here, what I'm going to do

137
05:52.100 --> 05:53.360
is just give the name

138
05:53.360 --> 05:57.610
of the actual variable. And the name itself is going to be test var. So if I

139
05:57.610 --> 05:58.720
just say test var

140
05:58.720 --> 06:03.880
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

140
05:58.720 --> 06:03.880
right here, when we execute for blue box, we call in the variable within the

141
06:03.880 --> 06:05.600
blue box host var file,

142
06:05.600 --> 06:10.410
which is test var rendering this value. And when we're executing for yellow box

143
06:10.410 --> 06:11.600
, we'll look into

144
06:11.600 --> 06:16.400
host yellow box dot yml, grab the test var variable, and we render this

145
06:16.400 --> 06:18.160
particular value a different

146
06:18.160 --> 06:22.010
value entirely. So let's see if actually it works then. So I'll press writing

147
06:22.010 --> 06:22.880
quits, and all I will

148
06:22.880 --> 06:27.980
say is answerable playbook var play and to enter, and we can see our task right

149
06:27.980 --> 06:28.640
info to a file,

150
06:28.640 --> 06:32.270
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

150
06:28.640 --> 06:32.270
we have two changes, both of which happen to be successful. So let's go to the

151
06:32.270 --> 06:33.200
device here.

152
06:33.200 --> 06:37.040
So we're on blue box, let's go into the temp directory, do an LS. Here we can

153
06:37.040 --> 06:38.160
see the vertex

154
06:38.160 --> 06:41.830
file. If I happen to cat vertex, look at the actual information we've written

155
06:41.830 --> 06:42.240
in here,

156
06:42.240 --> 06:48.400
blue box, and then the IP address. However, if I go to my yellow box here, cd

157
06:48.400 --> 06:48.800
into temp,

158
06:48.800 --> 06:52.720
do an LS, there is a fail var test. Let's cat that fail and look at this right

159
06:52.720 --> 06:53.280
here, we have a

160
06:53.280 --> 06:57.040
completely different value that has been populated using answerable. So now

161
06:57.040 --> 06:58.160
that we've seen how to

162
06:58.160 --> 07:02.080
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

162
06:58.160 --> 07:02.080
use host variables, let's dig our teeth into the creation of group variables.
