Copy Multiple Files to Remote Hosts - Local to Remote - Ansible lookup plugin fileglob
How to automate the copy of the "example/*.txt" files to a target host using the with_fileglob Ansible loop statement and Ansible lookup plugin fileglob.


How to Copy Multiple Files to Remote Hosts with Ansible?
I’m going to show you a live demo 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
.
The Best Resources For Ansible
Video Course
Printed Book
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
demo
Copy Multiple Files with Ansible Playbook.
code
- copy-multiple.yml
---
- name: copy module demo
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 demo] ***************************************************************************
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 demo] ***************************************************************************
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
[[email protected] ~]$ 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
[[email protected] ~]$
after execution
$ ssh [email protected]
Last login: Mon Jan 17 11:52:24 2022 from 192.168.0.105
[[email protected] ~]$ 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
[[email protected] ~]$ cat report.txt
test report.txt
[[email protected] ~]$ cat report2.txt
test report.txt
[[email protected] ~]$
Recap
Now you know how to Copy Multiple Files to Remote Hosts with Ansible. Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.
Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate