Skip to Content

7 Ansible Copy Module Examples to Copy File to Remote Server

The copy module in Ansible is used to copy files or directories from the local machine to a remote machine. It can also copy files between remote machines.

The syntax for the Ansible copy module is as follows:

- name: Copy file to remote machine
  copy:
    src: /path/to/local/file
    dest: /absolute/path/on/remote/machine
    [optional parameters]

The copy module takes the following parameters:

Parameter Description
src This is the source file or directory path on the local machine.
dest This is the destination file or directory path on the remote machine.
backup If set to yes, this parameter creates a backup file of the original file on the remote machine.
checksum If set to yes, Ansible uses a checksum to compare the source and destination files to determine if the files are the same. If they are not the same, the file is copied.
force If set to yes, Ansible will overwrite the destination file or directory, even if it already exists.
mode This sets the permissions on the destination file or directory.
owner This sets the owner of the destination file or directory.
group This sets the group of the destination file or directory.

 

We will explore several examples in this article.

Copy file to a remote machine with Ansible

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 the destination location in the remote server, it will replace with the file from ansible machine.

Blindly it will copy the file from ansible machine to the remote server.

- name: copy file from local host to remote host (relative path, ./files/)
  copy:
    src: devops.txt
    dest: $HOME/devops.txt

Here the devops.txt file is  a relative path. The relative path is a file or directory path that is expressed in relation to the current working directory. The current working directory is the directory you are currently in when you enter commands in the terminal.

For example, if your current working directory is /home/user/documents and you want to reference a file located in the directory /home/user/downloads, you can use a relative path to reference the file. The relative path would be ../downloads/filename.txt, where .. represents the parent directory of the current working directory.

Copy file with absolute path to a remote machine with Ansible

The copy module takes two parameters in this example:

  • src: This is the source file path on the local machine. In this example, the file is located at /path/to/local/file.
  • dest: This is the destination file path on the remote machine. In this example, the file will be copied to /absolute/path/on/remote/machine.

 

- name: Copy file to remote machine
  copy:
    src: /path/to/local/file
    dest: /absolute/path/on/remote/machine

In this example, we use the absolute path as the file path. The absolute path specifies the complete path to a file or directory, starting from the root directory /. An absolute path always starts with a forward slash (/) and specifies the full path to the file or directory. 

Copy multiple files to remote destination with Ansible copy module

To copy multiple files to a remote server using Ansible, you can use the copy module within a loop. Here’s an example playbook task:

- name: Copy multiple files to remote server
  hosts: my_server
  tasks:
    - name: Copy files
      copy:
        src: "{{ item }}"
        dest: "/path/on/remote/machine/"
      with_items:
        - /path/to/local/file1
        - /path/to/local/file2
        - /path/to/local/file3

In this task, replace my_server with the name of your remote server or a group of remote servers in your inventory. The with_items parameter specifies a list of files to copy to the remote machine.

In this example, we are copying three files: file1, file2, and file3. You can add or remove files from this list as needed.

To learn how to use list in Ansible, you can refer to this article: Ansible Lists 101: A Beginner’s Guide to Managing and Manipulating Data

The src parameter is set to {{ item }}, which is a reference to each item in the with_items list. This allows the copy module to iterate over the list of files and copy them one by one.

The dest parameter specifies the destination directory on the remote machine. In this example, we are copying the files to the directory /path/on/remote/machine. The item reference is used again to append the name of each file to the destination directory path.

When this task is executed, Ansible will copy each file from the local machine to the remote machine. You can use the loop keyword instead of with_items in newer versions of Ansible (2.5 and later).

Note that in older versions of Ansible (prior to 2.5), you must use with_items instead of loop. If you are using an older version of Ansible, the example in my previous answer would be the correct syntax.

Copy multiple files to multiple destinations with Ansible

This is an Ansible playbook task that uses the copy module to copy multiple files from a local machine to a remote machine. The files are specified in a list of dictionaries using the with_items parameter. 

  • The source and destination paths are dynamically generated using the with_items directive, which loops through the list of dictionaries containing the src and dest key-value pairs.
  • For each iteration of the loop, the src and dest paths are passed to the copy module using the src and dest directives, respectively.
  • Sets the ownership and permissions of the copied files using the owner, group, and mode directives.

 

with_items: A list of dictionaries that define the files to be copied. Each dictionary has a src key that specifies the source file path on the local machine, and a dest key that specifies the destination file path on the remote machine. In this case, there are three dictionaries, each specifying a different file to copy.

- name: Copy some files to /etc/myapp/
  copy:
    src: "{{ item.src }}"
    dest: "{{ item.dest }}"
    owner: root
    group: root
    mode: u=rw,g=rw,o=r
  with_items:
    - { src: app1.conf, dest: /etc/app1/ }
    - { src: app2.conf, dest: /etc/app2/ }
    - { src: app3.conf, dest: /etc/app3/ }

Copy files between locations on the remote host with Ansible

Ansible copy module allows us to copy the files from one directory to another on the same remote machine. But this is only for files, not for the directories. we can use the remote_src parameter to let Ansible know our intentions.

 

- name: Copy file between locations on the remote host
  copy:
    src: "{{ ansible_env.HOME }}/test_file"
    remote_src: true
    dest: "{{ ansible_env.HOME }}/test_file2"

In this code, the copy module is used to copy a file on the remote host from one location to another. The src parameter specifies the source file path, and is set to $HOME/test_file using the ansible_env.HOME variable.

The remote_src parameter is set to true to indicate that the source file is located on the remote host. The dest parameter specifies the destination file path, and is set to $HOME/test_file2 using the ansible_env.HOME variable.

Note that the variable references in the src and dest parameters are enclosed in double curly braces.

Copy Files between local and Remote Machine with one ansible command

We can also use command line to copy files between local and Remote Machine. The below command will copy test.txt to /tmp/test.txt on destination server.

ansible test_node -i inventory -m copy -a "src=test.txt dest=/tmp/test.txt"

  • -m copy: This option specifies the name of the Ansible module to use, which in this case is the copy module.
  • -a “src=test.txt dest=/tmp/test.txt”: This option specifies the module arguments, which in this case are the source and destination paths of the file to be copied.

 

 we can use the following command to verify the content of the file on the remote host.

ansible test_node -i inventory -m shell -a "cat /tmp/text.txt"