In this video we are going to copy files from our local host or ansible control node to our remote clients.
If you copying one file from one location or one host to another host, it's fairly simple.
You could just run a SIP command or any other command of Linux to copy from your local client to a remote
client.
But what if you have to copy that same file to multiple or hundreds of remote clients?
Then that becomes a lot of work.
So that's when you will use Ansible copy module to automate that task.
All right.
The first thing we'll do is we'll create some file.
We'll call it some dot cfg file or config file, and we'll put that file inside of home files all directory.
And that's just a random directory that I picked.
You can pick any directory of your choice.
Then we are adding some stuff, something that we are writing inside of that somewhat cfg file.
All right, once that file is created, then we will use the vim editor and we'll name the file copy
html.
Once we are in editor will define it with dash, dash, dash and which you will tell the system that
it's a YAML file and then the name copy file from local host to remote clients.
Or you could pick any name of your choice.
It's just a description of the play or playbook.
Then the host run this playbook against the hosts or all the hosts that are in ETSI Ansible Host file,
then the MTP line and then we have the task.
And if you pay close attention once again, you'll notice the task is exactly vertically aligned right
below the name and the host.
So that's the task and it's telling us run the following task or list of tasks, but we are running.
Only one task is to copy the files.
Now what is the name of that task?
Copying files.
All right.
And then become true.
That said, additional added thing that I added in here.
You don't have to add it.
But in case you wanted to run this as a different user, or if you are running it as regular user and
you wanted to run it as root, then you could use this option right now is telling you become true transfer
as a crunch user.
So if you logged in as root, it will transfer it as root.
If you logged in as regular user or transfer as a regular user.
All right.
Now becomes the most important part of the playbook.
The module.
What module are we going to use to copy it?
There is a copy module.
All right.
If you have that copy module, then of course it comes with other options and those options are source
as RC.
Just that that option is telling it.
Where is the file located?
And this is the source of the file.
Now, if you are defining the source, then of course one more thing.
You have to define the destination.
Exactly.
We are copying it from the Ansible control node from home.
I absol some dot cfg to temp directory that's the destination of the file owner.
Now it gives you additional information.
You don't have to specify all that.
If you want to transfer this route, it will transfer a route.
Now, if you want the file that you are transferring to have a certain permissions and ownership, you
could define it right here and you can put it in owner.
I want to transfer as I have, so that's fine group.
Okay.
I want to transfer as a group as well.
Excellent.
Now, here's another thing.
Yes, it gives you the mod or mod options, which will tell you if you wanted to change the permission
of the file.
So this will change the ownership and file permission of the file once it gets to the destination.
See how granular it becomes.
It goes down to details when it's going to copy the file.
Excellent.
Now we are going to create that playbook in our control mode.
We'll run it and we'll check in our remote client.
All right.
So we'll first go to our Ansible control node, which is right here.
Log in as route.
All right, we're logged in as route.
And let's move this down right here.
Let's go to our at sea and Symbol Playbooks directory.
Here we are going to create the vim file.
But before we do the vim file, let's put do echo some stuff.
And I'll put that to home.
I have all.
Some dos cfg.
All right, so you'll notice this file has been created in my home directory right here.
Some cfg.
And when I do cat.
Someday.
If you want to do cat it, is there some stuff?
Okay, let's go back to our Etsy playbook.
Our file is there.
That's clear the screen.
Sorry.
Now let's do him and copy the YAML.
Define that.
It's a YAML file.
Define the name.
Copy file from local host to remote clients.
Host.
All tasks.
Dash name.
Copying file.
This is what the task will do become.
Oops.
Sorry.
You notice one thing.
There has to be space right here, so be very careful.
Now when you hit enter, it will automatically do the indentation for you to put that vertically below
the end.
Or the name.
Now become.
True.
As the same user used the module copy.
Excellent.
Now once you define the module and hit enter, the system knows that this module has been added.
Now when the user hit enter, I will automatically do the indentation for him or her.
All right.
And the options source.
Slash home, slash eye of salt dash some dot cfg destination.
Just temp.
All right, owner.
Who do you want it to transfer it as?
I want to have it.
The ownership change to I absolve the group ownership change to I absol if I don't do it then it will
be transferred as route and the mode.
I want to change it to 0644, which is telling it that I want to have read permission for everyone and
only write permission for the owner of the file.
All right.
This is this completes our playbook.
All right.
Excellent.
Now, I will.
Before I run the playbook, I wanted to make sure that at Sea Ansible host which we have right here,
this is the host.
That's the only IPV have for our client.
I want to make sure I could ping that because I rebooted the system and my system remote client is running
on DHCP, so it might or its IP might have changed.
So I wanted to ping it first.
10.2 53.1 .115.
All right.
Perfect.
It means its IP is the same.
It did not change.
Perfect.
Let's clear the screen.
And now let's run our Ansible.
Playbook and we are already in playbook, so we don't need to define the entire absolute path for our
playbook.
So we'll just put in copy YAML and hit enter and it is telling us copy file from local host to remote
clients, which is the name right here, gathering task to which host change to this is the host IP
address that it found inside of the host file.
Player recap.
This is it.
Okay.
To done change.
Done.
It means it has completed its task successfully.
Now to confirm we will go to our.
Remote client.
Here is our email client.
And I'm going to log in as root.
And we asked the playbook to copy the file in the destination slash temp.
So we'll do CD slash temp and the moment of truth is let's do ls minus l tr.
And the file is right here, guys.
Some dos cfg or let's clear the screen.
You could just do ls minus l some dos cfg and it will tell you that it has been transferred.
Check the content cat, some de cfg and some stuff is in there.
Excellent.
Congratulations.
You have just completed your playbook in which you can copy files to so many of your remote clients.
Now you can copy to as many remote clients as you want if you have them.
And it's the same way.
If you wanted to move all file, you can use the MOV module and many of the things that you could do
with this module, you could check the list of options available to you from the documentation.