Ansible Copy Module

Ansible is a software tool which offers simple and powerful automation to cross-platform computer support. It was mainly designed for IT professionals, who use it for the deployment of applications, desktop and server updates, intra-service orchestration, configuration management, cloud provisioning and almost everything a system admin does every week or every day. Ansible does not rely on agent software and does not have any extra security infrastructure. Therefore it is easy to deploy. Since Ansible is related to automation, it needs instructions to complete every work. With anything written as a simple script, it is easy to make the version control.

Although Ansible can be at the cutting edge of automation, system administration and DevOps, it is also helpful to daily users. Ansible enables you to set up not only a single computer but potentially an entire network of computers at a time, and using it does not require any programming skill. Humans can read the Instructions written to Ansible. No matter if you are a computer novice or an expert, Ansible files can be easily understood. To copy files and folders from the local system to the remote servers, we use the copy module in Ansible. In this blog, let us go through Ansible Copy Module in detail.

Get ahead in your career by learning Ansible Course through hkrtrainings Ansible Online Training!

Ansible Copy Module:

Ansible is used to duplicate documents and indexes using both copy and fetch modules. The copy module is flexible enough. It is used to duplicate documents and organizers of the neighbouring machine to the remote servers. And, the fetch module is used to duplicate the information on the remote system to the local system. If you wish to copy files after they have been replaced by variables, like configuration files with IP address changes, you can also use the template module. The module enables you to perform many complex tasks.

How to Copy Files and Directories?

Copying Local Files to Remote Server with Ansible:

The playbook below will copy a file named sample.html of the Ansible control machine to the remote server, where we execute the playbook. Don't forget that Ansible is agent-free, so you don't need to install Ansible on a remote machine. You only require an SSH connection with the remote server.

-name: example to show copying a local file to remote machine
copy: 
src: sample_file
dest: $HOME/sample_file

It can also be executed as an ad hoc command in a single line.

Copying Local Directories to Remote Server with Ansible: 

We can copy Local directories to the remote server with the Ansible copy module. However, there are two different ways of copying the directory.

  • Type 1: Copy the content in the source directory (and not the directory)
  • Type 2: Copy the source directory and the contents of the directory
Copy the Content in the Source Directory (and not the directory):

Here, only the content of the source directory is copied into the destination directory. The content may be a directory, subdirectory or file. It is an ansible command for copying the content of a directory to the remote server.

ansible remoteserver -m copy -a “src=~/Desktop/Downloads/ dest=/files/applications/ owner: apache group: apache mode: 0644” -i ansible_hosts -b

Note that there must be a slash at the end of the src path. It belongs to the second type if there is no slash at the end of the source file.

We have the perfect professional Ansible Tutorial for you. Enroll now!

Playbook form of the AD HOC Command:

-name: copying the content of a directory (subdirectory or a file)
copy: 
src: ~/Desktop/Downloads/ 
dest: /files/applications/

Ansible Training

  • Master Your Craft
  • Lifetime LMS & Faculty Access
  • 24/7 online expert support
  • Real-world & Project Based Learning
Copy the Source Directory and the Contents of the Directory:

Here, the directory is copied to the destination directory along with the content. Ansible would be responsible for the creation of the new directory on the remote server. It is an ansible AD HOC command for copying a directory on the remote server.

ansible remoteserver -m copy -a “src=~/Desktop/Downloads dest=/files/applications/ owner: apache group: apache mode: 0644” -i ansible_hosts -b

Note that there must be no slash at the end of the src path. It belongs to the first type if there is a slash at the end of the source file.

Click here to get latest Ansible Interview Questions and Answers for 2022!

Playbook form of the AD HOC Command:

-name: copying the directory and the content of the directory
copy: 
src: ~/Desktop/Downloads/ 
dest: /files/applications/ 

How can we copy a file from the local server to a remote server with a group, owner and file permissions?

The parameters owner, mode and group, will give you precise control on the file permissions and ownership of the files that are created by the copy module. Keep in mind that you require 'become: true' if you set the owner or group to values that do not match ansible_user. The mode parameter allows setting file permissions as follows: 

  • An octal number such as 0755, 0644, 0400
  • A symbolic mode like u=rw, g=r, o=r.

Here g is the group, o is the other and u is the owner. We write r, w, x permissions that indicate r to read, w to write and x to run.

Octal Mode:

-name: copying a file from the local server to a remote server with a group, owner and file permissions
copy: 
src: sample_file
dest: $HOME/sample_file
owner: “{{ ansible_user}}”
group: “{{ ansible_user}}”
mode: 0644

