Table of contents
1.
Introduction
2.
Ansible Copy
3.
Synopsis
4.
Parameters
5.
Copying Files from Local to Remote
6.
Copying Directories from Local to Remote
7.
Copying Files between Directories on Remote Machine
8.
Frequently Asked Questions
8.1.
Does Ansible Copy utilize SCP?
8.2.
How does copy ansible work?
8.3.
Does Ansible copy overwrite?
8.4.
Is Ansible copy idempotent?
9.
Conclusion
Last Updated: Mar 27, 2024

Ansible - Copy

Author Aditi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Infrastructure as code is made possible by the Ansible toolkit. The open-source Suite contains Software provisioning, configuration management, and application deployment features. The copy module transfers a file from one computer to another. These computers can either be local or distant. Fetch is a built-in module in Ansible. It copies files from remote places to a local box. Use the ansible if you require variable interpolation in copied files.

Let's dive into the article for more information about the Ansible copy module.

Ansible Copy

With the copy and fetch modules, Ansible offers the ability to copy files and directories. The copy module has many uses. The copy module sends files and directories from a local computer to distant servers. Moreover, the fetch module transfers data from a remote machine to a local machine.

You can also try the template module if you want to duplicate files. These files can have variables substituted, like configuration files with IP changes. With the help of this module, many challenging jobs are possible.

ansible copy img

Synopsis

  • The copy module sends a file between local and distant computers or from one computer to another.
  • Use the fetch module to copy files from distant places to the local box.
  • Use the template module if you require variable interpolation in duplicate files. Unpredictable results will be produced if a variable is used in the content field.
  • Use the win copy module for Windows targets instead.

Parameters

parameter table 1
parameter table 2

Copying Files from Local to Remote

The file specified in the src parameter is verified on the local machine using the copy module. The data will then be copied to the distant machine path indicated by the destpath.

The following example will copy the "sample.txt" file from the local machine's home directory to the remote server's "/tmp" directory as the destination. The remote file's default permission is set to -rw-rw-r- as long as we don't specify any further options (0664).

- hosts: blocks  
  tasks:  
  - name: Ansible copy file to a remote server  
    copy:  
      src: ~/sample.txt  
      dest: /tmp  

Case 1: The destination file will be updated if the source file's content differs from the destination file and the file is already available on the remote server. By adjusting the force parameter, you may manage this. Yes is the default setting. Thus, by default, it updates the file. 

If the source file is different and you don't want the file edited, then select No. If the file is missing from the remote server, the following operation will copy the missing file.

- hosts: blocks  
  tasks:  
  - name: Ansible copy file force  
    copy:  
      src: ~/sample.txt  
      dest: /tmp  
      force: no  

Case 2: Ansible will throw an error if it can not locate the file on the local system.

Copying Directories from Local to Remote

The Ansible copy module also allows you to copy directories or folders. If the 'src' path is a directory, it will be copied recursively. Or the directory will be copied in its entirety. For this task, there are two distinct variants. Whether or not the '/' character is present at the 'src' path's conclusion.

The first approach will create a directory with the name specified in the src parameter. The creation happens on the remote server. The content of the source folder will then be copied and pasted into that directory. If you desire this behavior, leave off the src parameter '/' after the path.

In the following example, it will initially create a directory called copy dir ex in the remote server computer's /tmp directory.

- hosts: blocks  
  tasks:  
  - name: Ansible copy the directory to the remote server  
    copy:  
      src:/Users/mdtutorials2/Documents/Ansible/copy_dir_ex  
      dest:/Users/mdtutorials2/Documents/Ansible/tmp  

Copying Files between Directories on Remote Machine

You can use Ansible copy to move files within the same remote computer from one directory to another. But directories are not included in this; only files are. You can use the remote src parameter to communicate your goals to Ansible. The code below will move the file /tmp/test.txt to the user's home directory (/home/[username]/).

---  
-hosts: webservers  
tasks:  
-name: copy the file between directories on a remote server  
copy:  
src: /tmp/test.txt  
dest: ~/test.txt  
remote_src: yes  

 

Check out this problem - Duplicate Subtree In Binary Tree

Frequently Asked Questions

Does Ansible Copy utilize SCP?

The copy (scp) module would work just fine if you wanted to copy a file from an Ansible Control Master to distant hosts.

How does copy ansible work?

The copy module sends a file between local and distant computers or from one computer to another. Fetch is a built-in module in Ansible. It copies files from remote places to a local box. Use the ansible if you require variable interpolation in copied files.

Does Ansible copy overwrite?

The default behavior of the Ansible copy module is to overwrite any existing files. Then it performs a forced copy to the destination.

Is Ansible copy idempotent?

The contents of files will be copied continuously. It indicates that Ansible is not working idempotently.

Conclusion

In this article, we have extensively discussed the Ansible Copy module. We have also explained the parameters, how to copy files from local to remote, how to copy directories from local to remote, and more in detail.

We hope this blog has helped you enhance your Ansible Copy module knowledge. To learn more, check out our articles on Ansible Interview Question Part-1Part-2, and Part-3. Practice makes a man perfect. To practice and improve yourself in the interview, you can check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Do upvote our blog to help other ninjas grow. Happy Coding!

Thank You img
Live masterclass