Operations 2 min read

Copy Multiple Files or Directories Using with_items in Ansible

This guide demonstrates how to use Ansible's with_items loop to copy multiple files or directories to a target server, showing the playbook structure, required parameters, example command execution, and verification of copied files.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
Copy Multiple Files or Directories Using with_items in Ansible

When you need to copy several files or directories with Ansible, the with_items loop provides a concise solution.

Example playbook:

- hosts: all
  remote_user: root
  gather_facts: false
  tasks:
    - name: copy file
      copy:
        src: /root/{{item}}
        dest: /usr/local/apps/
      with_items:
        ['45.txt', 'run.sh', 'node_exporter.tar.gz']

Run the playbook in check mode to preview actions:

ansible-playbook -C copy.yaml

Execute the playbook normally to perform the copy:

ansible-playbook copy.yaml

After execution, verify the files on the target server under /usr/local/apps/ to ensure they were transferred correctly.

AutomationOperationsAnsiblefile-copywith_items
Practical DevOps Architecture
Written by

Practical DevOps Architecture

Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.