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.

Watch Video

Watch "Ansible Copy Multiple Files: fileglob Lookup & with_fileglob Examples" on YouTube

What You'll Learn

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

Read the full written article: Ansible Copy Multiple Files: fileglob Lookup & with_fileglob Examples

Topics Covered

Related Video Tutorials