Ansible copy Module: Copy Files & Content to Remote Hosts (Complete Guide) — Video Tutorial
Complete guide to Ansible copy module (ansible.builtin.copy). Copy files and content to remote hosts with permissions, backup, and validate options.
Watch Video
Watch "Ansible copy Module: Copy Files & Content to Remote Hosts (Complete Guide)" on YouTube
What You'll Learn
- How to copy files to remote hosts?
- Ansible copy files to remote hosts
- Parameters
- Demo
- code
- Conclusion
- Advanced Copy Examples
- Copy with backup
- Copy entire directory
- Validate before replacing
Full Tutorial Content
How to copy files to remote hosts?
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 files to remote hosts
Today we're talking about Ansible module copy.
The full name is "ansible.builtin.copy" which means is part of the collection of modules "builtin" with ansible and shipped with it.
This module is pretty stable and out for years.
The purpose is to copy files to remote locations.
Please note that the opposite is done by [Ansible fetch module](/articles/copy-files-from-remote-hosts-ansible-module-fetch).
For Windows target use [Ansible win_copy module](/articles/copy-files-to-windows-remote-hosts-ansible-module-wincopy).
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/sbi
About This Tutorial
- Author: Luca Berton
- Difficulty: Advanced
- Read time: 8 min
- Category: windows-automation
Read the full written article: Ansible copy Module: Copy Files & Content to Remote Hosts (Complete Guide)
Topics Covered
Related Video Tutorials
- Ansible Copy Multiple Files: fileglob Lookup & with_fileglob Examples — Discover how to use Ansible fileglob lookup plugin and copy module to efficiently transfer multiple files to remote hosts. Explore practical examples and steps.
- Ansible win_copy: Copy Files to Windows Hosts (Complete Guide) — How to copy files to Windows hosts with Ansible win_copy module. Transfer files, set permissions, copy directories, and manage Windows file operations.
- Ansible Fetch Module: Copy Files from Remote Hosts (Windows & Linux) — How to use Ansible fetch module to copy files from remote Windows and Linux hosts. Checksum validation, flat mode, and practical examples for file retrieval.
- Ansible fetch Module: Download Files from Remote Hosts (Complete Guide) — Complete guide to Ansible fetch module (ansible.builtin.fetch). Download and retrieve files from remote hosts to local machine with flat, dest, and validate_checksum.
- Ansible Create File: copy Module for Text Files (Complete Guide) — How to create text files with Ansible copy module. Write content inline, copy from controller, set permissions, and manage config files with examples.
- Ansible slurp Module: Read File Content from Remote Hosts (ansible.builtin.slurp) — Complete guide to ansible.builtin.slurp module. Read and decode file content from remote hosts as base64. Practical examples with register and b64decode.