Ansible is an open-source automation tool that allows developers to deploy and manage their applications with ease. One of the most important features of Ansible is its ability to copy files from one location to another using the "Copy" module. This module provides a simple yet powerful way to transfer files between hosts, making it a popular choice among developers for managing their infrastructure.

The Ansible Copy module offers several advantages over other file transfer tools. Firstly, it uses SSH, which ensures secure file transfers without requiring any extra configuration. Secondly, it supports various options like creating backups before transferring files, setting permissions and ownership on transferred files and folders, and excluding certain files or directories from being copied.

Ansible Overview

Ansible is an open-source IT automation tool that allows you to automate configuration management, application deployment, and task automation. It uses a simple, declarative language called YAML to define automation tasks and executes them on remote hosts over SSH or other transport mechanisms. Ansible uses a clientless architecture, meaning it does not require any agent software to be installed on the target hosts. Instead, it connects to them using SSH and executes tasks using modules.

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

What is Ansible Copy Module?

The Ansible Copy module is used to copy files or directories from the control machine (where Ansible is being run) to the target hosts. It provides a simple and straightforward way to transfer files while also allowing you to specify ownership and permissions for the destination files.

Here's the basic syntax of the Ansible Copy module:

- name: Copy file or directory


    src: /path/to/source

    dest: /path/to/destination

    owner: user

    group: group

    mode: permissions

Let's break down the parameters used in the module:

src: This parameter specifies the path to the source file or directory on the control machine.

dest: This parameter specifies the path to the destination file or directory on the target hosts.

owner: (Optional) It sets the ownership of the destination file or directory. You can specify either a username or UID.

group: (Optional) It sets the group ownership of the destination file or directory. You can specify either a group name or GID.

mode: (Optional) It sets the permissions for the destination file or directory. You can specify the permissions in octal format (e.g., 0644).

Here's an example of using the Ansible Copy module to copy a file:


Copy code

- name: Copy file


    src: /tmp/example.txt

    dest: /etc/example.txt

    owner: root

    group: root

    mode: '0644'

In the above example, the file /tmp/example.txt will be copied to /etc/example.txt on the target hosts. The ownership is set to root:root, and the permissions are set to 0644.


You can also use the Copy module to copy directories by specifying the source and destination directories. The module will recursively copy all files and subdirectories within the source directory.

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

Ansible Copy Module Usage:

The Ansible Copy module is used to copy files or directories from the control machine to the target hosts. To use the Copy module, follow these steps:

Define a playbook in YAML format and specify the tasks to be executed.

Use the copy module in the playbook to copy files or directories.

Specify the source (src) and destination (dest) paths.

Optionally, set the ownership, group ownership, and permissions for the copied files or directories using the owner, group, and mode parameters.

Here's an example playbook that uses the Copy module to copy a file:



Copy code


- name: Copy file to remote hosts

  hosts: target_hosts


    - name: Copy file


        src: /path/to/source/file.txt

        dest: /path/to/destination/file.txt

        owner: root

        group: root

        mode: '0644'

In the above example, the playbook will copy the file /path/to/source/file.txt to the specified destination path on the target hosts. It sets the ownership to root:root and the permissions to 0644.


Advantages of Ansible Copy Module:


Simplicity: The Copy module provides a straightforward way to copy files or directories, making it easy to manage file transfers in Ansible playbooks.

Declarative syntax: Ansible uses a declarative syntax, allowing you to specify the desired state of the system rather than writing complex procedural code. This makes it easy to understand and maintain playbooks.

Idempotent operations: Ansible playbooks are designed to be idempotent, meaning they can be run multiple times without changing the end state if nothing has changed. This ensures predictable and reliable results when using the Copy module.

Disadvantages of Ansible Copy Module:

Limited file transfer capabilities: While the Copy module is useful for basic file transfers, it may not be suitable for more complex scenarios such as synchronizing directories or transferring large amounts of data efficiently. In such cases, other tools like rsync or SCP may be more appropriate.

Requires SSH connectivity: The Copy module relies on SSH connectivity to transfer files to the target hosts. If SSH access is not available or limited, the module may not be usable in certain environments.

No support for Windows hosts: The Copy module is primarily designed for Unix-like systems and may not work seamlessly with Windows hosts. Ansible provides alternative modules like win_copy for managing file transfers on Windows systems.

It's important to consider these factors and choose the appropriate module or tool based on your specific requirements when working with file transfers in Ansible.

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


In conclusion, Ansible's Copy module is an essential tool for any system administrator or developer who needs to automate the copying of files and directories across multiple servers. With its simple syntax and powerful functionality, the Copy module can save time and effort by eliminating the need for manual file transfers. Additionally, it allows for precise control over file permissions and ownership, ensuring that critical data stays secure. Whether you're managing a small team of servers or a large-scale infrastructure, Ansible's Copy module is a must-have component in your automation toolkit. So why not give it a try today? Your workflow will thank you!

If you want to know more about Ansible Module, visit this blog Ansible Copy Module !