WEBVTT

1
00:00.000 --> 00:04.140
So friends, now that we have a better understanding about YAML, what we now

2
00:04.140 --> 00:05.740
want to do is to actually

3
00:05.740 --> 00:07.540
get hands on with Ansible.

4
00:07.540 --> 00:10.840
Now here's the thing, the very first thing we're going to be doing with Ansible

5
00:10.840 --> 00:11.460
is not

6
00:11.460 --> 00:13.580
going to involve YAML or Playbooks.

7
00:13.580 --> 00:17.240
We're going to be looking at something called ad hoc commands to effectively

8
00:17.240 --> 00:17.960
manage these

9
00:17.960 --> 00:22.040
two machines we have right here on the fly directly via the command line.

10
00:22.040 --> 00:23.940
So with that said, let's just do it.

11
00:23.940 --> 00:26.300
So like I say, I have my two virtual machines.

12
00:26.300 --> 00:30.230
I have my yellow box and my blue box right here, let me just minimize these
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

12
00:26.300 --> 00:30.230
I have my yellow box and my blue box right here, let me just minimize these

13
00:30.230 --> 00:30.980
from now.

14
00:30.980 --> 00:34.420
And right here I have my what is going to be control node for Ansible.

15
00:34.420 --> 00:38.710
So right now within my directory structure, I have my inventory as we know, let

16
00:38.710 --> 00:38.900
's just

17
00:38.900 --> 00:41.340
denote my blue box and my yellow box.

18
00:41.340 --> 00:45.400
And these are correlating to the IP addresses within the XA host configuration

19
00:45.400 --> 00:45.940
file.

20
00:45.940 --> 00:52.170
We can see here I have mapped blue box to the IP address of 192.168.134, which

21
00:52.170 --> 00:53.100
is the

22
00:53.100 --> 00:56.810
first machine we're managing, as well as we have the name yellow box correl

23
00:56.810 --> 00:57.340
ating to

24
00:57.340 --> 00:59.340
the dot 135 address.

25
00:59.340 --> 01:02.140
So we have these two devices map within our inventory.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

25
00:59.340 --> 01:02.140
So we have these two devices map within our inventory.

26
01:02.140 --> 01:06.450
We can refer to both devices at once by just referencing the all group, which

27
01:06.450 --> 01:07.180
comprises

28
01:07.180 --> 01:10.380
all the devices we're managing, which is only two after all.

29
01:10.380 --> 01:12.260
And we have our Ansible CFG file.

30
01:12.260 --> 01:15.260
So inside of here, we specify where our inventory is.

31
01:15.260 --> 01:18.020
The user we're going to log in as is going to be Ansible.

32
01:18.020 --> 01:21.460
And we have some basic information about how we elevate the privileges.

33
01:21.460 --> 01:23.620
We talked about this in the previous skill.

34
01:23.620 --> 01:26.980
Now right now I am actually not going to go and write a playbook.

35
01:26.980 --> 01:30.700
Like I said, we're going to use these ad hoc commands.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

35
01:26.980 --> 01:30.700
Like I said, we're going to use these ad hoc commands.

36
01:30.700 --> 01:34.620
Now ad hoc is simply a word that actually comes from Latin.

37
01:34.620 --> 01:37.260
And it just simply means for this.

38
01:37.260 --> 01:40.660
And this actually kind of tells you the purpose and use case for using ad hoc

39
01:40.660 --> 01:41.380
commands.

40
01:41.380 --> 01:45.670
It's kind of like a off the cuff thing you would do for a one off type of thing

41
01:45.670 --> 01:45.780
.

42
01:45.780 --> 01:49.180
These are absolutely not meant to replace the use of playbooks.

43
01:49.180 --> 01:53.180
It should be used for more repeatable, reliable and well tested automation.

44
01:53.180 --> 01:56.410
But sometimes perhaps maybe you don't have a particular playbook already

45
01:56.410 --> 01:57.020
created.

46
01:57.020 --> 02:00.460
And you just want to make a quick change to a few devices in the network.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

46
01:57.020 --> 02:00.460
And you just want to make a quick change to a few devices in the network.

47
02:00.460 --> 02:04.020
Good way we can instruct Ansible to do this is just quickly via an ad hoc

48
02:04.020 --> 02:04.740
command.

49
02:04.740 --> 02:07.690
So before we actually run a command, let's go and check the Ansible

50
02:07.690 --> 02:08.580
documentation.

51
02:08.580 --> 02:11.980
So if you just search for Ansible ad hoc commands, we can see we have this

52
02:11.980 --> 02:12.660
documentation

53
02:12.660 --> 02:13.660
right here.

54
02:13.660 --> 02:14.660
Let's click the link.

55
02:14.660 --> 02:18.460
Now it says right here an Ansible ad hoc command is a command line tool like I

