How to copy a file from local to remote host using Ansible?

AnsibleAnsibleBeginner
Practice Now

Introduction

Ansible is a powerful open-source automation tool that simplifies the management of infrastructure and applications. In this tutorial, we will explore how to use Ansible to copy a file from your local machine to a remote host, a common task in system administration and DevOps workflows.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL ansible(("`Ansible`")) -.-> ansible/ModuleOperationsGroup(["`Module Operations`"]) ansible(("`Ansible`")) -.-> ansible/PlaybookEssentialsGroup(["`Playbook Essentials`"]) ansible/ModuleOperationsGroup -.-> ansible/copy("`Transfer Files`") ansible/ModuleOperationsGroup -.-> ansible/file("`Manage Files/Directories`") ansible/ModuleOperationsGroup -.-> ansible/template("`Generate Files from Templates`") ansible/PlaybookEssentialsGroup -.-> ansible/playbook("`Execute Playbook`") ansible/PlaybookEssentialsGroup -.-> ansible/roles("`Assign Roles`") subgraph Lab Skills ansible/copy -.-> lab-414918{{"`How to copy a file from local to remote host using Ansible?`"}} ansible/file -.-> lab-414918{{"`How to copy a file from local to remote host using Ansible?`"}} ansible/template -.-> lab-414918{{"`How to copy a file from local to remote host using Ansible?`"}} ansible/playbook -.-> lab-414918{{"`How to copy a file from local to remote host using Ansible?`"}} ansible/roles -.-> lab-414918{{"`How to copy a file from local to remote host using Ansible?`"}} end

Introduction to Ansible

Ansible is a powerful open-source automation tool that simplifies the process of managing and configuring remote systems. It is designed to be easy to use, agentless, and highly scalable, making it an excellent choice for IT professionals and DevOps teams.

What is Ansible?

Ansible is a configuration management and orchestration tool that allows you to automate various tasks, such as software deployment, configuration management, and infrastructure provisioning. It uses a simple, human-readable language called YAML to define the desired state of your systems, and it then executes the necessary actions to bring those systems into the desired state.

Key Features of Ansible

  • Agentless: Ansible does not require any special software or agents to be installed on the remote systems it manages. It uses SSH (or Windows Remote Management) to communicate with the remote hosts.
  • Declarative Approach: Ansible uses a declarative approach, where you define the desired state of your systems, and Ansible takes care of the necessary steps to achieve that state.
  • Modular Design: Ansible is designed with a modular architecture, which allows you to extend its functionality by using pre-built modules or creating your own custom modules.
  • Idempotency: Ansible's tasks are designed to be idempotent, meaning that running the same task multiple times will not change the end result.
  • Simple and Readable: Ansible's YAML-based syntax is easy to read and write, making it accessible to both experienced and novice users.

Use Cases for Ansible

Ansible can be used in a wide range of scenarios, including:

  • Software deployment
  • Configuration management
  • Infrastructure provisioning
  • Application orchestration
  • Security and compliance management
  • Continuous Integration/Continuous Deployment (CI/CD) pipelines

Getting Started with Ansible

To get started with Ansible, you'll need to have a control node (the machine from which you'll run Ansible commands) and one or more remote hosts (the systems you want to manage). On the control node, you'll need to install Ansible and set up your inventory file, which defines the remote hosts you want to manage.

Here's an example of how to install Ansible on an Ubuntu 22.04 system:

sudo apt update
sudo apt install -y ansible

With Ansible installed, you can start automating your infrastructure and application management tasks.

Copying Files with Ansible

One of the most common tasks in infrastructure automation is copying files from the control node (the machine running Ansible) to the remote hosts. Ansible provides a simple and efficient way to accomplish this task using the copy module.

The copy Module

The copy module in Ansible is used to copy files from the control node to the remote hosts. It supports various options, such as:

  • src: The source file or directory on the control node.
  • dest: The destination path on the remote host.
  • owner: The owner of the file on the remote host.
  • group: The group of the file on the remote host.
  • mode: The permissions of the file on the remote host.

Copying a Single File

To copy a single file from the control node to a remote host, you can use the following Ansible playbook:

- hosts: all
  tasks:
    - name: Copy a file
      copy:
        src: /path/to/local/file.txt
        dest: /path/to/remote/file.txt
        owner: myuser
        group: mygroup
        mode: "0644"

In this example, the copy module is used to copy the file file.txt from the local path /path/to/local/file.txt to the remote path /path/to/remote/file.txt. The file will be owned by the myuser user and the mygroup group, and it will have permissions of 0644 (read-write for the owner, read-only for the group and others).

Copying a Directory

To copy an entire directory from the control node to a remote host, you can use the following Ansible playbook:

- hosts: all
  tasks:
    - name: Copy a directory
      copy:
        src: /path/to/local/directory/
        dest: /path/to/remote/directory/
        owner: myuser
        group: mygroup
        mode: "0755"
        recursive: yes

In this example, the copy module is used to copy the contents of the local directory /path/to/local/directory/ to the remote directory /path/to/remote/directory/. The recursive option is set to yes to ensure that the entire directory structure is copied. The files and directories will be owned by the myuser user and the mygroup group, and they will have permissions of 0755 (read-write-execute for the owner, read-execute for the group and others).

By using the copy module, you can easily and efficiently copy files and directories from the control node to the remote hosts, streamlining your infrastructure automation workflows.

Practical Example

In this section, we'll walk through a practical example of using Ansible to copy a file from the control node to a remote host.

Preparing the Environment

For this example, we'll assume that you have the following setup:

  • Control node: Ubuntu 22.04 system with Ansible installed
  • Remote host: Ubuntu 22.04 system

Make sure you have SSH access to the remote host and that the ansible user on the control node has the necessary permissions to connect to the remote host.

Creating the Ansible Playbook

Create a new file named copy_file.yml on the control node and add the following content:

- hosts: all
  tasks:
    - name: Copy a file
      copy:
        src: /path/to/local/file.txt
        dest: /path/to/remote/file.txt
        owner: myuser
        group: mygroup
        mode: "0644"

In this playbook:

  • hosts: all targets all the hosts defined in your Ansible inventory.
  • The copy module is used to copy the file file.txt from the local path /path/to/local/file.txt to the remote path /path/to/remote/file.txt.
  • The file will be owned by the myuser user and the mygroup group, and it will have permissions of 0644 (read-write for the owner, read-only for the group and others).

Running the Ansible Playbook

To run the playbook, execute the following command on the control node:

ansible-playbook copy_file.yml

Ansible will connect to the remote host(s) using SSH, copy the file, and set the appropriate ownership and permissions.

Verifying the Result

After running the playbook, you can log in to the remote host and verify that the file has been copied successfully:

ssh myuser@remote_host
ls -l /path/to/remote/file.txt

This should display the file with the expected ownership and permissions.

By following this practical example, you've learned how to use the copy module in Ansible to copy a file from the control node to a remote host. You can adapt this approach to your specific use cases and requirements, and continue exploring the rich functionality that Ansible provides for infrastructure automation.

Summary

This Ansible tutorial has provided a comprehensive guide on how to copy files from your local machine to a remote host. By leveraging Ansible's file module, you can streamline your infrastructure management and ensure consistent file distribution across your environment. Whether you're a seasoned Ansible user or new to the platform, this tutorial has equipped you with the knowledge to efficiently manage file transfers using the Ansible automation tool.

Other Ansible Tutorials you may like