In this video, we will create a directory and file inside of the directory and at the end we will remove
that file through our playbook.
So let's go right into it.
We'll go and log into our control node and we'll create a file, a YAML file, which will create it
as a CR for change or create or remove file YAML.
All right, so let's go over our entire syntax or parameters that we will be using inside of our YAML
file.
So very first thing is we'll define that it's a YAML file by putting dash, dash, dash then comes in
the name of the playbook, create and remove file.
All right, host, where are you going to run this playbook against?
So I'm putting it all meaning it is going to Etsy as a host file and whatever the client information
in there, it will send it to all of those clients.
Now in our lab environment, we only have one client and we have added that into our host file, so
it will run it against our client one All right, then the task are what do you want this playbook to
do?
So very first task is, and the name of that task is to create a directory, the directory.
And for that we will use the file module.
So you either create a file or a directory, you will have to use a same module which is file.
Where do you want to create the directory?
Specify the path, the location.
We wanted to create it in a slash temp and the name of the directory is Seinfeld.
All right.
Who is or who is going to be the owner of that directory?
I want to be the owner.
So I'm just putting in I have Zal, who's the group owner.
I have all the same thing.
These are the permissions of that directory.
770.
Then the state is going to be a file or is it going to be a directory?
So we want it to be a directory.
So that's what you have to specify it.
All right.
Once that is created, the next task within the same main task is to create a file in that same directory
that we created.
So we will use the same module.
And module is the file module path, same way we did it for directory.
We have to specify the path, slash, temp, slash Seinfeld and the name of the file.
And this time it's going to be Jerry.
All right.
So this is to create a file.
Now, the state is not a directory, so we just use for the file.
We'll just use a touch.
All right, next step.
The third task within this task is start the new file, Jerry.
So, meaning we have to check and confirm whether that Jerry file has been created.
So we'll use the module stat and the path is which file do we want to start?
It's a file we created in the earlier tasks Temp, Seinfeld and Jerry.
Okay, so we once we check it, we cannot just really show it on the screen or Ansible or Playbook cannot
show it on the screen, but it has to do it has to put it into some kind of container.
So you will use a registered parameter and the name of that variable or the name of container.
Anything of your choice.
I'll just put Jeff's for Jerry file.
All right.
So this to put the result in the container, now show file status.
Now we have to read that container and we'll use a debug module for it.
And for that we'll use message and the name of the message will be file, status and attribute and it
will read from that container that we specified in a register.
And that is.
All right.
So print container or variable contents.
All right.
And the last thing that we want to do in this entire task is to remove that Jerry file.
So we'll use the same file module to remove to create anything we need to do.
We need to use the file module and we'll use the path which file or where is that file located and the
state.
What do you want it to do now?
You want the state to be, not touch, but instead absent.
So it will remove it.
This will delete that file.
All right, so this is how we're going to create our playbook.
I'll go ahead and log into my control node.
As you see, I have will be creating in my control node.
So right here.
My control node.
As the control node is here, I'm going to log in as root.
All right.
I already have a.
A terminal open.
Sorry, and I am logged in as who am I am logged in as route.
I am an Etsy as well playbook directory.
That's where I keeping all my playbooks.
So it depends.
Where are you keeping or where your company is keeping it?
You should go by the organization standards anyway.
So here I have already created the YAML file to save some time.
So what I'll do is I'll just do vim see or file yaml.
All right.
So as you see, let's start from the top.
We defined that it is a YAML file the name create and remove file host on all.
So make sure when you are running against all the host that is inside of your Etsy Ansible host directory,
that host or that client is up and running.
So for that, let's see if I have a go to file a new tab and I'll do Cat at C and Sybil and Host.
This is my host file.
Let me move up and I notice that I have defined only one host.
So 10 to 53 1.115.
So make sure you can ping that.
So that is ping 10.2 53.1 .115.
Yes you are.
It is payable great.
I also have a client up and running, so if you do not have the client up and running, you should make
sure your client is there.
So right here I have my client.
So let me go in.
And right here, this is my client.
All right.
So let me go in back to the control node.
And the control node is.
Right here.
My control node right here.
All right.
So clear the screen back again.
And I'm going to go back to my file, the file that I am editing.
All right.
Coming down the task, what task do you want to create?
The first task is create a directory.
We specified all of that.
Make sure the indentation is defined.
Clearly, indentation is the key.
If you have the wrong indentation anywhere, it will not run.
All right.
Name.
Create a file in that directory.
The file, the module is file.
Then we using the stat, then we using a debugger to message the output and then we are doing remove
file.
So I have everything in there.
And by the way, I have already created this file for you in the notepad plus, plus and I am going
to upload that with in the handouts directory.
All right.
So anyway, let's save that now.
Let's go ahead and run it.
So we will use the command ansible dash playbook and we will going to specify the YAML file.
So while I'm going to run it, I'm going to have this one just to make sure my client is there and it's
going to create it in the temp directory and I'm going to run that.
I'm just going to do ls minus l tr And you see right now in that temp directory there is no sign file
directory.
All right.
So going back to.
My.
Control load.
Right.
Here's a control node and let's go ahead and run it.
All right.
So it's running it, creating and removing the file.
This is the main play.
And the task is to go into this client and stat let's go up, create a directory.
It did create a file in the directory.
It did change.
Start the new file.
It did the result of the stat came back.
It's with all the attributes.
Then remove the file from that directory.
It did and everything worked perfectly fine.
Change three.
All right.
So let's go to our.
Client.
And we will see.
The directory is there, but we will not see.
They'll file because it created the file and it removed the file.
So let's do ls minus later in the temp directory.
I'm in temp.
So there you go.
There is the Seinfeld directory owned by I of XL and group by I Absol and this is the permission that
you specified.
And if I go into the Seinfeld directory and do LS minus l tr you'll see nothing in there.
So it created the file and it removed it.
So if you go back to the control node, you'll notice that the file is right here, created the path
here.
Temp Seinfeld, Jerry and it was created, but then of course it was deleted.
So that's how you create a file state file, check the status of the file if it is there.
And also you can run certain condition based on the status of the file.
For example, if the file existed, do that.
So you could put that conditions which you could learn later on.
But anyway, this is just a very simple way of creating file directory and removing files.
And that was the purpose of this video that you will learn how to manipulate your file system structure.