Ansible Copy Multiple Files: fileglob Lookup & with_fileglob Examples
By Luca Berton · Published 2024-01-01 · Category: installation
Discover how to use Ansible fileglob lookup plugin and copy module to efficiently transfer multiple files to remote hosts. Explore practical examples and steps.

How to Copy Multiple Files to Remote Hosts with Ansible?
See also: Ansible win_copy Module: Copy Files to Windows Hosts (ansible.windows.win_copy)
Ansible Copy Multiple Files
ansible.builtin.fileglob- list files matching a pattern
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
with_fileglob.
## 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
example2See also: Ansible copy Module: Copy Files to Remote Hosts (ansible.builtin.copy Guide)
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 devops@demo.example.com
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 devops@demo.example.com
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 ~]$
Conclusion
Now you know how to Copy Multiple Files to Remote Hosts with Ansible.See also: Ansible Create File with Content: copy Module content Parameter
Related Articles
Category: installation
Watch the video: Ansible Copy Multiple Files: fileglob Lookup & with_fileglob Examples — Video Tutorial