WEBVTT

1
00:00.000 --> 00:01.680
Okay friends, so think about it like this.

2
00:01.680 --> 00:03.840
We're going to have to write a playbook

3
00:03.840 --> 00:06.520
that is effectively going to gather factual information

4
00:06.520 --> 00:07.960
about our devices.

5
00:07.960 --> 00:10.440
Now what type of information are we going to have to get?

6
00:10.440 --> 00:12.600
Well, we're going to have to get the host name, of course,

7
00:12.600 --> 00:14.640
as well as the Linux distribution,

8
00:14.640 --> 00:16.640
then the IPv4 address,

9
00:16.640 --> 00:19.560
as well as the device's uptime and seconds.

10
00:19.560 --> 00:21.720
Now this information can all be gathered

11
00:21.720 --> 00:23.760
ultimately via the setup module,

12
00:23.760 --> 00:26.000
but we want to effectively devise

13
00:26.000 --> 00:27.920
which particular keys pull out

14
00:27.920 --> 00:30.280
these very particular pieces of information.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

14
00:27.920 --> 00:30.280
these very particular pieces of information.

15
00:30.280 --> 00:31.880
So let's just go and try that first in.

16
00:31.880 --> 00:34.360
What I will do here is I'll just say Ansible,

17
00:34.360 --> 00:35.760
and let's just target one device for now

18
00:35.760 --> 00:37.480
to keep the output a little bit smaller.

19
00:37.480 --> 00:38.680
We'll say device number one,

20
00:38.680 --> 00:40.680
and I'll just use the setup module here.

21
00:40.680 --> 00:42.040
So if I hit enter here,

22
00:42.040 --> 00:44.240
just give it a moment to gather all the facts

23
00:44.240 --> 00:45.240
and check this out.

24
00:45.240 --> 00:47.680
We see all of this information, a whole ton of it.

25
00:47.680 --> 00:48.680
So let's scroll on up

26
00:48.680 --> 00:51.160
and let's find out the keys that are actually relevant to us.

27
00:51.160 --> 00:52.880
So I feel like at the key here,

28
00:52.880 --> 00:55.400
Ansible default IPv4,

29
00:55.400 --> 00:58.040
inside of this here, we also have an address key

30
00:58.040 --> 01:00.360
that is going to give us the IPv4 address
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

30
00:58.040 --> 01:00.360
that is going to give us the IPv4 address

31
01:00.360 --> 01:01.640
of that particular device.

32
01:01.640 --> 01:02.600
So think about it that.

33
01:02.600 --> 01:04.520
So let's collect that information straight away.

34
01:04.520 --> 01:05.400
So inside of here,

35
01:05.400 --> 01:06.800
what I'm going to do is paste this in

36
01:06.800 --> 01:08.240
and then say dot address

37
01:08.240 --> 01:10.000
that gets that piece of information.

38
01:10.000 --> 01:11.160
If I scroll on down,

39
01:11.160 --> 01:13.600
I see this key for Ansible distribution,

40
01:13.600 --> 01:16.600
which returns red hat that is the distribution of the device.

41
01:16.600 --> 01:19.160
So let's grab this particular key here,

42
01:19.160 --> 01:21.120
and again, we'll paste this in right here.

43
01:21.120 --> 01:22.120
If we grab the key,

44
01:22.120 --> 01:23.720
Ansible_hostname,

45
01:23.720 --> 01:25.320
that is going to return the actual hostname.

46
01:25.320 --> 01:27.360
So let's just grab this one right here.

47
01:27.360 --> 01:30.280
And lastly, we want to see the uptime in seconds.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

47
01:27.360 --> 01:30.280
And lastly, we want to see the uptime in seconds.

48
01:30.280 --> 01:31.120
And as we can see,

49
01:31.120 --> 01:32.200
we have this particular value,

50
01:32.200 --> 01:33.760
Ansible uptime second.

51
01:33.760 --> 01:34.920
So let's just copy this

52
01:34.920 --> 01:36.120
and I'll get around that space here.

53
01:36.120 --> 01:37.560
So let's just pop this one there.

54
01:37.560 --> 01:38.480
Okay. So basically,

55
01:38.480 --> 01:40.640
what we now want to do is to create a template

56
01:40.640 --> 01:42.760
that's going to use this information

57
01:42.760 --> 01:46.240
and effectively populate it into a text file on the device.

58
01:46.240 --> 01:48.440
So let's go to our device right here

59
01:48.440 --> 01:50.120
or rather our control mode, should I say.

60
01:50.120 --> 01:52.280
And let's just call this playbook right

61
01:52.280 --> 01:53.920
or maybe even let's say right facts.

62
01:53.920 --> 01:54.760
Okay.

63
01:54.760 --> 01:55.600
So let's go into the top,

64
01:55.600 --> 01:56.760
have our three hyphens.

65
01:56.760 --> 01:58.200
Let's give this playbook a name.

66
01:58.200 --> 01:59.920
We'll just a gather information

67
01:59.920 --> 02:02.320
and write details about devices.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

67
01:59.920 --> 02:02.320
and write details about devices.

68
02:02.320 --> 02:03.360
So here we're going to target,

69
02:03.360 --> 02:04.480
we'll target the all group.

70
02:04.480 --> 02:05.320
In this case here,

71
02:05.320 --> 02:07.000
we do actually want to gather facts.

72
02:07.000 --> 02:08.840
Now by default, facts are going to be gathered

73
02:08.840 --> 02:10.920
so you don't actually have to specify this explicitly,

74
02:10.920 --> 02:12.600
but let's just do so for posterity.

75
02:12.600 --> 02:14.440
And now I'll define my tasks.

76
02:14.440 --> 02:17.640
So task here is going to be use a template

