DevOps

Ansible Copy: Automated file copy module

Using the Ansible Copy module, learn how to copy files, manage permissions, and synchronize data between local and remote hosts

There is no doubt that Ansible is an excellent tool in the home lab, production, and any other environment you want to automate. It provides configuration management capabilities, and the learning curve isn’t too steep. There is a module, in particular, we want to look at, the Ansible copy module, and see how we can use it to copy files between a local machine and a remote server.

What is Ansible?

Ansible is a tool that I have been using for quite some time now, in the home lab and in production environments. It is an awesome configuration management solution that allows automating multiple operations on a remote host, from management to app deployment and configuring new greenfield installations.

One of the great things about Ansible is its agentless design, meaning you don’t have to worry about installing agents on your endpoints or servers to manage them with Ansible.

Simplifying Complex Tasks

By using YAML, a human-readable language, you can define automation ‘playbooks’ that describe the desired state of your system.

Whether copying individual files using the ansible copy module or managing entire infrastructure estates, the playbooks allow you to do this in an automated way.

Local to Remote Operations

One of the core functionalities of Ansible is its ability to manage tasks across localhost connection to remote. You can copy files from local hosts to remote destinations, synchronize directories, and execute commands on destination hosts.

Modular Approach

Ansible offers various modules, like the copy module and fetch modules, each for specific tasks. These modules allow complex operations, enabling users to copy files from local to remote or fetch files from remote servers.

Configuration Management

With Ansible, you can maintain consistent configuration files across remote hosts. It simplifies copying configuration files to remote destinations and accurately sets file permissions, leveraging the copy src and mode parameters.

Introduction to Ansible Copy

Ansible can copy files and directories between local and remote systems. With its various modules, such as the ansible copy module, you can manage file permissions, synchronize directories, and other tasks. You can also use an Ansible copy template module for copying files to remote hosts.

Let’s look at the capabilities of Ansible’s copy module, fetch module, and related functions. As a note, you can also copy files to Windows targets.

Establishing connectivity to your remote server

Before we try to copy files from our Ansible control machine to the remote server, we need to ensure successful connectivity to the remote host.

SSH keys are the best way to establish connectivity using Ansible and your remote host. With SSH keys, you don’t need passwords, providing a much more secure way to connect.

First, if you haven’t already, you will want to generate an SSH keypair on your Ansible control machine. You can do this with the command:

ssh-keygen

It will ask you where you want to store the keys. You can accept the defaults.

The ssh keygen utility creates a public key pair
The ssh keygen utility creates a public key pair

Next, you will want to use the ssh-copy-id command to copy over the key to your remote server you want to manage with Ansible.

The ssh copy id copies the keypair to the remote host
The ssh copy id copies the keypair to the remote host

Here you will see the syntax of copying it to the remote host. You specify the remote user and server to which you want to copy over the key.

ssh-copy-id <youruser>@<yourserver>

Below, I have already copied the key so you see the message that it was skipped.

Running the ssh copy id command
Running the ssh copy id command

You should now be able to issue an SSH command and establish connection without being asked for a password, etc.

Below, we are connected to the remote host without any prompts.

Testing SSH connectivity
Testing SSH connectivity

Testing connectivity

Once SSH connectivity is established after copying over the SSH key, we can use Ansible to test using a special ansible ping command.

ansible <hosts in playbook> -i <inventory file> -m ping

As you can see below, we get a SUCCESS message running the Ansible ping command. This is good. It means that we are ready to start working with the remote host using Ansible.

Running the Ansible ping command
Running the Ansible ping command

Copying Files with Ansible Copy Module

The ansible copy module is designed to copy files and directories from a local machine to a remote machine or from one host to another. Using this module, you can transfer single files or an entire directory.

Copying a Single File

To copy a single file from a local machine to a file on the remote host, you can use the following Ansible playbook. You might include something like this in a webservers default tasks playbook to copy out config files, etc:

- name: Copy a file to a remote server
  copy:
    src: /sourcefile/path
    dest: /destinationfile/path

