WEBVTT

1
00:00.000 --> 00:04.720
So friends with our new found super power, how about we begin exploring some

2
00:04.720 --> 00:05.560
additional

3
00:05.560 --> 00:08.880
and simple modules directly via the command line.

4
00:08.880 --> 00:11.120
So what we're waiting for, let's get to it.

5
00:11.120 --> 00:15.400
So let's go in and have a look at the listing of our modules right here.

6
00:15.400 --> 00:19.260
And the first one I'm going to have a look at right here is this one there, the

7
00:19.260 --> 00:19.600
STAT

8
00:19.600 --> 00:20.600
module.

9
00:20.600 --> 00:24.900
As it says here, this is going to allow us to retrieve file or file system

10
00:24.900 --> 00:25.640
status.

11
00:25.640 --> 00:28.000
So let's just grab it and copy it.

12
00:28.000 --> 00:32.080
And I'll press Q2quits, let's just clear the screen and we'll just say Ansible
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

12
00:28.000 --> 00:32.080
And I'll press Q2quits, let's just clear the screen and we'll just say Ansible

13
00:32.080 --> 00:32.360
- Doc

14
00:32.360 --> 00:35.360
and paste in the option for STAT.

15
00:35.360 --> 00:38.770
So as we can see this is going to retrieve facts for a file system similar to

16
00:38.770 --> 00:39.320
the Linux

17
00:39.320 --> 00:41.120
or Unix STAT commands.

18
00:41.120 --> 00:44.630
So scrolling on down we can see the path parameter has an equal sign next to it

19
00:44.630 --> 00:45.360
that tells us

20
00:45.360 --> 00:49.630
this is a require parameter and this is going to be the full path of the file

21
00:49.630 --> 00:50.200
or object

22
00:50.200 --> 00:51.960
where we want to get the facts.

23
00:51.960 --> 00:54.320
So what I will do here is I'll create a new playbook.

24
00:54.320 --> 00:56.760
So let's create Play4.yaml.

25
00:56.760 --> 00:58.520
Over three hyphens, give it a name.

26
00:58.520 --> 01:02.040
So we play for testing the STAT module.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

26
00:58.520 --> 01:02.040
So we play for testing the STAT module.

27
01:02.040 --> 01:04.280
What we're going to do, we're going to target the all group.

28
01:04.280 --> 01:09.280
We'll have gather facts set to false and now we define our tasks.

29
01:09.280 --> 01:11.480
So I'll just say run the STAT module.

30
01:11.480 --> 01:15.860
So what we'll use, we'll use the Ansible built in.stat module and the require

31
01:15.860 --> 01:16.360
parameter

32
01:16.360 --> 01:17.560
is going to be the path.

33
01:17.560 --> 01:20.720
So in this case here I'll test for the temp directory and we're going to check

34
01:20.720 --> 01:21.320
for a file

35
01:21.320 --> 01:23.840
called CBT file.txt.

36
01:23.840 --> 01:27.420
Now when I actually run this particular command I want to capture the output so

37
01:27.420 --> 01:27.960
I'm going

38
01:27.960 --> 01:32.520
to use the register option and I'll just call this STAT_result.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

38
01:27.960 --> 01:32.520
to use the register option and I'll just call this STAT_result.

39
01:32.520 --> 01:36.140
So for now all I will do is I'll create a new task and I'll just say print the

40
01:36.140 --> 01:36.800
result.

41
01:36.800 --> 01:40.930
And I'll use the debug module with the var parameter and the variable I'm going

42
01:40.930 --> 01:41.480
to print

43
01:41.480 --> 01:43.600
is going to be STAT_result.

44
01:43.600 --> 01:46.410
So what I'm going to do here is I'm going to create this particular file on the

45
01:46.410 --> 01:46.680
blue

46
01:46.680 --> 01:49.640
box but not have it exist on the yellow box.

47
01:49.640 --> 01:54.240
So I will say touch CBT file.txt so now it exists right here on the blue box

48
01:54.240 --> 01:54.800
but not

49
01:54.800 --> 01:55.800
the yellow.

50
01:55.800 --> 01:56.800
Now watch what happens.

51
01:56.800 --> 01:58.720
Let's go and run this Ansible playbook.

52
01:58.720 --> 02:03.540
So I'll say Ansible playbook play 4.yml and to enter and look at this right
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

52
01:58.720 --> 02:03.540
So I'll say Ansible playbook play 4.yml and to enter and look at this right

53
02:03.540 --> 02:04.160
here.

54
02:04.160 --> 02:07.120
The STAT module is getting information about the file that we specified.

55
02:07.120 --> 02:08.400
So look at the yellow box.

56
02:08.400 --> 02:13.200
We have this option here for STAT and it says exists and the value is false.

57
02:13.200 --> 02:17.840
That is because it doesn't actually exist on the yellow box.

