WEBVTT

1
00:00.000 --> 00:03.280
So friends, the very next thing that I would like to talk to you about is the

2
00:03.280 --> 00:04.480
ability for us to

3
00:04.480 --> 00:08.640
register variables. So what exactly am I talking about then? Well let's say we

4
00:08.640 --> 00:09.920
use Ansible to push

5
00:09.920 --> 00:14.030
out some changes to earn managed devices. Perhaps maybe it's some type of

6
00:14.030 --> 00:15.920
configuration change, okay?

7
00:15.920 --> 00:20.810
Now how do we typically verify that the information is indeed correct? Well,

8
00:20.810 --> 00:21.840
one thing we may do is

9
00:21.840 --> 00:26.480
you may log in to each device via SSH and check the configurations manually.

10
00:26.480 --> 00:27.440
That would be the kind

11
00:27.440 --> 00:31.820
of old-scale outdated way to do things. What we could also do is use Ansible to
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

11
00:27.440 --> 00:31.820
of old-scale outdated way to do things. What we could also do is use Ansible to

12
00:31.820 --> 00:32.800
perhaps maybe

13
00:32.800 --> 00:36.570
cat a particular configuration file. That way we can see the actual results,

14
00:36.570 --> 00:37.600
that type of thing.

15
00:37.600 --> 00:42.880
But here is the thing via the use of the register commands, what we're actually

16
00:42.880 --> 00:43.760
able to do is to

17
00:43.760 --> 00:48.640
capture the output of a particular task and then we can perhaps reuse or

18
00:48.640 --> 00:49.920
reference that output.

19
00:49.920 --> 00:53.600
This might sound a little bit abstract. So how about we go to the Ansible

20
00:53.600 --> 00:54.720
documentation, have a

21
00:54.720 --> 00:59.360
look at what the actual module does and then run some examples. So let's go and

22
00:59.360 --> 00:59.920
do that.

23
00:59.920 --> 01:04.380
So if we go to Google and just search for Ansible Register, let's go to this
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

23
00:59.920 --> 01:04.380
So if we go to Google and just search for Ansible Register, let's go to this

24
01:04.380 --> 01:05.280
one right here using

25
01:05.280 --> 01:09.230
variables. Now look what it actually says here, it says you can create

26
01:09.230 --> 01:10.880
variables during a playbook

27
01:10.880 --> 01:15.980
run by registering the return value or values of a task as a new variable

28
01:15.980 --> 01:17.760
pretty much. Like I say,

29
01:17.760 --> 01:21.840
when we happen to run a particular task, we can then capture that output as a

30
01:21.840 --> 01:23.120
variable and then

31
01:23.120 --> 01:26.580
reference it later on within the same playbook. So let's scroll on down to

32
01:26.580 --> 01:27.520
actually see a little

33
01:27.520 --> 01:31.610
bit better. So if we go down to this option here registering variables, let's
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

33
01:27.520 --> 01:31.610
bit better. So if we go down to this option here registering variables, let's

34
01:31.610 --> 01:32.560
click this. Notice

35
01:32.560 --> 01:35.970
what it says here, you can create variables from the output of an Ansible task

36
01:35.970 --> 01:37.600
with the task keyword

37
01:37.600 --> 01:41.120
register. We'll take this fictional example right here directly from the

38
01:41.120 --> 01:42.080
documentation.

39
01:42.080 --> 01:45.600
What we're seeing here is that we have two different tasks. Now the first task

40
01:45.600 --> 01:46.560
is going to run a shell

41
01:46.560 --> 01:50.460
command and it's going to save the output as a variable using register. The

42
01:50.460 --> 01:51.440
actual command we

43
01:51.440 --> 01:55.220
are using here is obviously a fictional command that says user bin foo, which

44
01:55.220 --> 01:55.920
is not actually a

45
01:55.920 --> 01:59.530
command, but this can represent any type of command. Maybe it's the echo

46
01:59.530 --> 02:01.200
command, maybe it's the
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

46
01:59.530 --> 02:01.200
command, maybe it's the

47
02:01.200 --> 02:04.480
nkder command. It doesn't actually matter. But what we're doing here is taking

48
02:04.480 --> 02:05.680
the result of whatever

49
02:05.680 --> 02:10.330
this command happens to do, and we are saving it as a full result. And the way

50
02:10.330 --> 02:11.520
we save that like I

51
02:11.520 --> 02:16.260
say is utilizing this register keyword that is going to grab the output of this

52
02:16.260 --> 02:17.440
particular command

53
02:17.440 --> 02:21.130
and we can just reference it using whatever variable we assign right here. Now

54
02:21.130 --> 02:23.360
when it comes to the

55
02:23.360 --> 02:27.480
next task right here, we're actually making use of that captured output. You

