- In this video,
we will discover options to change file contents.
So how does it work?
Well, there is ansible.builtin.copy
by which we've talked before, which
you can use to create files with simple contents.
There's also ansible.builtin.replace,
which is used for simple replacements.
A lineinfile can be used to
manipulate single lines in a text file.
And finally, there's blockinfile.
Blockinfile is useful for
manipulating complete blocks of texts.
It offers a little bit more possibilities.
Let's talk about copy and replace.
Both of them can be used for simple operations.
You can use a content argument to the copy module
to copy your string into a destination file.
As in ansible all minus m copy,
content is hello, destination is TMP hellofile.
So that will create a file with the name hellofile,
with the content hello inside the file.
The replace module is going a little bit further.
It allows you to replace file contents
based on a regular expression.
It uses the regexp argument to identify the regular
expression that you want to replace.
And, replace, itself, is where
you specified the replacement text.
You can also just omit the replace argument
if you just want to remove text.
Completely different solutions are offered
by lineinfile and blockinfile.
You can use lineinfile to add a single line to a file.
And, also to determine where exactly
this file is going to be created.
And, you can use blockinfile to
add multiple lines to a file.
Let's go have a look at an example.
So the example is the file copied.yml
and you can find it in the files directly in
the get up repository.
In this file, there's a couple of modules I used.
So to start with, there is the task copy,
which is copying over the source, etc hosts,
to the destination TMP.
If you are using copy, then you will always copy
from the control host to the managed host.
So etc hosts is a file as it exists on the control host.
Next, this playbook is using blockinfile
to add some lines to the TMP hosts.
So, the path is TMP host and the block
specified is right here.
Notice the pipe behind the name of the module
which is making sure that the next couple
of lines is written to the configuration file
with this specific layout.
You can see this text is wrapped over multiple lines.
If you want to keep that, you need this pipe.
State present, ensures that these lines will be added.
Next, we are using this stat module.
I haven't mentioned the stat module before
but the stat module is a convenient module
that helps you to verify properties of files.
So, we are verifying the TMP host file
and the checksum algorithm is MD five.
And then, the stat model is mostly used
in combination with the register
because otherwise you don't see what it is doing.
So it registers a variable with a name result
and then it debugs the checksum of TMP host is a
result.stat.checksum.
And, finally we are using fetch to fetch a file.
So it fetches TMP hosts to the destination slash TMP.
Let's learn this playbook
and let's figure out what it is doing.
So let me use Ansible playbook on copy.
And there we go.
We can see all the work that it is doing.
The output itself is not revealing very much.
We can see the result of the debug command.
That's an interesting one because
that's what we've used the debug module for.
And then it fetches a file.
Now the interesting thing here is in fetch.
Let's check out the TMP directory because it was
instructed to fetch to the TMP directory.
There, you can see in the TMP directory, there's
an ansible1 as well as an ansible2 sub directory.
And in this ansible1 directory, what do we find?
Well, let's check it out.
Ansible1, where we can find a TMP sub directory.
That is what fetch is doing.
Fetch is maintaining the entire patent
as it exists on the target host,
and you will find the target host name
in the path on the control host as well.
And that is to allow you optimal control
to the different files that are fetched
from different hosts, because otherwise
how would Ansible ever see the difference
between the different hosts?
In the next video we are going to talk about find.
And I have a nice example of advanced file
manipulations in that video as well.