ansible copy module examples

Ansible Copy Examples

Ansible copy module is one of the modules in file modules in Ansible. Ansible copy module is used for copy the file from ansible machine to the remote server. With ansible copy module you can do various things let us see what we can do with ansible copy module. In this blog post we will see ansible copy modules examples and how to copy the files from ansible master to remote server.

Copy the file with force yes-Ansible Copy Module

In this example devops.txt file in the ansible machine will be copied to the destination location in the remote server. but if the same file(with the same name) already exists in destination location in the remote server, it will replace with the file from ansible machine. It will not take care of the file is existed or not existed in a remote location.  Blindly it will copy the file from ansible machine to the remote server.

by default, force is yes

So both above codes are work like the same. Mentioning force: yes or removing force: yes it depends on you. For better understanding you can mention force: yes.

Copy the file with force no-Ansible Copy Module

Here it will not force. if the file is already existed in destination location it will not replace the file. That means we are not forcing to copy the file from ansible machine to remote server.

Copy a directory from ansible machine to remote destination-Ansible Copy Examples

let us assume that we have a directory called scripts. In this directory we have two files called file1 and file2 and Linux directory, in Linux directory we have two files called file 3 and file 4.

Now the task is we have to send or copy Linux directory to the remote server.

By using above code you can send or copy Linux directory from ansible machine to a remote server location.

See at the end of src:/user/scripts/linux we have not mentioned (/) so that’s why Linux directory will copy from ansible machine to remote.

The output in the remote tmp directory.

If you mention (/) at the end of src, the files in Linux directory (file3 and file4) will copy to the remote.

see    src:/user/scripts/linux/

the output in the remote tmp directory

Only files in Linux directory will copy to the remote location if you mention (/) at the source end.

Copy the file with permissions-Ansible Copy Examples

By using the above code you can copy the file with different permissions.

It will work like same example as above. Here we mentioned permissions using a symbolic mode equivalent to 0644. we can use any type of mode.

Copy Content to Remote server-Ansible Copy Examples

By using this module you can write content and copy that content to the remote server. If the file has not existed in a remote location it will create that devops.txt  file and write that content to devops.txt. Here devops.txt is a filename you can give any file name. With ansible copy module, we can create the file with content.

If you want to know more about ansible copy,you can refer in ansible documentation.