AnsiblePilot — Master Ansible Automation
AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.
Popular Topics
About Luca Berton
Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.
Ansible Copy Multiple Files: fileglob Lookup & with_fileglob Examples — Video Tutorial
Discover how to use Ansible fileglob lookup plugin and copy module to efficiently transfer multiple files to remote hosts. Explore practical examples and steps.
What You'll Learn
- How to Copy Multiple Files to Remote Hosts with Ansible?
- Ansible Copy Multiple Files
- Usage
- Parameters
- Return Values
- code
- execution
- idempotency
- before execution
- after execution
Full Tutorial Content
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`.
## Playbook
Copy Multiple Files with Ansible Playbook.
code
- copy-multiple.yml
```yaml
---
- 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
```txt
example
```
- examples/report2.txt
```txt
example2
```
execution
```bash
$ 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
```bash
$ 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:
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 4 min
- Category: installation
Read the full written article: Ansible Copy Multiple Files: fileglob Lookup & with_fileglob Examples
Related Video Tutorials
- Ansible win_copy Module: Copy Files to Windows Hosts (ansible.windows.win_copy) — How to copy files to Windows remote hosts with Ansible win_copy module (ansible.windows.win_copy). Transfer files, directories, set permissions.
- Ansible copy Module: Copy Files to Remote Hosts (ansible.builtin.copy Guide) — How to copy files to remote hosts with Ansible copy module (ansible.builtin.copy). Transfer files, set permissions, use content parameter, backup.
- Ansible fetch Module: Copy Files from Remote Hosts to Control Node — How to copy files from remote hosts with Ansible fetch module (ansible.builtin.fetch). Download logs, configs, backups from Linux and Windows.
- Ansible fetch Module: Download Files from Remote Hosts (Complete Guide) — Complete guide to Ansible fetch module (ansible.builtin.fetch). Download and retrieve files from remote hosts to local machine with flat, dest.
- Ansible Create File with Content: copy Module content Parameter — How to create files with content in Ansible using the copy module content parameter. Write text, YAML, JSON to files without templates.
- Ansible slurp Module: Read File Content from Remote Hosts (ansible.builtin.slurp) — Complete guide to ansible.builtin.slurp module. Read and decode file content from remote hosts as base64. Practical examples with register and b64decode.