Ansible

How to Copy Multiple Files in Ansible

Ansible is a nifty automation tool that enables the automation of various features. It grants full control to the user on their devices, whether they be local or remote. It comes jam-packed with multiple modules (around 750 – 800 of them), which all prove to be quite beneficial for users. The Ansible playbook is simple to write and it helps with performing the duties assigned to it like setting time for a certain job or pausing certain activities etc.

There are times when we may need to copy multiple files from one device to another. Ansible also provides various modules for this. These modules enable us to copy multiple files at the same time and send them over remote devices. That brings us to the subject of this article: the Ansible copy module, which we will be implementing in this guide.

Copy Multiple Files

In case we want to transfer multiple files, we need to make a playbook with numerous tasks for each copy. While this may solve the problem, it is not very time-efficient and can get quite tedious. Consequently, we would have a large playbook with more chances of human errors. It would also be more difficult to debug. Luckily, we can use several other approaches for solving this issue that are much more convenient and save our resources.

Using Loops

In general, Ansible is a simple automation tool that does not require full-fledged programming knowledge. However, if you have the basic know-how of how a source code works, it can be really helpful in Ansible and give you a deeper insight to solve various problems you encounter from time to time.

A loop in computer programming is a set of instructions that tells the system to repeat a certain set of commands for a specified amount of times or until a certain condition is met.

Examples

-name: copy all files in myconf/
 copy:
    src: {{ items }}
    dest: /etc/myapp/
    owner: root
    group: root
    mode: u=rw, g=rw, o=r
 With_fileglob:
    -“myconf /*

Run the playbook by using this command in the Linux terminal.

ansible-playbook testbook.yml

The script above copies “myconf/” along with all its contents to the destination directory /etc/myapp/ on the remote host. The copy command is issued for the transfer of copied content. With_fileglob is the loop that runs until the files are completely copied to the remote host.

This approach works when there are multiple files to be transferred to the same directory.

Directory Copy

This is another approach to send file copies to the remote host. Here, we copy the entire directory containing the files we need to transfer and send it to the remote node. It may be a little time-consuming as one needs to first move all the files to a certain directory and then copy the directory altogether. An example demonstrates this.

-name: directory copy to remote device
 copy:
    src: myconfig/
    dest: etc/myfolder
    owner: root
    group: root
    mode: u=rw, g=rw, o=r

The script above copies the directory myconfig/ to a remote device with destination /etc/myfolder. It is an inter directory transfer. The “mode” segment simply ensures the objects of the file system are created with the correct permissions. These modes can be viewed on the module’s main page under “copy module parameters”.

Copying Files to Multiple Remote Destinations

For further efficiency in copy-pasting multiple files, we may use a loop to send files to multiple destinations altogether. Let us use the loop mechanism to send 4 utilities to the remote host in one go.

-name: copying multiple files to multiple destinations
 copy:
    src: {{ item.src }}
    dest: {{ item.dest }}
    owner: root
    group: root
    mode: u=rw, g=rw, o=r
with-items:
  -{ src: setup1.conf, dest: /etc/setup1f/ }
  -{ src: setup2.conf, dest: /etc/setup2f/ }
  -{ src: setup3.conf, dest: /etc/setup3f/ }
  -{ src: setup4.conf, dest: /etc/setup4f/ }

The code above copies the setup files of 4 utilities from the root owner to the remote host at the /etc/setup directory. The “item.src” indicates that more than 1 item is being copied from the target machine.

Copy Files from a Destination to Another on the Remote Host

With Ansible, we can also copy files from one destination to another on the remote host. While this procedure is valid for files, it does not work for directories. A small script that transfers a test file from folder1 to folder2 is shown below.

-name: file transfer on remote host
 copy:
    src: $home/folder1/test_file
    remote_src: true
    dest: $home/folder2/test_file

Create a Backup File at the Remote Host before Copying

There are times when we may copy another file by mistake. A remedy to avoid such problems is to create a backup file on the remote server.

The Ansible copy module comes with a “backup” parameter to counter just that. In case a remote file is there and is not the same as the copied file, a new file will be created. The difference is that the new file will be appended with the current timestamp and the name of the original file. By default, the backup parameter has its value set as ‘no’.

For example, the following script creates a backup of “myscript.txt” in the /abc directory of the remote host. It will be named something like ‘[email protected]:51:18’.

- hosts: blocks
  tasks:
  - name: ansible copy file backup example
    copy:
      src: ~/myscript.txt
      dest: /abc
      backup: yes

Conclusion

In this article, we went over the copy module and its parameters. We saw how we could use multiple approaches to transfer multiple files from local to the remote host and also manipulate certain aspects like changing the directory of copied files remotely or transferring multiple files to multiple destinations.

That was all for the copy module in Ansible along with copying procedures for multiple files across devices. Hopefully, any confusion you had regarding the Ansible copy mechanism is cleared after going through this article.

About the author

Zeeman Memon

Hi there! I'm a Software Engineer who loves to write about tech. You can reach out to me on LinkedIn.