How to Copy Multiple Files to Remote Hosts with Ansible?

I’m going to show you a live Playbook with some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.

Ansible Copy Multiple Files

  • ansible.builtin.fileglob
  • list files matching a pattern

Today we’re talking about the Ansible lookup plugin fileglob. Plugins are a way to expand the Ansible functionality. With lookup plugins specifically, you can load variables or templates with information from external sources. The full name is ansible.builtin.fileglob, it’s part of ansible-core and is included in all Ansible installations. The purpose of the lookup plugin is to list files matching a pattern.

Usage

Parameters

  • _terms string - path(s) of files to read

Return Values

  • _list list - list of files

The parameters of the plugin fileglob. The only required parameter is the default “_terms”, with the path(s) of files to read. You could easily use it in any Ansible loop with the Ansible statement: with_fileglob.

Join 50+ hours of courses in our exclusive community

Playbook

Copy Multiple Files with Ansible Playbook.

code

  • copy-multiple.yml
---
- name: copy module Playbook
  hosts: all
  become: false
  tasks:
    - name: copy multiple file(s)
      ansible.builtin.copy:
        src: "{{ item }}"
        dest: "/home/devops/"
        owner: devops
        mode: '0644'
      with_fileglob:
        - "examples/*.txt"
  • examples/report.txt
example
  • examples/report2.txt
example2

execution

$ ansible-playbook -i virtualmachines/demo/inventory copy\ files\ to\ remote\ hosts/copy-multiple.yml
PLAY [copy module Playbook] ***************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [copy multiple file(s)] **********************************************************************
changed: [demo.example.com] => (item=/Users/lberton/prj/github/ansible-pilot/copy files to remote hosts/examples/report2.txt)
changed: [demo.example.com] => (item=/Users/lberton/prj/github/ansible-pilot/copy files to remote hosts/examples/report.txt)
PLAY RECAP ****************************************************************************************
demo.example.com           : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ansible-pilot $

idempotency

$ ansible-playbook -i virtualmachines/demo/inventory copy\ files\ to\ remote\ hosts/copy-multiple.yml
PLAY [copy module Playbook] ***************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [copy multiple file(s)] **********************************************************************
ok: [demo.example.com] => (item=/Users/lberton/prj/github/ansible-pilot/copy files to remote hosts/examples/report2.txt)
ok: [demo.example.com] => (item=/Users/lberton/prj/github/ansible-pilot/copy files to remote hosts/examples/report.txt)
PLAY RECAP ****************************************************************************************
demo.example.com           : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ansible-pilot $

before execution

$ ssh [email protected]
Last login: Mon Jan 17 11:50:11 2022 from 192.168.0.105
[devops@demo ~]$ ls -al
total 16
drwx------. 4 devops wheel 111 Jan 11 17:37 .
drwxr-xr-x. 4 root   root   35 Jan  5 10:18 ..
drwx------. 3 devops wheel  17 Jan  5 10:22 .ansible
-rw-------. 1 devops wheel  92 Jan 17 11:50 .bash_history
-rw-r--r--. 1 devops wheel  18 Jul 26 09:51 .bash_logout
-rw-r--r--. 1 devops wheel 141 Jul 26 09:51 .bash_profile
-rw-r--r--. 1 devops wheel 376 Jul 26 09:51 .bashrc
drwx------. 2 devops wheel  29 Jan  5 10:18 .ssh
[devops@demo ~]$

after execution

$ ssh [email protected]
Last login: Mon Jan 17 11:52:24 2022 from 192.168.0.105
[devops@demo ~]$ ls -al
total 24
drwx------. 4 devops wheel 148 Jan 17 11:52 .
drwxr-xr-x. 4 root   root   35 Jan  5 10:18 ..
drwx------. 3 devops wheel  17 Jan  5 10:22 .ansible
-rw-------. 1 devops wheel 104 Jan 17 11:51 .bash_history
-rw-r--r--. 1 devops wheel  18 Jul 26 09:51 .bash_logout
-rw-r--r--. 1 devops wheel 141 Jul 26 09:51 .bash_profile
-rw-r--r--. 1 devops wheel 376 Jul 26 09:51 .bashrc
drwx------. 2 devops wheel  29 Jan  5 10:18 .ssh
-rw-r--r--. 1 devops wheel  16 Jan 17 11:52 report.txt
-rw-r--r--. 1 devops wheel  16 Jan 17 11:51 report2.txt
[devops@demo ~]$ cat report.txt
test report.txt
[devops@demo ~]$ cat report2.txt
test report.txt
[devops@demo ~]$

code with ❤️ in GitHub

Conclusion

Now you know how to Copy Multiple Files to Remote Hosts with Ansible. Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my Udemy 300+ Lessons Video Course.

BUY the Complete Udemy 300+ Lessons Video Course

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Patreon Buy me a Pizza