Ansible Copy Module

Ansible Copy Module

·

7 min read

cicube site


Introduction

We'll examine Ansible, discussing its importance, and then focus on the Ansible copy module. Through examples, we'll demonstrate its typical usage scenarios.

Steps to be covered:

What is Ansible?

Ansible is a powerful automation tool used in the field of IT for managing and orchestrating software applications, systems, and infrastructure. It enables users to automate various tasks such as configuration management, application deployment, cloud provisioning, and more.

At its core, Ansible operates based on a simple philosophy: automation should be easy to use, straightforward, and accessible to everyone. With Ansible, users can define their infrastructure and deployment procedures in a straightforward way using configuration files written in YAML format.

One of the key strengths of Ansible lies in its agentless architecture, which means that it doesn't require any additional software or daemons to be installed on the target systems. Instead, Ansible communicates with the remote servers and hosts hosts over SSH (Secure Shell) protocol, making it lightweight and easy to set up.

What is Ansible Copy Module?

The Ansible Copy Module simplifies file distribution tasks and ensures consistency across target hosts by automating file transfers.

It's especially useful in situations where files need to be distributed across multiple servers or when specific files are needed for configuring applications.

Let summarize it's concepts:

Functionality

  • This module allows copying files or directories from the local system (control machine) to remote hosts.

  • Provides flexibility in specifying source and destination paths.

  • Enables setting ownership and permissions for the copied files.

  • Handles symbolic links efficiently during the copying process.

When to use Ansible Copy?

  • Distributing configuration files across multiple servers.

  • Automating distribution of configuration file, scripts, binaries, or any other files required for system configuration or application deployment.

  • Managing file distribution tasks as part of infrastructure automation

Ansible copy module examples

Copying Files from Local to Remote

The following is a basic example for copying a file from the local machine (where Ansible is executed) to remote hosts.

- name: Copy a single file from local to remote
  hosts: target_hosts
  tasks:
      - name: Copy a file
        copy:
            src: /path/to/local/file.txt
            dest: /path/to/remote/location/file.txt

A task is defined using the copy module, specifying the source file path with the src parameter and the destination location with the dest parameter. This allows for the copying of a single file.

Copying Files and Directories from Local to Remote and Setting Permissions

This example extends the previous ones by including permissions while copying files and directories from the local machine to remote hosts. For files, the mode parameter is used to specify permissions (in octal format) for the copied file.

Similarly, for directories, permissions are set using the mode parameter.

- name: Copy a file and directory from local to remote with permissions
  hosts: target_hosts
  tasks:
      - name: Copy a file with permissions
        copy:
            src: /path/to/local/file.txt
            dest: /path/to/remote/location/file.txt
            //highlight-next-line
            mode: '0644'

      - name: Copy a directory with permissions
        copy:
            src: /path/to/local/directory
            dest: /path/to/remote/location/directory
            //highlight-next-line
            mode: '0755'
            //highlight-next-line
            recursive: yes

The recursive: yes parameter in the Ansible copy module is used when copying directories from a local machine to remote hosts. It indicates that the entire contents of the directory, including subdirectories and their files, should be copied recursively.

Handling file backups for preventing data loss

Enabling file backups with Ansible reduces the chance of accidentally losing or corrupting data while transferring files. It adds an extra level of security, giving users the option to keep existing files and go back to the previous version if necessary.

- name: Copy a file with backup on remote hosts
  hosts: target_hosts
  tasks:
      - name: Copy a file with backup
        copy:
            src: /path/to/local/file.txt
            dest: /path/to/remote/location/file.txt
            //highlight-next-line
            backup: yes

The backup parameter is set to 'yes' to enable file backup during the copying process. When copying the file from the local machine to remote hosts, Ansible will create a backup of the existing file on the remote hosts, if it exists.

The backup file will have a suffix appended to its name, typically .bak, and will contain the content of the original backup file created before the copy operation.

How to move files within the remote machine with Ansible copy?

To move our files to remote locations within the remote machine using Ansible's copy module, we can copy the file to the new destination and then use the remote_src: yes parameter to delete the original file after the copy operation.

