Javatpoint Logo
Javatpoint Logo

Ansible Copy

Ansible provides the functionality of copying the files and directories with the help of copy and fetch modules. The copy module is versatile.

The copy module is used to copy files and folders from the local machine to the remote servers. And the fetch module to copy data from the remote machine to the local machine.

If you want to copy files after substituting with variables, such as config files with IP changes, or you can use the template module also. You can perform a lot of complicated tasks with this module.

Copying Files from Local to Remote

The copy module is used to check the file set in the src parameter, on the local machine. And then, it will copy the data to the remote machine path specified in the destpath.

In the below example, we will copy the sample.txt file in the home directory of the local machine, to the destination is the /tmp directory on the remote server. as long as we are not specifying any permission for the file, the default permission for the remote file is set as -rw-rw-r-(0664).

Case 1: If the file is already present on the remote server, but the source file's content is different, then the destination file will be modified. You can control this by setting the force parameter. The default is set to yes. So it modifies the file by default.

If you don't want the file to be modified, if the source file is different, then you can set it No. The following task will copy the file if the file does not exist on the remote server.

Case 2: If the file did not found on the local machine, the Ansible throw an error.

For example: fatal: [remote-machine-1]: FAILED!=> {"changed": false, "failed": true, "msg": "unable to find '~/sample.txt' in expected paths."}

Copying Directories from Local to Remote

You can also copy folders or directories using the Ansible copy module. If the 'src' path is a directory, then it will be copied recursively. Or the entire directory will be copied.

There are two different variations for this task. Depending on whether you have the '/' character at the endpoint of the 'src' path or not.

The first method will create a directory on the remote server, with the name set in the src parameter. Then, it will copy and paste the content of the source folder into that directory.

If you want this behavior, then it doesn't give the '/' after the path in the src parameter.

in the below example, it will first create a directory named copy_dir_ex in the /tmp of the remote server.

Copying Files between Directories on Remote Machine

Ansible copy allows you to copy the files from one directory to another on the same remote machine. But this is only for files, not for the directories. You can use the remote_src parameter to let Ansible know your intentions.

The below code will copy /tmp/test.txt to the home directory of the user (/home/[username]/).


Next TopicAnsible Command




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA