WEBVTT

1
00:00.000 --> 00:05.040
So in the previous video, we learned how to utilize the STAT module and the way

2
00:05.040 --> 00:08.000
we learned how to use this module was via AnsibleDoc.

3
00:08.000 --> 00:11.570
Now in this particular video, what I want to highlight to you is yet another

4
00:11.570 --> 00:12.000
module.

5
00:12.000 --> 00:17.330
This time called "line and fail" and once again we're going to be utilizing Ans

6
00:17.330 --> 00:20.000
ibleDoc to learn how to actually use this.

7
00:20.000 --> 00:24.330
Now one thing I will say here, you will recall in previous videos or rather

8
00:24.330 --> 00:28.000
previous skills, we happen to use the Genji 2 templating language

9
00:28.000 --> 00:32.310
and this allows us to effectively render an entire configuration file and then
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

9
00:28.000 --> 00:32.310
and this allows us to effectively render an entire configuration file and then

10
00:32.310 --> 00:34.000
we could use something like the copy module

11
00:34.000 --> 00:38.000
to copy those rendered configs over to the target machines.

12
00:38.000 --> 00:40.800
Now quite honestly when we happen to be making many many changes to a

13
00:40.800 --> 00:44.000
particular configuration, this is my preferred method.

14
00:44.000 --> 00:49.040
It is much more comprehensive, it gives us access to programmatic logic that

15
00:49.040 --> 00:52.000
type of thing, but it also can be quite time consuming

16
00:52.000 --> 00:56.670
and honestly it can be a little bit difficult depending on just how complex the

17
00:56.670 --> 00:58.000
configurations are.

18
00:58.000 --> 01:01.520
And quite honestly, sometimes all you have to do is to make a simple change
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

18
00:58.000 --> 01:01.520
And quite honestly, sometimes all you have to do is to make a simple change

19
01:01.520 --> 01:06.000
within a configuration file, perhaps maybe one line has to change

20
01:06.000 --> 01:09.580
or maybe a block of lines have to change that type of thing as opposed to

21
01:09.580 --> 01:12.000
really having to render an entire configuration altogether.

22
01:12.000 --> 01:16.490
So given the fact that we're now learning to explore AnsibleDoc, I thought this

23
01:16.490 --> 01:20.000
would be a good time to introduce this particular module, "line and fail".

24
01:20.000 --> 01:22.000
So with that said, let's go and check it out.

25
01:22.000 --> 01:26.000
So once again, if we just list what we actually have here within our AnsibleDoc

26
01:26.000 --> 01:30.730
, if I scroll on down, notice we have this one right here, "line and fail", and
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

26
01:26.000 --> 01:30.730
, if I scroll on down, notice we have this one right here, "line and fail", and

27
01:30.730 --> 01:31.000
it says here,

28
01:31.000 --> 01:35.000
this is going to allow us to manage lines in text files.

29
01:35.000 --> 01:39.650
So all I will do here is just grab this and copy here, press "Q" and I'll call

30
01:39.650 --> 01:44.180
the screen, and then I'll just say " AnsibleDoc" and then paste in the name of

31
01:44.180 --> 01:45.000
the module.

32
01:45.000 --> 01:48.820
So as per the description, what it says here is that this module is going to

33
01:48.820 --> 01:53.360
ensure that a particular line is in a file or replace a particular line and

34
01:53.360 --> 01:55.000
existing line that is.

35
01:55.000 --> 01:59.240
Notice the use case here, this is primarily useful when you want to change a

36
01:59.240 --> 02:01.000
single line in a file only.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

36
01:59.240 --> 02:01.000
single line in a file only.

37
02:01.000 --> 02:05.000
So like I say, as opposed to rendering the entire configuration via Ginger2,

38
02:05.000 --> 02:09.000
this module can help us simplify that process.

39
02:09.000 --> 02:12.710
So let's scroll on down, and we can see all of the available options once again

40
02:12.710 --> 02:13.000
.

41
02:13.000 --> 02:16.440
Notice we have options such as "insert before", so the line is going to be

42
02:16.440 --> 02:20.000
inserted before the last match of the specified regular expressions.

