So in the previous video, we learned how to utilize the STAT module and the way
we learned how to use this module was via AnsibleDoc.
Now in this particular video, what I want to highlight to you is yet another
module.
This time called "line and fail" and once again we're going to be utilizing Ans
ibleDoc to learn how to actually use this.
Now one thing I will say here, you will recall in previous videos or rather
previous skills, we happen to use the Genji 2 templating language
and this allows us to effectively render an entire configuration file and then WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and this allows us to effectively render an entire configuration file and then
we could use something like the copy module
to copy those rendered configs over to the target machines.
Now quite honestly when we happen to be making many many changes to a
particular configuration, this is my preferred method.
It is much more comprehensive, it gives us access to programmatic logic that
type of thing, but it also can be quite time consuming
and honestly it can be a little bit difficult depending on just how complex the
configurations are.
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
And quite honestly, sometimes all you have to do is to make a simple change
within a configuration file, perhaps maybe one line has to change
or maybe a block of lines have to change that type of thing as opposed to
really having to render an entire configuration altogether.
So given the fact that we're now learning to explore AnsibleDoc, I thought this
would be a good time to introduce this particular module, "line and fail".
So with that said, let's go and check it out.
So once again, if we just list what we actually have here within our AnsibleDoc
, 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
, if I scroll on down, notice we have this one right here, "line and fail", and
it says here,
this is going to allow us to manage lines in text files.
So all I will do here is just grab this and copy here, press "Q" and I'll call
the screen, and then I'll just say " AnsibleDoc" and then paste in the name of
the module.
So as per the description, what it says here is that this module is going to
ensure that a particular line is in a file or replace a particular line and
existing line that is.
Notice the use case here, this is primarily useful when you want to change a
single line in a file only. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
single line in a file only.
So like I say, as opposed to rendering the entire configuration via Ginger2,
this module can help us simplify that process.
So let's scroll on down, and we can see all of the available options once again
.
Notice we have options such as "insert before", so the line is going to be
inserted before the last match of the specified regular expressions.
Once again, this is what we're going to be utilizing.
So say for example, you have the option "insert before", pretty much what we're
going to do is to make use of a regular expression to determine a particular
pattern within the file, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
pattern within the file,
and this particular option is going to insert whatever text you select or
rather provide before the last match of that regex.
So like I say, you can explore this at your leisure, we have many, many options
here.
Notice this one right here, we have the "equal sign" and the "path" option.
This again tells us that this is a mandatory parameter, and pretty much this is
going to allow us to select the file that we want to target.
Notice this option here "state" as well, this is whether a line should be there
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
or not, so we have the option to specify should the line be absent or should
the line be present.
So if we want to remove a particular line, we can specify "absent" or if we
want to ensure it's there or we can set it to "present".
Notice we also have this other option here "redjxp" and it's via this
particular parameter that we can provide the regular expression for the pattern
matching.
So let's go and check this out then.
So what I'm going to do here is let me go into my blue box and all I will do is
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
go into my temp directory and I'm just going to create a file called "names.txt
".
So we go into insert mode, I will say "James", "Somonor", "John", "Knox", "K
ieth", and if I just press "skate colon wq" to write and quit, here is the file
right here.
And what I want to do is I want to target this particular file and I'm going to
remove a particular line from this file, in this case here, I'm going to remove
my name "John".
Now of course this could be applied to a complex configuration file, maybe it's
a DNS mapping, whatever it may be, but in the interest of simplicity, I'm just
highlighting the name of the file. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
I'm just highlighting the most simple case right here, so our basic text file.
So what I'm going to do here is I'm going to create a playbook called "play5.y
ml".
Going to insert mode, have our three hyphens at the top, you know the drill.
And we give it a name, "play5", "test the line in file module".
Who am I going to target? Well I could target both devices if I so wish but in
my case here, I'm just going to target the blue box and I will set "gather
facts" to false to keep things in this display.
And now I will define my tasks, so the name here, I'll just say "remove name
from text file". WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
from text file".
Now what is the actual module I'm going to be using? It's going to be "answer
able built in", "line in file".
And we know the path parameter is "mandatory".
So the path I want to target is going to be the temp directory and the file is
going to be called "names.txt".
Now what I want to do is I want to specify the states and I want something to
be absent.
Now what exactly do I want to be absent? What am I trying to match here? Well I
can specify my Reg XP parameter.
And all I'm going to do is to provide some regular expressions. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And all I'm going to do is to provide some regular expressions.
Now this here is not going to be a course on regular expressions.
You can look online to see how we can actually format these things.
So basically I have my carrot to denote my start anchor and my dollar sign to
denote my end anchor.
Pretty much all I'm doing is saying that I want to match the word "John"
exactly.
So quite simply the module is going to search for this particular pattern "John
".
And if it finds it it's going to remove it because the state should be absent.
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
So let's try this out then. And also "answerable playbook", "play5.yml" and "t
it" "enter".
Give it a moment and now we have a change. So if we go to verify this, we go
back to the blue box.
Let's cat the names. Once again, look at this right here.
"John" has actually been removed from the list of names.
Similarly, let's say we wanted to add a particular line in a file.
Let's go in to play5 once again. And I'll create a new task.
And I'll just say "adding name to text file".
What I'm going to use, I'm going to use the "answerable built-in" line and file
module once again.
The path will be targeting the same text file. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
The path will be targeting the same text file.
Now I'm going to specify the line that I want to add. So let me add the name "
La La" here.
And if you look at the documentation, we also have this option to create.
And all I will do is specify the value "yes".
So if I just save this right here. And I read on the playbook, give it a moment
.
And check this out. Now we have made a change once again to the blue box.
So let's cat the same file. Once again, we have successfully added "La La" to
the list of names using the line in file module.
So as you know, we have a lot of available modules within "answerable". WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So as you know, we have a lot of available modules within "answerable".
We've just seen a few more within this particular skill.
And remember the best place to find out about how to use these modules is
directly via the documentation.
And we can access that documentation directly on our terminal utilizing "answer
able.com".