Ansible Copy Examples Ansible Copy Module

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

tasks:
- name: Copy file to a remote machine
  copy:
  src: ~/devops.txt
  dest: /tmp

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

tasks:
- name: Copy file to a remote machine
  copy:
  src: ~/devops.txt
  dest: /tmp
  force: 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

tasks:
- name: Copy file to a remote machine
  copy:
  src: ~/devops.txt
  dest: /tmp
  force: no

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.

scripts

├── file1
├── file2
└── linux
   ├── file3
   ├── file4

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

tasks:
- name:  Copy a directory to a remote machine
  copy:
  src: /User/scripts/linux
  dest: /tmp

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.

├── tmp
└── linux
  ├── file3
  ├── file4

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/

tasks:
- name:  Copy files to a remote machine
  copy:
  src: /User/scripts/linux/
  dest: /tmp

the output in the remote tmp directory

└── tmp
   ├── file3
   ├── file4

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

- copy:
  src: /srd/myfiles/abc.conf
  dest: /etc/abc.conf
  owner: abc
  group: abc
  mode: 0644

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

- copy:
  src: /srd/myfiles/abc.conf
  dest: /etc/abc.conf
  owner: abc
  group: abc
  mode: "u=rw,g=r,o=r"

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

copy:
content: "Hello devops"
dest: /tmp/devops.txt

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.

 

Leave a Reply

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