Operations 3 min read

Using Ansible to Copy Single and Multiple Files to Target Servers

This guide demonstrates how to use Ansible playbooks to copy a single file or multiple files from a source host to a target server, detailing the required YAML configuration, copy module parameters, and execution steps for reliable file deployment in automated operations.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
Using Ansible to Copy Single and Multiple Files to Target Servers

This article provides a step‑by‑step tutorial on using Ansible to copy files from a source machine to one or more target servers. It covers both the simple case of copying a single file and the more complex case of copying multiple files with a loop.

Copy a single file

The following playbook ( copy.yml ) copies /root/ansible/luyan/copymodule/copytest1.txt to /opt/copymodule on every host in the inventory. Key parameters include remote_user , src , dest , owner , group , and mode set to 755 for executable permissions.

---
- hosts: all
  remote_user: root
  gather_facts: false
  tasks:
    - name: "复制主机上的文件到目标服务器上"
      copy:
        src: "/root/ansible/luyan/copymodule/copytest1.txt"
        dest: "/opt/copymodule"
        owner: root
        group: root
        mode: 755

Copy multiple files

For copying several files, the playbook ( copyduogewenjian.yml ) uses the with_items loop. Each item defines a src and dest pair, allowing the same task to run repeatedly for different files.

---
- hosts: all
  remote_user: root
  gather_facts: false
  tasks:
    - name: "复制主机上的文件到目标服务器上"
      copy:
        src: "{{ item.src }}"
        dest: "{{ item.dest }}"
        owner: root
        group: root
        mode: 755
      with_items:
        - {src:"/root/ansible/luyan/copymodule/copytest1.txt", dest:"/opt/copymodule"}
        - {src:"/root/ansible/luyan/copymodule/copytest2.txt", dest:"/opt/copymodule"}
        - {src:"/root/ansible/luyan/copymodule/copytest3.txt", dest:"/opt/copymodule"}

The article ends with a separator line and a list of recommended reading links to related DevOps and monitoring topics, encouraging readers to like, share, and follow the author for more technical content.

automationoperationsDevOpsyamlAnsiblefile-copy
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.