77
02:17.640 --> 02:19.320
to write fail to this.

78
02:19.320 --> 02:20.280
So what we're going to use,

79
02:20.280 --> 02:22.880
we're going to use the Ansible_Belten template module.

80
02:22.880 --> 02:24.080
And as we saw before,

81
02:24.080 --> 02:26.080
we're going to have a particular source.

82
02:26.080 --> 02:27.440
And the source here is going to be called,

83
02:27.440 --> 02:28.560
let's just call this,

84
02:28.560 --> 02:31.720
I don't know, device info.j2.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

84
02:28.560 --> 02:31.720
I don't know, device info.j2.

85
02:31.720 --> 02:33.640
And the destination is where abouts

86
02:33.640 --> 02:35.440
on the actual target do we want to write

87
02:35.440 --> 02:36.400
this particular fails.

88
02:36.400 --> 02:38.400
I'm going to use the temp directory.

89
02:38.400 --> 02:40.480
And I believe the actual fails should be called

90
02:40.480 --> 02:42.840
device_info.txt.

91
02:42.840 --> 02:44.160
Let me just double check that.

92
02:44.160 --> 02:46.640
Yep, device info.txt, that's the right one.

93
02:46.640 --> 02:47.480
Perfect.

94
02:47.480 --> 02:48.840
So that is all we need for the actual playbook,

95
02:48.840 --> 02:53.000
but we do have to construct our template device info.j2.

96
02:53.000 --> 02:55.720
So let's ensure we have our templates directory right here.

97
02:55.720 --> 02:56.920
So let's go into this.

98
02:56.920 --> 03:00.600
And let's just create this template called device_info.j2.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

98
02:56.920 --> 03:00.600
And let's just create this template called device_info.j2.

99
03:00.600 --> 03:02.840
So let's just say my hostname is,

100
03:02.840 --> 03:05.200
and then I'll use my double color braces.

101
03:05.200 --> 03:07.680
And we'll grab the Ansible hostname right here,

102
03:07.680 --> 03:08.520
paste it in.

103
03:08.520 --> 03:10.320
We'll say my Linux distribution.

104
03:10.320 --> 03:11.240
And again, let's go back.

105
03:11.240 --> 03:13.160
Let's grab this particular key here.

106
03:13.160 --> 03:14.920
Again, double color braces.

107
03:14.920 --> 03:16.440
And we'll just paste this in.

108
03:16.440 --> 03:18.600
Say my IPE4 address,

109
03:18.600 --> 03:21.360
let's grab the default IPE4 address right here.

110
03:21.360 --> 03:23.040
Again, let's have the double color braces

111
03:23.040 --> 03:24.440
and just paste in our value.

112
03:24.440 --> 03:27.040
I'll just say my current uptime is,

113
03:27.040 --> 03:29.960
and then pass in the value with my color braces.

114
03:29.960 --> 03:33.440
And we'll just copy in Ansible uptime seconds right here.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

114
03:29.960 --> 03:33.440
And we'll just copy in Ansible uptime seconds right here.

115
03:33.440 --> 03:35.440
Grab here and just paste.

116
03:35.440 --> 03:37.640
And let's just add on the word seconds at the end,

117
03:37.640 --> 03:40.000
because this is just going to give us an numerical value.

118
03:40.000 --> 03:42.280
So let's just write and quit to save this.

119
03:42.280 --> 03:44.280
Let's go back a directory to our playbook,

120
03:44.280 --> 03:45.280
clear the screen.

121
03:45.280 --> 03:47.240
And all we'll say is AnsiblePlayBook,

122
03:47.240 --> 03:50.560
write facts.yml and hit enter.

123
03:50.560 --> 03:53.360
So let's give it some time to gather facts about the devices.

124
03:53.360 --> 03:54.400
Now the gather facts.

125
03:54.400 --> 03:58.360
And we begin using your template to write the file to the disk.

126
03:58.360 --> 04:00.840
So what I can do is go into my devices via SSH
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

126
03:58.360 --> 04:00.840
So what I can do is go into my devices via SSH

127
04:00.840 --> 04:01.680
to actually verify this.

128
04:01.680 --> 04:03.880
And in fact, instead of doing that, what I will do.

129
04:03.880 --> 04:05.880
So I'll just use Ansible to target both devices

130
04:05.880 --> 04:07.720
at once, I'll say AnsibleAll.

131
04:07.720 --> 04:09.280
And all I will do here,

132
04:09.280 --> 04:11.520
is I'll just attempt to cat the device itself.

133
04:11.520 --> 04:13.160
So I'll say cat forward slash TMP.

134
04:13.160 --> 04:16.640
And the file should be called device_info.txt

135
04:16.640 --> 04:17.640
within the temp directory.

136
04:17.640 --> 04:19.000
If I just enter here,

137
04:19.000 --> 04:22.000
check this out right here for device number two.

138
04:22.000 --> 04:24.280
Here is the actual information.

139
04:24.280 --> 04:25.600
My hostname is device two.

140
04:25.600 --> 04:27.520
My Linux distribution is Red Hat.

141
04:27.520 --> 04:32.200
My IPv4 address is 192.168.160.137.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

141
04:27.520 --> 04:32.200
My IPv4 address is 192.168.160.137.

142
04:32.200 --> 04:36.880
And it says my current uptime is 9,678 seconds.

143
04:36.880 --> 04:39.080
Then we go below to device number one.

144
04:39.080 --> 04:41.160
And we get a different set of information.

145
04:41.160 --> 04:43.480
It says my hostname is device one.

146
04:43.480 --> 04:45.520
My distro is Red Hat.

147
04:45.520 --> 04:49.560
My IPv4 address is 192.168.160.136.

148
04:49.560 --> 04:54.040
And my current uptime is 9,481 seconds.
