Ansible Copy Module

1 Comment

Ansible Copy Module

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.

Copy the file with force yes-Ansible Copy Module

 

by using this module the 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. so 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

so it will not force.

by doing this if the file 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 Module

let us assume 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 Linux directory to the remote server.

 

by using above code you can send Linux directory from ansible machine to a remote 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 Module

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

The same example as above, but using a symbolic mode equivalent to 0644

You can use any type of mode both are same.

Copy Content to Remote server-Ansible Copy Module

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. so by using this module, we can create the file with content.

you can refer in ansible documentation https://docs.ansible.com/ansible/2.3/copy_module.html

 

[top]
One Response to Ansible Copy Module
  1. Mollie Bodden at Reply

    I agree with your details , excellent post.


[top]

Leave a Reply

Your email address will not be published. Required fields are marked *