43
02:20.000 --> 02:22.000
Once again, this is what we're going to be utilizing.

44
02:22.000 --> 02:25.950
So say for example, you have the option "insert before", pretty much what we're

45
02:25.950 --> 02:29.560
going to do is to make use of a regular expression to determine a particular

46
02:29.560 --> 02:31.000
pattern within the file,
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

46
02:29.560 --> 02:31.000
pattern within the file,

47
02:31.000 --> 02:35.910
and this particular option is going to insert whatever text you select or

48
02:35.910 --> 02:40.000
rather provide before the last match of that regex.

49
02:40.000 --> 02:43.500
So like I say, you can explore this at your leisure, we have many, many options

50
02:43.500 --> 02:44.000
here.

51
02:44.000 --> 02:48.000
Notice this one right here, we have the "equal sign" and the "path" option.

52
02:48.000 --> 02:52.080
This again tells us that this is a mandatory parameter, and pretty much this is

53
02:52.080 --> 02:55.000
going to allow us to select the file that we want to target.

54
02:55.000 --> 02:59.430
Notice this option here "state" as well, this is whether a line should be there

55
02:59.430 --> 03:03.290
or not, so we have the option to specify should the line be absent or should
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

55
02:59.430 --> 03:03.290
or not, so we have the option to specify should the line be absent or should

56
03:03.290 --> 03:04.000
the line be present.

57
03:04.000 --> 03:07.970
So if we want to remove a particular line, we can specify "absent" or if we

58
03:07.970 --> 03:11.000
want to ensure it's there or we can set it to "present".

59
03:11.000 --> 03:14.670
Notice we also have this other option here "redjxp" and it's via this

60
03:14.670 --> 03:19.230
particular parameter that we can provide the regular expression for the pattern

61
03:19.230 --> 03:20.000
matching.

62
03:20.000 --> 03:22.000
So let's go and check this out then.

63
03:22.000 --> 03:26.330
So what I'm going to do here is let me go into my blue box and all I will do is

64
03:26.330 --> 03:30.700
go into my temp directory and I'm just going to create a file called "names.txt
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

64
03:26.330 --> 03:30.700
go into my temp directory and I'm just going to create a file called "names.txt

65
03:30.700 --> 03:31.000
".

66
03:31.000 --> 03:36.710
So we go into insert mode, I will say "James", "Somonor", "John", "Knox", "K

67
03:36.710 --> 03:41.580
ieth", and if I just press "skate colon wq" to write and quit, here is the file

68
03:41.580 --> 03:42.000
right here.

69
03:42.000 --> 03:45.740
And what I want to do is I want to target this particular file and I'm going to

70
03:45.740 --> 03:50.000
remove a particular line from this file, in this case here, I'm going to remove

71
03:50.000 --> 03:51.000
my name "John".

72
03:51.000 --> 03:55.450
Now of course this could be applied to a complex configuration file, maybe it's

73
03:55.450 --> 03:59.470
a DNS mapping, whatever it may be, but in the interest of simplicity, I'm just

74
03:59.470 --> 04:00.000
highlighting the name of the file.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

75
04:00.000 --> 04:04.000
I'm just highlighting the most simple case right here, so our basic text file.

76
04:04.000 --> 04:07.510
So what I'm going to do here is I'm going to create a playbook called "play5.y

77
04:07.510 --> 04:08.000
ml".

78
04:08.000 --> 04:11.000
Going to insert mode, have our three hyphens at the top, you know the drill.

79
04:11.000 --> 04:16.000
And we give it a name, "play5", "test the line in file module".

80
04:16.000 --> 04:19.250
Who am I going to target? Well I could target both devices if I so wish but in

81
04:19.250 --> 04:22.830
my case here, I'm just going to target the blue box and I will set "gather

82
04:22.830 --> 04:25.000
facts" to false to keep things in this display.

83
04:25.000 --> 04:29.680
And now I will define my tasks, so the name here, I'll just say "remove name

84
04:29.680 --> 04:31.000
from text file".
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

84
04:29.680 --> 04:31.000
from text file".

85
04:31.000 --> 04:34.130
Now what is the actual module I'm going to be using? It's going to be "answer