56
02:18.460 --> 02:18.660
say

57
02:18.660 --> 02:19.660
it's not a playbook.

58
02:19.660 --> 02:24.100
And it's used to automate a single task on one or more managed nodes.

59
02:24.100 --> 02:28.200
Basically you can specify which set of devices within your inventory you want

60
02:28.200 --> 02:28.980
to target.

61
02:28.980 --> 02:32.860
And you can effectively induce a single task on all of those devices.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

61
02:28.980 --> 02:32.860
And you can effectively induce a single task on all of those devices.

62
02:32.860 --> 02:35.860
So we see the actual use case for ad hoc commands.

63
02:35.860 --> 02:38.940
Like I say, they can be quickly used to reboot servers, copy files, that type

64
02:38.940 --> 02:39.700
of thing.

65
02:39.700 --> 02:42.260
So let's actually check out some of the syntax we can see here.

66
02:42.260 --> 02:43.740
So let's scroll on down.

67
02:43.740 --> 02:46.220
Let's look at this example and the documentation right here.

68
02:46.220 --> 02:50.340
It says Ansible Atlanta dash a and then it says a spin reboot.

69
02:50.340 --> 02:51.340
What's going on here?

70
02:51.340 --> 02:54.740
The first commands we see here is the word Ansible that is just invoking Ans

71
02:54.740 --> 02:55.700
ible to run

72
02:55.700 --> 02:57.100
an ad hoc command.

73
02:57.100 --> 02:58.940
The next one here says Atlanta.

74
02:58.940 --> 03:03.300
Now in this case here Atlanta will be a group in the user's inventory.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

74
02:58.940 --> 03:03.300
Now in this case here Atlanta will be a group in the user's inventory.

75
03:03.300 --> 03:05.260
In our case, all we have is an all group.

76
03:05.260 --> 03:08.780
But like I say, as we know within the inventory, we can group things up by

77
03:08.780 --> 03:09.620
location or via

78
03:09.620 --> 03:10.620
function.

79
03:10.620 --> 03:13.860
In this case here, they are apparently grouping devices via the location.

80
03:13.860 --> 03:15.580
So in this case here Atlanta, Georgia.

81
03:15.580 --> 03:20.080
So all devices in Atlanta, we're going to specify a particular argument and

82
03:20.080 --> 03:20.340
this is

83
03:20.340 --> 03:22.300
effectively the command that we want to send.

84
03:22.300 --> 03:26.630
So pretty much we want to call the a spin reboot binary, which is going to

85
03:26.630 --> 03:27.340
reboot all

86
03:27.340 --> 03:30.580
of the devices within the Atlanta group.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

86
03:27.340 --> 03:30.580
of the devices within the Atlanta group.

87
03:30.580 --> 03:34.490
Simply just typing this command on the command line could be potentially reb

88
03:34.490 --> 03:35.500
ooting thousands

89
03:35.500 --> 03:38.260
of devices in Atlanta all at the same time.

90
03:38.260 --> 03:42.060
Now notice here we have this option dash f for forks.

91
03:42.060 --> 03:44.740
This is actually really important as to how Ansible operates.

92
03:44.740 --> 03:49.700
We can see here by default Ansible is only going to use five simultaneous

93
03:49.700 --> 03:50.500
processes pretty

94
03:50.500 --> 03:53.860
much it will connect to five devices at the same time.

95
03:53.860 --> 03:57.140
Then once that is done, it will spin up a new process, connect to another five

96
03:57.140 --> 03:57.660
and then

97
03:57.660 --> 03:59.820
another five and then another five.

98
03:59.820 --> 04:03.230
If you only happen to have five or 10 devices, this is not really much of an
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

98
03:59.820 --> 04:03.230
If you only happen to have five or 10 devices, this is not really much of an

99
04:03.230 --> 04:03.820
issue.

100
04:03.820 --> 04:07.660
But if you happen to have thousands of devices, this can take a long time.

101
04:07.660 --> 04:10.620
So you can effectively touch your computer more.

102
04:10.620 --> 04:14.160
You test the power of the CPU and you can increase the amount of parallel

103
04:14.160 --> 04:14.900
connections

104
04:14.900 --> 04:15.900
at once.

105
04:15.900 --> 04:16.900
This is called forking.

106
04:16.900 --> 04:20.650
So imagine if you happen to have 100 devices, if you only connect to two

107
04:20.650 --> 04:21.500
devices at a time

108
04:21.500 --> 04:23.340
that's going to take a long time.

109
04:23.340 --> 04:27.290
Whereas if you connect to 50 devices at the same time, well, it's only going to

110
04:27.290 --> 04:28.060
take Ansible

111
04:28.060 --> 04:32.340
two runs to effectively automate all of the devices in the network.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

111
04:28.060 --> 04:32.340
two runs to effectively automate all of the devices in the network.