56
02:27.480 --> 02:28.480
see this down here,

57
02:28.480 --> 02:32.190
we have our conditional statements. And the condition is taking that full
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

57
02:28.480 --> 02:32.190
we have our conditional statements. And the condition is taking that full

58
02:32.190 --> 02:33.520
result. And in this case,

59
02:33.520 --> 02:37.880
here we have this dot RC attribute to determine a particular value. And all we

60
02:37.880 --> 02:39.760
're doing is evaluating

61
02:39.760 --> 02:43.950
that the result of this command is the value five. Now if that is the case,

62
02:43.950 --> 02:45.040
then we want to run

63
02:45.040 --> 02:48.290
another command that again, this is another fictional command as per the

64
02:48.290 --> 02:49.040
documentation,

65
02:49.040 --> 02:53.200
it says user bin bar. Like I say, this is not a real world example, it's just

66
02:53.200 --> 02:53.680
to give you a

67
02:53.680 --> 02:57.300
conceptual overview. So basically at its simplest level, we run this particular

68
02:57.300 --> 02:58.320
command, we save

69
02:58.320 --> 03:02.790
the output of that particular command, we then evaluate what that command
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

69
02:58.320 --> 03:02.790
the output of that particular command, we then evaluate what that command

70
03:02.790 --> 03:04.000
actually is. And if

71
03:04.000 --> 03:07.990
it happens to be the number five, then we want to run another command

72
03:07.990 --> 03:09.760
contingent upon whatever

73
03:09.760 --> 03:14.090
this particular result was. So how about we dive in to an example and actually

74
03:14.090 --> 03:15.280
see how this really

75
03:15.280 --> 03:20.550
operates. Okay, so let's create a new playbook called play five dot yml. And we

76
03:20.550 --> 03:21.360
're going to insert

77
03:21.360 --> 03:24.670
multiple hover three hyphens at the top, we'll give the playbook a name, and we

78
03:24.670 --> 03:25.600
'll just say playbook

79
03:25.600 --> 03:29.910
five testing register. Now who are we going to target? We're going to target

80
03:29.910 --> 03:30.720
the all group.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

80
03:29.910 --> 03:30.720
the all group.

81
03:30.720 --> 03:34.320
And just for fun, not that it's necessary, but now that we know how to do it,

82
03:34.320 --> 03:34.960
let's just set

83
03:34.960 --> 03:38.310
gather facts to fall so that the playbook runs a little bit faster. Now what I

84
03:38.310 --> 03:38.960
will do is I'll

85
03:38.960 --> 03:44.110
define my tasks. And the task here is going to have a name, and we'll just say

86
03:44.110 --> 03:46.560
get the date. And I

87
03:46.560 --> 03:50.290
will use the answerable built in command module. And the command that I'm going

88
03:50.290 --> 03:51.120
to run is simply

89
03:51.120 --> 03:54.220
the date command. So you know the way that when you can type on the command

90
03:54.220 --> 03:55.280
line date, get a result

91
03:55.280 --> 03:58.730
right here, that is why I want answerable to execute and all my managed devices

92
03:58.730 --> 04:00.000
. Okay, here's the thing,
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

93
04:00.000 --> 04:04.620
if I just happened to run this right now, as it stands, if I say answerable

94
04:04.620 --> 04:06.240
playbook play five dot

95
04:06.240 --> 04:10.470
yml and to enter, answerable executes, and actually does run that particular

96
04:10.470 --> 04:12.240
command. But the problem

97
04:12.240 --> 04:16.640
is we're not actually seeing the outputs. So the command was actually executed

98
04:16.640 --> 04:17.360
on both of those

99
04:17.360 --> 04:21.840
devices. We just didn't capture the outputs. So here's the thing, what I want

100
04:21.840 --> 04:22.640
to do is I want to

101
04:22.640 --> 04:26.980
create another task that is going to print the actual output. But how do I

102
04:26.980 --> 04:28.080
capture the output

103
04:28.080 --> 04:32.160
of this command? Well, you guessed it, we're going to use that register option.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

103
04:28.080 --> 04:32.160
of this command? Well, you guessed it, we're going to use that register option.

104
04:32.160 --> 04:32.640
So all I will do is

105
04:32.640 --> 04:36.270
also register. And now I will just create a variable name. Now you can call

106
04:36.270 --> 04:37.760
this anything use a wish

107
04:37.760 --> 04:41.620
so long as you reference it. So let me just call this, let's just say date

108
04:41.620 --> 04:42.960
output. So now I will

109
04:42.960 --> 04:47.610
create a new task. And I'll just say printing the results of the command. So

110
04:47.610 --> 04:48.800
what I'm going to do

111
04:48.800 --> 04:53.110
here is use the debug module. And again, I can use the message parameter and