- name: Move files within the remote machine
  hosts: target_hosts
  tasks:
      - name: Copy file to new destination
        copy:
            src: /path/to/source/file.txt
            dest: /path/to/new/destination/file.txt
            //highlight-next-line
            remote_src: yes
      - name: Remove original file
        file:
            path: /path/to/source/file.txt
            state: absent

This will first copy the file /path/to/source/file.txt to the new destination /path/to/new/destination/file.txt. Then, it will use the file module to delete the original file /path/to/source/file.txt.

How to validate files before copying?

The validate parameter in Ansible's copy module allows you to specify a command that Ansible will run on the source file before copying it to the destination. This is useful for performing custom validation checks or transformations on the file before copying it.

- name: Copy and validate a file
  hosts: target_hosts
  tasks:
      - name: Copy a file with validation
        copy:
            src: /path/to/source/file.txt
            dest: /path/to/destination/file.txt
            //highlight-next-line
            validate: '/bin/cat %s'

The validate parameter specifies the command /bin/cat %s that Ansible will run on the source file before copying it. In this case, we're using /bin/cat to simply read the contents of the file, but you can replace it with any other command that performs the any validation.

How to use Ansible Copy module with sudo permission?

To copy files with sudo permissions using Ansible's copy module, we can use the become parameter along with become_user and become_method options.

- name: Copy files with sudo permissions
  hosts: target_hosts
  //highlight-next-line
  become: yes
  become_user: root
  tasks:
      - name: Copy file with sudo permissions
        copy:
            src: /path/to/local/file.txt
            dest: /path/to/remote/destination/file.txt
            remote_src: yes
        become: yes
  • The become: yes parameter allows the tasks in the playbook to execute with sudo permissions.

  • The become_user: root parameter specifies the user to become when executing the tasks with sudo permissions. In this case, we're becoming the root user.

  • The remote_src: yes parameter indicates that the source file is located remotely.

  • Additionally, become: yes is specified within the copy task to ensure that the copy operation itself is performed with sudo permissions.

How to Deploy Static Content with Ansible copy?

To deploy static content using Ansible's copy module, you can create a playbook that copies the static files from your local machine to the desired location on the target.

- name: Deploy static content with Ansible copy
  hosts: target_hosts
  tasks:
      - name: Copy static files
        copy:
            src: /path/to/local/static_content
            dest: /var/www/html/
            recursive: yes
  • The src parameter specifies the path to the local directory containing the static content (/path/to/local/static_content).

  • The dest parameter specifies the destination directory on the target hosts where you want to deploy the static content (/var/www/html/).

  • The recursive: yes parameter ensures that all files and subdirectories within the source directory are copied recursively to the destination directory.

This is useful for scenarios such as deploying HTML, CSS, JavaScript, or other static files for a web application.

Ansible's Role with Kubernetes, CI/CD, and Docker?

Ansible has roles in managing infrastructure and automating deployment processes when working with technologies like Kubernetes, CI/CD, and Docker.

Infrastructure Management: Ansible simplifies infrastructure management by allowing users to define server configurations and software installations in code. It's commonly used for setting up systems like Kubernetes clusters, ensuring consistency and reliability across environments.

Deployment Automation: Ansible automates deployment tasks as part of CI/CD pipelines. It streamlines processes such as deploying Docker containers or updating Kubernetes clusters, ensuring smooth and consistent software delivery.

Integration and Coordination: Ansible facilitates integration and coordination between different tools and technologies. For example, it can seamlessly integrate with CI/CD tools to automate tasks like container building and deployment to Kubernetes. Additionally, Ansible helps ensure applications are deployed consistently across various environments (development, testing, production).

Conclusion

In this article, we explored the Ansible Copy Module and its functionality for copying files from the local machine to remote hosts. We covered various examples demonstrating how to copy files, set permissions, handle backups, move files within the local or remote machine, validate files before copying, and use sudo permissions.

CICube is launching soon for DevOps professionals!