86
04:34.130 --> 04:37.000
able built in", "line in file".

87
04:37.000 --> 04:40.000
And we know the path parameter is "mandatory".

88
04:40.000 --> 04:43.980
So the path I want to target is going to be the temp directory and the file is

89
04:43.980 --> 04:46.000
going to be called "names.txt".

90
04:46.000 --> 04:50.990
Now what I want to do is I want to specify the states and I want something to

91
04:50.990 --> 04:52.000
be absent.

92
04:52.000 --> 04:55.470
Now what exactly do I want to be absent? What am I trying to match here? Well I

93
04:55.470 --> 04:58.000
can specify my Reg XP parameter.

94
04:58.000 --> 05:01.000
And all I'm going to do is to provide some regular expressions.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

94
04:58.000 --> 05:01.000
And all I'm going to do is to provide some regular expressions.

95
05:01.000 --> 05:04.000
Now this here is not going to be a course on regular expressions.

96
05:04.000 --> 05:07.000
You can look online to see how we can actually format these things.

97
05:07.000 --> 05:11.810
So basically I have my carrot to denote my start anchor and my dollar sign to

98
05:11.810 --> 05:14.000
denote my end anchor.

99
05:14.000 --> 05:17.340
Pretty much all I'm doing is saying that I want to match the word "John"

100
05:17.340 --> 05:18.000
exactly.

101
05:18.000 --> 05:21.700
So quite simply the module is going to search for this particular pattern "John

102
05:21.700 --> 05:22.000
".

103
05:22.000 --> 05:26.000
And if it finds it it's going to remove it because the state should be absent.

104
05:26.000 --> 05:30.260
So let's try this out then. And also "answerable playbook", "play5.yml" and "t
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

104
05:26.000 --> 05:30.260
So let's try this out then. And also "answerable playbook", "play5.yml" and "t

105
05:30.260 --> 05:31.000
it" "enter".

106
05:31.000 --> 05:36.270
Give it a moment and now we have a change. So if we go to verify this, we go

107
05:36.270 --> 05:37.000
back to the blue box.

108
05:37.000 --> 05:40.000
Let's cat the names. Once again, look at this right here.

109
05:40.000 --> 05:43.000
"John" has actually been removed from the list of names.

110
05:43.000 --> 05:46.000
Similarly, let's say we wanted to add a particular line in a file.

111
05:46.000 --> 05:50.000
Let's go in to play5 once again. And I'll create a new task.

112
05:50.000 --> 05:53.000
And I'll just say "adding name to text file".

113
05:53.000 --> 05:56.740
What I'm going to use, I'm going to use the "answerable built-in" line and file

114
05:56.740 --> 05:58.000
module once again.

115
05:58.000 --> 06:01.000
The path will be targeting the same text file.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

115
05:58.000 --> 06:01.000
The path will be targeting the same text file.

116
06:01.000 --> 06:04.980
Now I'm going to specify the line that I want to add. So let me add the name "

117
06:04.980 --> 06:06.000
La La" here.

118
06:06.000 --> 06:09.000
And if you look at the documentation, we also have this option to create.

119
06:09.000 --> 06:12.000
And all I will do is specify the value "yes".

120
06:12.000 --> 06:16.990
So if I just save this right here. And I read on the playbook, give it a moment

121
06:16.990 --> 06:17.000
.

122
06:17.000 --> 06:21.000
And check this out. Now we have made a change once again to the blue box.

123
06:21.000 --> 06:25.420
So let's cat the same file. Once again, we have successfully added "La La" to

124
06:25.420 --> 06:29.000
the list of names using the line in file module.

125
06:29.000 --> 06:32.000
So as you know, we have a lot of available modules within "answerable".
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

125
06:29.000 --> 06:32.000
So as you know, we have a lot of available modules within "answerable".

126
06:32.000 --> 06:35.000
We've just seen a few more within this particular skill.

127
06:35.000 --> 06:38.130
And remember the best place to find out about how to use these modules is

128
06:38.130 --> 06:40.000
directly via the documentation.

129
06:40.000 --> 06:44.090
And we can access that documentation directly on our terminal utilizing "answer

130
06:44.090 --> 06:45.000
able.com".