112
04:53.110 --> 04:54.000
use my double curly

113
04:54.000 --> 04:58.200
places if I so wish or if I want I can just call the var parameter directly. So

114
04:58.200 --> 04:59.280
in this case here

115
04:59.280 --> 05:03.450
for variety, I will just use the var parameter. And I will just say dates
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

115
04:59.280 --> 05:03.450
for variety, I will just use the var parameter. And I will just say dates

116
05:03.450 --> 05:04.880
underscore outputs.

117
05:04.880 --> 05:09.050
So check this out. We run the command dates. Whatever is returned from that

118
05:09.050 --> 05:10.240
command, we capture

119
05:10.240 --> 05:14.550
it in store as this particular variable. Now we can print this particular

120
05:14.550 --> 05:16.400
variable using the debug

121
05:16.400 --> 05:21.270
module. So check it out. If I clear the screen, rerun the playbook, hit enter,

122
05:21.270 --> 05:22.000
I had checked this

123
05:22.000 --> 05:25.750
out. We're now actually getting output back. Now if I happen to scroll on up a

124
05:25.750 --> 05:26.720
little bit,

125
05:26.720 --> 05:29.780
you actually see here we're getting a whole bunch of other information that is

126
05:29.780 --> 05:30.800
not actually
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

126
05:29.780 --> 05:30.800
not actually

127
05:30.800 --> 05:34.550
relevant to what we want to see things like Ansible facts, whether something

128
05:34.550 --> 05:35.280
was changed.

129
05:35.280 --> 05:39.060
Perhaps if the command failed that type of thing, we are not concerned with

130
05:39.060 --> 05:40.240
that. What we just want

131
05:40.240 --> 05:45.170
to see is the output of the command. Now notice here, if we happen to look at

132
05:45.170 --> 05:47.200
the STD outs key,

133
05:47.200 --> 05:51.880
this is actually going to give us the actual standard output of the command. So

134
05:51.880 --> 05:52.560
just by calling

135
05:52.560 --> 05:56.220
this particular key, we can use don't notation, that will actually return the

136
05:56.220 --> 05:57.200
output of the command.

137
05:57.200 --> 06:00.570
So let's go back into the playbook here. And as opposed to just calling the
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

137
05:57.200 --> 06:00.570
So let's go back into the playbook here. And as opposed to just calling the

138
06:00.570 --> 06:01.520
date output,

139
06:01.520 --> 06:06.080
I will say dot STD out just to filter for that particular piece of information.

140
06:06.080 --> 06:06.720
So now if I write

141
06:06.720 --> 06:10.550
and quit rerun the playbook, hit enter, check this out right here. Look at that

142
06:10.550 --> 06:11.680
. We've now just got

143
06:11.680 --> 06:16.560
the result of the command that we happen to issue on the devices. And again,

144
06:16.560 --> 06:17.680
the actual name itself

145
06:17.680 --> 06:20.810
doesn't actually matter. You can call this anything you so wish. I can call

146
06:20.810 --> 06:22.320
this date output blast,

147
06:22.320 --> 06:26.900
long as I also reference it as my variable here. So I can just change this once

148
06:26.900 --> 06:27.360
again.

149
06:27.360 --> 06:32.080
If we rerun notice we have the exact same effect. Similar left, we go back in
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

149
06:27.360 --> 06:32.080
If we rerun notice we have the exact same effect. Similar left, we go back in

150
06:32.080 --> 06:33.280
and modify the playbook.

151
06:33.280 --> 06:36.760
Let's maybe say be around the command. Oh, I don't know. Let's go with LS dash

152
06:36.760 --> 06:37.760
L and I'll just change

153
06:37.760 --> 06:42.190
just to getting the dirt contents. If I just run this on its own and to enter

154
06:42.190 --> 06:43.280
the command actually

155
06:43.280 --> 06:47.030
executes, we just don't see the result of that command. So how do we fix it?

156
06:47.030 --> 06:47.920
You guessed it.

157
06:47.920 --> 06:51.380
We just registered the output. Let's just call this or I don't know list

158
06:51.380 --> 06:52.240
results. And again,

159
06:52.240 --> 06:55.030
we just print the output of the command use a debug module. Again, we can use

160
06:55.030 --> 06:57.280
the verb parameter.

161
06:57.280 --> 07:01.480
And again, we can just say list result and grab the STD out just to see the
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

161
06:57.280 --> 07:01.480
And again, we can just say list result and grab the STD out just to see the

162
07:01.480 --> 07:02.720
result of that particular

163
07:02.720 --> 07:06.480
command. We're in the playbook, hit enter. As we can see here, despite the ugly

164
07:06.480 --> 07:07.280
formatting, we're

165
07:07.280 --> 07:11.680
actually seeing the output of the LS dash L command for both of our devices.