112
04:32.340 --> 04:34.740
But be aware of course that will stress your CPU.

113
04:34.740 --> 04:38.300
Now again, we can see some more information right here by default.

114
04:38.300 --> 04:42.020
You can see that Ansible is going to be running from your user account.

115
04:42.020 --> 04:45.750
If you want to specify a different user name from the command line, you can use

116
04:45.750 --> 04:46.180
a dash

117
04:46.180 --> 04:47.180
u flag.

118
04:47.180 --> 04:50.860
Similarly, we can use the dash M flag, which is going to allow to specify

119
04:50.860 --> 04:51.940
different modules

120
04:51.940 --> 04:56.020
we want to use Ansible actually has a whole ecosystem of modules for particular

121
04:56.020 --> 04:56.500
tasks.

122
04:56.500 --> 04:59.820
And we'll get to look at that in more detail in an upcoming scale.

123
04:59.820 --> 05:03.020
But perhaps you want to maybe copy particular files.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

123
04:59.820 --> 05:03.020
But perhaps you want to maybe copy particular files.

124
05:03.020 --> 05:06.620
You could use a copy module specified with the dash M flag.

125
05:06.620 --> 05:08.740
You just want to run some regular shell commands.

126
05:08.740 --> 05:10.220
You can use the shell module.

127
05:10.220 --> 05:13.140
There are a whole bunch of different ones available to us.

128
05:13.140 --> 05:14.140
So check this out.

129
05:14.140 --> 05:18.080
Perhaps I want to create a new directory on all of my devices as opposed to

130
05:18.080 --> 05:18.820
logging in

131
05:18.820 --> 05:21.940
via SSH one by one and doing this manually.

132
05:21.940 --> 05:25.580
What I can do is just say Ansible and then all to target the all group.

133
05:25.580 --> 05:28.500
I'll then say dash M to specify the module.

134
05:28.500 --> 05:30.580
And in this case here, I want to call the shell module.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

134
05:28.500 --> 05:30.580
And in this case here, I want to call the shell module.

135
05:30.580 --> 05:34.420
I'm going to be typing a regular shell command and the argument I want to pass

136
05:34.420 --> 05:35.340
in via the dash

137
05:35.340 --> 05:38.700
A flag is simply within inverted commas.

138
05:38.700 --> 05:39.700
MKDAR.

139
05:39.700 --> 05:44.260
Let's just say forward slash TMP forward slash Ansible test.

140
05:44.260 --> 05:47.180
So if I hit enter now, we can see here we get somebody back.

141
05:47.180 --> 05:49.260
It says yellow box changed.

142
05:49.260 --> 05:51.260
And it also says blue box changed.

143
05:51.260 --> 05:54.940
This indicates that we have successfully made a change on both of those devices

144
05:54.940 --> 05:55.180
.

145
05:55.180 --> 05:56.980
So let's go and test this in.

146
05:56.980 --> 06:00.430
So let's crack open a terminal window and I will just cd enter the temp
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

146
05:56.980 --> 06:00.430
So let's crack open a terminal window and I will just cd enter the temp

147
06:00.430 --> 06:01.100
directory if

148
06:01.100 --> 06:02.620
I do an LS.

149
06:02.620 --> 06:03.620
Look at this right here.

150
06:03.620 --> 06:06.890
We actually do have a directory called Ansible test now right now and say to

151
06:06.890 --> 06:07.740
that directory,

152
06:07.740 --> 06:10.300
we don't have any content because we didn't create any content.

153
06:10.300 --> 06:14.330
If we so wished we could also create a file within this directory or perhaps

154
06:14.330 --> 06:14.900
even copy

155
06:14.900 --> 06:18.540
a file from one directory to another that type of thing.

156
06:18.540 --> 06:22.440
Similarly, if we go over to our other machine, the yellow box, which is full

157
06:22.440 --> 06:23.060
screen this

158
06:23.060 --> 06:28.260
go to activities terminal and we just cd enter the temp directory as well doing

159
06:28.260 --> 06:28.820
LS.

160
06:28.820 --> 06:31.940
Notice we have also created a new directory here as well.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

160
06:28.820 --> 06:31.940
Notice we have also created a new directory here as well.

161
06:31.940 --> 06:35.880
Let's maybe say I wanted to say Ansible all and I will use the shell module

162
06:35.880 --> 06:36.620
once again.

163
06:36.620 --> 06:40.570
It will specify the argument is going to be and this time I will say IP adder

164
06:40.570 --> 06:40.980
to get

165
06:40.980 --> 06:44.820
the IP address information and I'm going to grep for 192.

166
06:44.820 --> 06:48.690
If I hit enter, we can quickly see within the yellow box, we have the IP

167
06:48.690 --> 06:52.300
address 192.168.135.