Below is my main.yml file that I am using for the Ansible playbook. As you can see, we are performing an Ansible copy file operation. I am copying a simple existing file test.txt file to a user’s home directory called linuxadmin. Here we define the absolute path source and destination.

Ansible copy playbook
Ansible copy playbook

This playbook utilizes the src and dest parameters to specify the source file and destination path. The ansible copy module takes care of the file transfer, preserving the original file permissions. If the path does not exist, it will create a new directory reflecting the path.

Running the Ansible copy playbook
Running the Ansible copy playbook

The file is copied successfully over to the remote host.

The remote file is created
The remote file is created

If you run into permissions issues, you may need to use root mode with a become user.

Copying Multiple Files

When you need to copy multiple files or copying directories, the process is similar as mentioned earlier but with slight modifications:

- name: Copy entire directory
  copy:
    src: /sourcepath/directory
    dest: /destinationpath/directory/

This code snippet copies the directory contents from the local machine to the remote host, including all files and subdirectories.

Using Remote Copy Between Remote Hosts

For copying files between remote hosts, you can use the synchronize module. This module leverages the rsync command to transfer files, offering efficient remote file synchronization.

Managing File Permissions with Ansible

In addition to copying files, Ansible provides the ability to manage file permissions. Here’s how you can set permissions during a copy operation:

- name: Copy file with specific permissions
  copy:
    src: /sourcefile/path
    dest: /destinationfile/path
    mode: 0644

The mode parameter allows you to define the symbolic mode for the destination file, ensuring that the file permissions are set accurately.

Fetching Files from Remote Hosts

Using the Fetch Module

The fetch module in Ansible allows you to retrieve files from remote servers to your local machine. It’s an excellent tool for backing up configuration files or other important data.

- name: Fetch a file from a remote system
  fetch:
    src: /sourcefile/path
    dest: /destinationfile/path

The above playbook demonstrates how to use the src and dest parameters with the fetch module, where src is the remote file path and dest is the destination directory on the local system.

Fetching Entire Directory

When you need to copy an entire directory from a remote host, the synchronize module can be used in conjunction with the fetch module to achieve this:

- name: Fetch entire directory
  synchronize:
    src: /sourcefile/path
    dest: /destinationfile/path
    mode: pull

Working with Configuration Files

Configuration files are crucial in managing remote servers, and Ansible makes handling these files straightforward.

Copying Configuration Files

You can copy configuration files using the same ansible copy module, preserving the original file permissions and attributes.

- name: Copy configuration file
  copy:
    src: /path/to/local/configuration/file
    dest: /path/on/remote/server

– name: Copy configuration file copy: src: /path/to/local/configuration/file dest: /path/on/remote/server

Advanced Copying Techniques

Recursive Copy

Ansible supports the recursive copying of files and directories. By specifying the src path as a directory, you can copy all contents, including subdirectories, and have them copied recursively:

- name: Recursive copy of directory
  copy:
    src: /path/to/source/directory/
    dest: /path/to/remote/destination/directory/

Local to Remote Synchronization

Transferring files from local to remote hosts is one of Ansible’s powerful functionalities. You can efficiently manage local tasks involving remote destination paths by utilizing the src and dest parameters with the copy module.

Wrapping up

Ansible is a great tool that also has excellent file management. By using the ansible copy module, you can manage files and directories across remote hosts and local systems. You can perform basic copy tasks to advanced file synchronization, permissions handling, and configuration file management.

Subscribe to VirtualizationHowto via Email 🔔

Enter your email address to subscribe to this blog and receive notifications of new posts by email.



Brandon Lee

Brandon Lee is the Senior Writer, Engineer and owner at Virtualizationhowto.com, and a 7-time VMware vExpert, with over two decades of experience in Information Technology. Having worked for numerous Fortune 500 companies as well as in various industries, He has extensive experience in various IT segments and is a strong advocate for open source technologies. Brandon holds many industry certifications, loves the outdoors and spending time with family. Also, he goes through the effort of testing and troubleshooting issues, so you don't have to.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.