Hi.
In this video we are going to learn how we could create a file and add text or content inside of a file.
So we will log into our control node and we will create our playbook and we are going to name the playbook
as add a text file.
Of course you could pick any name of your choice.
Now what goes inside of the playbook?
And as you know, very first thing is be defined that it's a YAML file.
And then we start with the name of the playbook, create a file and add text.
Then the host Where do you want it to run against or where do you want to create that file?
At this for this video, we will create a file to our local host, meaning we will create on the control
node where we running our Ansible.
All right, good.
Now let's go into the task what exactly you want to do.
So very first thing we want to do is, of course, to create a file, and the second is to add text.
So these are the two tasks we wanted to do, and we could put them both under the same one task.
So we'll define the tasks.
The very first task and the name of that is to create a new file.
And what is the module we should be using to create a file?
It should be the file module, yes.
Then you need to define where are you creating and what will be the name of the file.
So we're creating in slash temp and the name of the file will be Jorge.
All right.
And that's the location and the name of the file, then the state.
What do you want to do with the file?
I mean, if it exists, do you want to delete it by putting in state equal Epson or state to be touch
because you want to create it.
So in this case we are creating it, so we need to use touch.
All right, then the next task, but in this play is to add text to the file.
So now file is created.
Now we need to populate some text into the file.
And by the way, this is very useful.
Let's say if you want to update your ETSI host file your MTP configuration file or your HTTP dx configuration
file, and you have to update that to, let's say 100 different servers.
So you don't want it to log into those hundred servers and update or add some new information to that
file.
So that's when you this.
A module lock in file comes to your rescue.
This is the one that actually helps you add texts to your files.
All right, then the module needs to know where your file is located, and we define the path.
Our file is in temp directory and the name of the file is short.
So let's say if you are updating at sea host file, then here you would put in here or any configuration
file.
Now the block.
The block is the parameters that you need to use to put in what exactly that you wanted to add into
it.
I'm just adding random text to the file because George.
George is one of the main characters of Seinfeld show and he is Jerry's best friend.
And by the way, Seinfeld show is a sitcom and he is the best friend of Jerry who created the Seinfeld
show.
So it's just the text that I'm adding to it.
So let's say if you are adding into ATC host file and you want it to be added in a proper indentation,
you could define that proper indentation, but putting block call in space and then putting pipe.
And then you could enter, hit, enter and put down the exact way that you want like IP and hostname.
All right.
So then once the file is created, these are the two tasks that we wanted to create in this play.
Then what happens?
Of course, text, add text to a file, and we run the Ansible playbook and it will create the file
and add text.
So let's go into our control node and let's create the file, which, by the way, I have already created
for you to save some time.
And we'll go over it and then we'll run it.
The my.
Aspen.
Lord is right here and I have already I'm already logged in as route.
Or if you are running as yourself or different user, just make sure you are logged in as that person.
All right.
Now let's go to our playbook that I created earlier.
Add text dynamo file.
And if you notice, I added everything exactly the same as I have it on my PowerPoint slide.
So local host task is to create a new file and then the file is the module, the path to define where
you want to create the file.
The state is to touch meaning create a file name is next.
This is the next task of the play.
Add text to the file and then will use block and file the name of the magic module path where you want
to add that text.
And what is the text is actually the block.
All right.
So that is saved.
Let's go ahead and run it and we'll run it as Ansible dash playbook and add text.
All right, create a file and add.
This is the name of the playbook.
Gathering facts create local host is going to the local host which is this is the own host changed to
perfect meaning it created the file and it added the text.
So how do we verify we could just simply do cat temp.
George This will tell me that the file has been created and the content of the file.
So the file is there definitely.
And here are the contents of the file.
And by default when you run or when you add text through Ansible block, then it will add these two
lines where it says Begin, assemble, manage block and then end as manage block.
But of course this has no effect on any of your file or configuration file because it has it starts
with LB So anything or any any line that start with LB it's actually the comment in a file and it has
no effect on the file.
So anyway, so now you know how to create a file or copy a file, and then you can add a text to a file.