168
06:52.300 --> 06:58.890
And in the blue box, we have the address 192.168.160.134, easily accessing all

169
06:58.890 --> 07:00.580
the IP address information,
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

169
06:58.890 --> 07:00.580
the IP address information,

170
07:00.580 --> 07:02.620
all the devices within our inventory.

171
07:02.620 --> 07:05.400
Now as we're going to explore, there are a whole bunch of different Ansible

172
07:05.400 --> 07:05.900
modules.

173
07:05.900 --> 07:09.020
Let's maybe say we could use the user module, which is designed to create brand

174
07:09.020 --> 07:09.620
new users

175
07:09.620 --> 07:10.620
on the system.

176
07:10.620 --> 07:15.130
We can say -a for the arguments and all I will say is name is equal to and then

177
07:15.130 --> 07:15.540
give

178
07:15.540 --> 07:20.500
a new username, let's just say test user and I will say the state is equal to

179
07:20.500 --> 07:21.220
present.

180
07:21.220 --> 07:23.220
If I hit enter, check this out right here.

181
07:23.220 --> 07:26.090
We can see on the yellow box, we have a change and look at this information

182
07:26.090 --> 07:26.540
here.

183
07:26.540 --> 07:28.340
We have this new user.

184
07:28.340 --> 07:32.460
The name is test user, they have a home directory, they have a UID.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

184
07:28.340 --> 07:32.460
The name is test user, they have a home directory, they have a UID.

185
07:32.460 --> 07:36.730
Similarly, if we go up to the blue box, also in the blue box, we have created

186
07:36.730 --> 07:37.420
this user,

187
07:37.420 --> 07:38.420
they also have a UID.

188
07:38.420 --> 07:42.900
And once again, if I so wished, I can maybe cat the Etsy group file right here

189
07:42.900 --> 07:43.500
and look

190
07:43.500 --> 07:44.500
at this right here.

191
07:44.500 --> 07:47.670
If we scroll on up, we can see all of the group information for yellow box and

192
07:47.670 --> 07:47.940
at the

193
07:47.940 --> 07:51.780
very bottom, we have the new user we just created and if we scroll up to the

194
07:51.780 --> 07:52.300
top, we

195
07:52.300 --> 07:56.660
can see the information for the blue box and just moving on down a little bit.

196
07:56.660 --> 08:00.420
We can also see the test user exists on the blue box too.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

196
07:56.660 --> 08:00.420
We can also see the test user exists on the blue box too.

197
08:00.420 --> 08:04.110
Now just one thing to note, sometimes when using these commands, you might get

198
08:04.110 --> 08:04.580
erroneous

199
08:04.580 --> 08:06.540
information from Ansible.

200
08:06.540 --> 08:10.460
Quite simply, you might be told that a task has failed, even though the task

201
08:10.460 --> 08:11.180
has actually

202
08:11.180 --> 08:12.820
succeeded, let me show you what I mean.

203
08:12.820 --> 08:14.980
So let me go and pull up these devices right here.

204
08:14.980 --> 08:18.550
So clearly we can see on the right hand side, we have a blue box and yellow box

205
08:18.550 --> 08:18.820
.

206
08:18.820 --> 08:22.860
What I'm going to say here is Ansible, all and the module is going to be the

207
08:22.860 --> 08:24.060
shell module

208
08:24.060 --> 08:29.420
and the argument is going to simply be Espin, a reboot and if I hit enter, look

209
08:29.420 --> 08:29.540
on the

210
08:29.540 --> 08:33.100
right hand side, they actually are rebooting but notice the information we're
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

210
08:29.540 --> 08:33.100
right hand side, they actually are rebooting but notice the information we're

211
08:33.100 --> 08:33.540
getting

212
08:33.540 --> 08:37.530
back here, apparently it says the blue box is unreachable and the yellow box is

213
08:37.530 --> 08:38.300
unreachable

214
08:38.300 --> 08:40.620
failed to connect to the host via SSH.

215
08:40.620 --> 08:44.360
However, we can clearly see that the command that we sent, the reboot command,

216
08:44.360 --> 08:44.940
did indeed

217
08:44.940 --> 08:46.820
reboot both of these devices.

218
08:46.820 --> 08:50.770
So in this case here, because the actual SSH connection was disturbed due to

219
08:50.770 --> 08:51.300
the fact

220
08:51.300 --> 08:56.370
that we did reboot from an ad hoc command, Ansible falsely interpreted that as

221
08:56.370 --> 08:57.060
a failure

222
08:57.060 --> 09:01.270
in the connection, even though the task itself was indeed successful, just
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

222
08:57.060 --> 09:01.270
in the connection, even though the task itself was indeed successful, just

223
09:01.270 --> 09:01.940
something to be

224
09:01.940 --> 09:04.140
aware of when running these ad hoc commands.