58
02:17.840 --> 02:21.830
However if we scroll on up to the blue box right here the output is a little

59
02:21.830 --> 02:22.760
bit different

60
02:22.760 --> 02:23.920
a lot more verbose.

61
02:23.920 --> 02:28.390
The STAT option has all of these other attributes right here and if I scroll on

62
02:28.390 --> 02:29.200
down let's go

63
02:29.200 --> 02:30.480
to the exist option.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

63
02:29.200 --> 02:30.480
to the exist option.

64
02:30.480 --> 02:31.840
We see the value is true.

65
02:31.840 --> 02:36.120
That is because that particular file exists on the blue box.

66
02:36.120 --> 02:40.770
So here's what I could do is I could use this information to create a file if

67
02:40.770 --> 02:41.320
it does

68
02:41.320 --> 02:42.640
not exist on a device.

69
02:42.640 --> 02:44.440
So let's go into play 4.

70
02:44.440 --> 02:48.010
As opposed to just printing the STAT result we actually want to evaluate some

71
02:48.010 --> 02:48.880
of the information

72
02:48.880 --> 02:50.680
we've gotten back from that.

73
02:50.680 --> 02:54.190
So remember the format of the output within STAT result and let me just go back

74
02:54.190 --> 02:54.760
a second.

75
02:54.760 --> 02:58.910
The Kiwi grab is going to be the STAT key and then we can grab the exist key

76
02:58.910 --> 02:59.520
and we'll

77
02:59.520 --> 03:04.530
determine what that value is if the value is true then we're going to skip over
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

77
02:59.520 --> 03:04.530
determine what that value is if the value is true then we're going to skip over

78
03:04.530 --> 03:05.000
it and

79
03:05.000 --> 03:08.720
if the value is false we actually want to invoke the creation of the file on

80
03:08.720 --> 03:09.520
that particular

81
03:09.520 --> 03:10.520
machine.

82
03:10.520 --> 03:11.520
So let's go back in.

83
03:11.520 --> 03:16.750
And all I will do is I'll remove the information here and I'll just create the

84
03:16.750 --> 03:17.960
file if it doesn't

85
03:17.960 --> 03:19.600
exist.

86
03:19.600 --> 03:23.410
So what we'll use is the Ansible built-in command module and the command I'm

87
03:23.410 --> 03:24.440
going to run is

88
03:24.440 --> 03:31.480
simply going to be touch forward slash TMP forward slash CBT-file dot TXT.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

88
03:24.440 --> 03:31.480
simply going to be touch forward slash TMP forward slash CBT-file dot TXT.

89
03:31.480 --> 03:34.840
Now I only want to run this in a particular condition and that particular

90
03:34.840 --> 03:35.560
condition is

91
03:35.560 --> 03:39.440
when we grab the STAT result I'm going to the STAT key and then going to the

92
03:39.440 --> 03:40.080
exist key

93
03:40.080 --> 03:42.880
if that value equals false.

94
03:42.880 --> 03:47.170
Basically when we run this command if any of the devices do not have it present

95
03:47.170 --> 03:47.320
we're

96
03:47.320 --> 03:48.800
going to create it open.

97
03:48.800 --> 03:52.570
I've just noticed I have an error that should be built and not built in and

98
03:52.570 --> 03:53.280
lastly let me

99
03:53.280 --> 03:57.060
just make sure this key is correct that should be exists with an S that is very

100
03:57.060 --> 03:57.680
important

101
03:57.680 --> 04:00.400
that we get the keys correct and rerun the playbook.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

101
03:57.680 --> 04:00.400
that we get the keys correct and rerun the playbook.

102
04:00.400 --> 04:03.670
So check this out we ran the STAT module and when it came to creating the file

103
04:03.670 --> 04:04.240
we skipped

104
04:04.240 --> 04:08.470
over the blue box because it already existed in the case of the yellow box

105
04:08.470 --> 04:09.280
though we did

106
04:09.280 --> 04:13.260
create that particular file so if I go back and let's log into the yellow box

107
04:13.260 --> 04:13.680
let's first

108
04:13.680 --> 04:18.030
gain this and if I do an LS now we can see the CBT file has been created which

109
04:18.030 --> 04:18.520
means

110
04:18.520 --> 04:22.560
if we go back and we rerun the playbook once again it will now exist therefore

111
04:22.560 --> 04:22.960
when it

112
04:22.960 --> 04:27.680
comes to the creation we just skip over the task for two devices now.

113
04:27.680 --> 04:31.170
So that is the basics of using the STAT command if you want to learn more we
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

113
04:27.680 --> 04:31.170
So that is the basics of using the STAT command if you want to learn more we

114
04:31.170 --> 04:31.680
know the place

115
04:31.680 --> 04:35.480
we can go to is directly via the terminal via the Ansible documentation.
