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.
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.yamlExecute the playbook normally to perform the copy:
ansible-playbook copy.yamlAfter execution, verify the files on the target server under /usr/local/apps/ to ensure they were transferred correctly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.