Ansible Pilot

Copy files to remote hosts - Local to Remote - Ansible module copy

How to copy report.txt file to remote hosts using Ansible module copy.

August 11, 2021
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

How to copy files to remote hosts?

I’m going to show you a live demo 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. For Windows target use Ansible win_copy module.

Parameters

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

---
- name: copy module demo
  hosts: all
  become: false
  tasks:
    - name: copy report.txt
      ansible.builtin.copy:
        src: report.txt
        dest: /home/devops/report.txt
        owner: devops
        mode: '0644'
test report.txt

code with ❤️ in GitHub

Recap

Now you know how to Copy files to remote hosts with Ansible. Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases