DevOps

Ansible Copy: Automated file copy module

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.

Frequently Asked Questions

How Does Ansible’s Copy Module Handle File Permissions on the Remote Host?

Ansible’s copy module allows you to control file permissions on the remote host through the mode parameter.

By setting the symbolic mode, you can ensure that the copied file retains the intended permissions. This also includes root group permissions, providing flexibility and security when copying files to remote systems.

What is the Difference Between the Copy Module and the Fetch Module in Ansible?

The ansible copy module transfers files from the local system to remote hosts. Comparting the two, the fetch module retrieves files from remote servers to the local machine.

The two modules are suitable for different scenarios. This could be copying data to remote destinations or fetching backup copies to the local system.

Can I Use Ansible to Copy Only the Contents of a Directory Without the Directory Itself?

Yes, you can copy only the contents of a directory using Ansible by specifying the src parameter with a trailing slash.

This way, you can ensure that only the contents, not the directory itself, are copied to the remote destination. It offers a straightforward solution to merge directory contents with existing files in the destination directory.

How Do I Utilize the Backup Parameter in Ansible’s Copy Module?

The backup parameter in Ansible’s copy module allows you to back up a copy of the destination file before you overwrite the file. By setting this parameter to yes, you can keep the original file, allowing you to recover it in case something happens and the original file is needed.

Is it Possible to Copy Files to Multiple Remote Hosts Simultaneously?

Ansible’s copy functionalities extend to managing files across multiple remote hosts. You can define a list of target hosts in your playbook, allowing you to copy files or directories to various remote destinations.

Whether it’s configuration files or entire directories, Ansible ensures simultaneous and consistent transfers across remote machines.

How Can I Copy Files from Local Host to Remote Servers Using a Relative Path?

Ansible supports using relative paths in the src and dest parameters, facilitating file transfers without hard-coded absolute paths. Specifying the relative path allows you to maintain a flexible and dynamic setup, especially when working with different environments or remote hosts.

Other notables

  1. When working with Ansible, variable interpolation allows you to insert values dynamically within your playbooks, providing greater flexibility in defining paths, including temporary directories like “ansible tmp.”

  2. By utilizing the force parameter in Ansible’s copy module, you can ensure that files are overwritten in the destination, even within temporary locations like “ansible tmp,” making it an essential aspect of file management.

  3. Managing temporary files in Ansible through the “ansible tmp” directory can be customized using variable interpolation and control parameters like the force option, ensuring efficient and secure handling of transient data.

Mastering File Management with Ansible

Ansible contains excellent file management capabilities using the ansible copy module. It offers a flexible solution for managing 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.

By using Ansible, you can streamline file transfers and management, minimize errors, and maintain consistency across various remote servers and local machines. The Ansible copy module and parameters are reliable and automated solutions, whether copying a single file or synchronizing entire directories.

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 has over two decades of experience in Information Technology. Having worked for numerous Fortune 500 companies as well as in various industries, Brandon 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.

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.