Ansible Copy Module Tutorial with Examples

Ansible is a valuable automation tool that enables IT professionals to manage infrastructure more consistently and efficiently. One of the key features of Ansible is its copy module, which is straightforward yet powerful, allowing for the copying of files from local to remote systems. If you want to take your…

Ansible is a valuable automation tool that enables IT professionals to manage infrastructure more consistently and efficiently. One of the key features of Ansible is its copy module, which is straightforward yet powerful, allowing for the copying of files from local to remote systems.

If you want to take your Ansible skills to the next level, consider enrolling in our Ansible course at the link below!

Course: Ansible Training for Beginners

What is Ansible? Ansible is an easy to learn automation tool that streamlines complex IT tasks with …

57 Lessons
6 Quizzes
8 Labs
12 Hr

This post will explain how to use the Ansible copy module, covering everything from basic syntax to advanced usage options.

The Ansible Copy Module

The Ansible Copy Module is a tool used in Ansible to transfer files from the control machine to remote hosts. It allows users to specify the source file or directory on the local machine and the destination path on the remote system. This module is useful for copying configuration files, scripts, or any other necessary files to ensure that the target systems are configured correctly and consistently. It also supports features like setting file permissions and ownership, making it a commonly used module for automating file distribution with Ansible.

In the screenshot below, you can see an example of the copy module being used to copy a web page index.html to a remote web server.

image 1
Ansible Copy Module

How to use the Ansible Copy Module

The basic syntax for the Ansible copy module looks something like this:

- name: Copy text file to Ansible Server
  copy:
    src: text_file.txt
    dest: /home/iacadmin/text_file.txt

The copy module also comes with a variety of options to customize its behavior. For instance, you can set permissions, change the owner and group of the copied file, and even decide whether to overwrite existing files.

Here’s a table with the top 10 most common arguments for the Ansible Copy Module and their use cases:

ArgumentUse Case
srcSpecifies the path to the source file or directory on the local machine to be copied.
destDefines the destination path on the remote host where the file or directory will be copied.
modeSets the file permissions using symbolic modes (e.g., 0644 for read/write permissions).
ownerSpecifies the owner of the copied file on the remote host.
groupDefines the group ownership of the copied file on the remote host.
backupIf set to yes, creates a backup of the destination file before overwriting it.
forceForces the copying of files even if the destination file already exists and is identical.
contentAllows specifying the content directly instead of using a source file, useful for small files.
remote_srcIndicates that the src path is on the remote host instead of the local machine.
validateRuns a validation command on the destination file before copying, ensuring file integrity.
Ansible Copy Common Arguments Table

These arguments help customize and control the file transfer process, ensuring it meets specific requirements and maintaining system consistency.

Copying Files from Local to Remote with Ansible Copy

Automating the transfer of files from your workstation to remote servers is a breeze with Ansible’s copy module. Here’s a straightforward example of how to execute this task. Let’s say you have a configuration file that needs to be updated across several servers. The following Ansible task does exactly that:

- name: Copy a single file with Ansible
  copy:
    src: /path/to/local/config.yml
    dest: /path/to/remote/config.yml

This task picks up config.yml from your local directory and places it into the specified path on the remote machine.

Copying Files from Remote to Local with Ansible

To copy files from remote servers to your Ansible controller is simple with Ansible’s fetch module. Here’s a straightforward example of how to execute this task. Let’s say you need to retrieve a log file from multiple servers. The following Ansible task does exactly that:

- name: Fetch a single file with Ansible
  fetch:
    src: /path/to/remote/logfile.log
    dest: /path/to/local/logfile.log
    flat: yes

This task fetches logfile.log from the specified path on the remote machine and places it into the designated path on your local system. The flat parameter ensures the file is copied without creating additional directories.

Setting File Owners, Groups, and Permissions with the Ansible Copy module

When it comes to managing files on remote servers, it’s important not just to place them in the right location but also to ensure they have the appropriate permissions, ownership, and group settings. The Ansible Copy module provides granular control over these attributes.

To set the owner, group, and permissions of a file you’re copying, you can use the owner, group, and mode options respectively. Here’s a practical example:

- name: Copy file and set permissions with Ansible
  copy:
    src: /home/iacadmin/ansible/application.properties
    dest: /opt/serveracademy/application.properties
    owner: webadmin
    group: webgroup
    mode: '0644'

In the task above, application.properties is transferred to the remote server. Upon arrival, the file’s ownership is set to the webadmin user, and it’s associated with the webgroup group. The mode option specifies the file’s permissions—in this case, 0644 means that the file is readable by anyone, but only writable by the owner.

Copying Entire Directories with Ansible Copy

You can copy an entire directory by setting the src and dest to something like that shown below:

- name: Copy file and set permissions with Ansible
  copy:
    src: /home/iacadmin/copy-directory/
    dest: /tmp/copy-directory/

Adding a trailing slash, like dir/ means that only the specific directory and it’s immediate children will be copied. If you remove the trailing slash (dir), then the directory will be copied recursively.

If path is a directory, it is copied recursively. In this case, if path ends with “/”, only inside contents of that directory are copied to destination. Otherwise, if it does not end with “/”, the directory itself with all contents is copied. This behavior is similar to the rsync command line tool.

Conclusion

If you’re looking to expand your Ansible knowledge and explore more advanced topics, consider enrolling in our Ansible for Complete Beginners course that will take you from Zero to Hero in Ansible automation!

CREATE YOUR FREE ACCOUNT & GET OUR

FREE IT LABS

profile avatar

Paul Hill

Paul Hill is the founder of ServerAcademy.com and IT instructor to over 500,000 students online!