Symbolic Mode: 

-name: copying a file from the local server to a remote server with group, owner and file permissions
copy: 
src: sample_file
dest: $HOME/sample_file
owner: “{{ ansible_user}}”
group: “{{ ansible_user}}”
mode: u=rw, g=r, o=r

Setting another owner with 'become: true'

-name: copying a file from the local server to a remote server with a group, owner and file permissions
copy: 
src: sample_file
dest: “/home/{{ansible_user}}/sample_file”
owner: “{{ ansible_user}}”
group: “{{ ansible_user}}”
mode: 0644
Become: true

Subscribe to our youtube channel to get new updates..!

How can we copy a file between remote server directories with group, owner and file permissions?

Octal Mode:

-name: copying a file between remote server directories with a group, owner and file permissions
copy: 
src: sample_file
remote_src: true
dest: $HOME/sample_file2
owner: “{{ ansible_user}}”
group: “{{ ansible_user}}”
mode: 0644

Symbolic Mode: 

-name: copying a file from the local server to a remote server with group, owner and file permissions
copy: 
src: sample_file
remote_src: true
dest: $HOME/sample_file
owner: “{{ ansible_user}}”
group: “{{ ansible_user}}”
mode: u=rw, g=r, o=r

Setting another owner with 'become: true'

-name: copying a file from the local server to a remote server with group, owner and file permissions
copy: 
src: sample_file
remote_src: true
dest: “/home/{{ansible_user}}/sample_file”
owner: “{{ ansible_user}}”
group: “{{ ansible_user}}”
mode: 0644
Become: true

How can we write plain text into a file on the remote server with a copy module?

We can write plain text in a file with the content parameter.

-name: write plain text into a file on the remote server
copy: 
dest: $home/sample_file
Content: "Hey you!"

How can we write templated text into a file on the remote server with a copy module?

We can write templated text in a file with the content parameter. It is good for single-line files, but if we want to write multiple-line templated text in a file, then we need to use the template module.

-name: write templated text into a file on the remote server
copy: 
dest: $home/sample_file
Content: “Hey {{ansible_user}}”

Ansible Training

Weekday / Weekend Batches

How can we test if a file is valid before we copy it to the remote server?

We can use the validate parameter to check if a file is valid on the remote server prior to copying the file in place. This parameter is strongly advised if a program can verify the validity of a target file.

%s in the validation string that indicates the file you want to check.

How can we back up a file when the copy should be overwritten?

The copy module, by default, will overwrite any file that already exists on the remote server. If we wish to backup the file prior to overwriting, use the backup parameter:

-name: copying file from a local server on the remote server with backup
copy: 
src: sample_file
dest: $home/sample_file
backup: true

Using 'backup: true', a copy will back up and add the timestamp prior to overwriting the file.

How can we copy multiple files with a loop?

We use the ‘loop’ keyword and {{item}} for copying multiple files.

-name: copying multiple files from local server on the remote server 
copy: 
src: sample_file
dest: “$home/sample_file_{{item}}”
register: copy_output
loop:
-1
-2

Now, let us revise the parameters:

Hosts: The target host group must already be set into the aka hosts file of the Ansible inventory.

Tasks: under this, all the tasks would be defined.

Become: it tells Ansible to run the corresponding task as user sudo root except if specified by another user with become_user.

Copy: name of the module that will be used for this task.

SRC: it indicates the path of the source file on the local computer on which ad-hoc command or the playbook is called. We can set Ansible for searching the file in the remote server using remote_src also.

Dest: it indicates the destination path of the host/remote server to which you want to copy the file.

Owner: it indicates the owner of the file on the destination server after it has been copied.

Group: It indicates the group of the file to the destination server after it has been copied.

Mode: setting file permissions after copying. 0644 would be defined as permission on the file rw- r-- r--

Conclusion:

In this blog, we have gone through the Ansible copy module. I hope this information is helpful. If you have any queries related to the ansible copy module, feel free to comment below.

Other Related Articles:

Find our upcoming Ansible Training Online Classes

  • Batch starts on 10th Jul 2022, Weekend batch

  • Batch starts on 14th Jul 2022, Weekday batch

  • Batch starts on 18th Jul 2022, Weekday batch

Global Promotional Image
 

Categories

Request for more information

Srivalli
Srivalli
Research Analyst
As a senior technical content writer for HRK tainings, srivalli patchava has a greater understanding of today's data-driven environment, which includes key aspects of data management and IT organizations. She manages the task of creating great content in the areas of software testing, DevOps, Robotic process automation. Connects with her on Linkedin and Twitter.