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 Module: Copy Files to Remote Hosts (ansible.builtin.copy Guide) — Video Tutorial

How to copy files to remote hosts with Ansible copy module (ansible.builtin.copy). Transfer files, set permissions, use content parameter, backup.

Watch on YouTube · Read the written article

Tutorial summary

What you'll learn

  • How to copy files to remote hosts?
  • Ansible copy module — ansible.builtin.copy
  • Parameters
  • Demo
  • code
  • Conclusion
  • Advanced Copy Examples
  • Copy with backup
  • Copy entire directory
  • Validate before replacing
How to copy files to remote hosts? `ansible.builtin.copy` copies files, directories, and inline string content from the Ansible controller to remote hosts over SSH. It is idempotent — files are only transferred when content or permissions differ — and supports checksum validation, atomic backup creation, and SELinux context management. The opposite operation (downloading from remote to controller) is done by the [Ansible fetch module](/articles/copy-files-from-remote-hosts-ansible-module-fetch). For Windows targets use the [Ansible win_copy module](/articles/copy-files-to-windows-remote-hosts-ansible-module-win-copy). Ansible copy module — ansible.builtin.copy Parameters - dest _path_ - remote path - src _string_ - local path - backup _boolean_ - no / yes - validate _string_ - validation command - checksum _string_ 2.5+ - mode/owner/group - setype/seuser/selevel The parameter list is pretty wide but I'll summarize the most useful. The only required parameter is "dest" which specifies the remote absolute path destination. The "src" specifies the source file in the controller host. It could be a relative or absolute path. I recommend absolutely. The backup boolean option allows you to create a backup if the utility overwrites any file. If there is any tool to validate the file we could specify it in the validate parameter, very useful for configuration files. Let me also highlight that we could also specify the permissions and SELinux properties. Demo Let's jump in a real-life playbook to copy files to remote hosts with Ansible. code - copy.yml ```yaml --- - name: copy module Playbook hosts: all become: false tasks: - name: copy report.txt ansible.builtin.copy: src: report.txt dest: /home/devops/report.txt owner: devops mode: '0644' ``` - report.txt ```txt test report.txt ``` [code with ❤️ in GitHub](https://github.com/lucab85/ansible-pilot/tree/master/copy%20files%20to%20remote%20hosts) Conclusion Now you know how to Copy files to remote hosts with Ansible. Advanced Copy Examples Copy with backup ```yaml - name: Deploy config with automatic backup ansible.builtin.copy: src: files/nginx.conf dest: /etc/nginx/nginx.conf owner: root group: root mode: '0644' backup: true # Saves existing file as .backup become: true notify: reload nginx ``` Copy entire directory ```yaml - name: Copy directory to remote ansible.builtin.copy: src: files/app/ # Trailing slash copies CONTENTS dest: /opt/myapp/ owner: appuser group: appuser mode: '0755' become: true Without trailing slash: copies the directory itself src: files/app → creates /opt/myapp/app/ src: files/app/ → copies contents into /opt/myapp/ ``` Validate before replacing ```yaml - name: Copy and validate nginx config ansible.builtin.copy: src: files/nginx.conf dest: /etc/nginx/nginx.conf validate: /usr/sbin/nginx -t -c %s become: true # Only replaces if ng

About this tutorial

  • Author: Luca Berton
  • Difficulty: Advanced
  • Read time: 8 min
  • Category: windows-automation

Topics covered

Related video tutorials