Loading...

Watch: Copy Multiple Files to Remote Hosts with Ansible Efficiently

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?

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 vir

Read the full tutorial: Copy Multiple Files to Remote Hosts with Ansible Efficiently