6 Ways to Copy Files to Remote Hosts with Ansible (Step by Step)

choubertsprojects

VPN offers!

1. NordVPN

2. Surfshark

3. ExpressVPN

When you are trying to copy files from one host to multiple hosts, Ansible is the answer. In this tutorial we will show how easy it really is and provide some practical examples of using Ansible in your day-to-day work.

Ansible is a tool that allows users to copy files to remote hosts. In this tutorial, we will go through 6 different ways to use Ansible in order to copy files. Read more in detail here: ansible copy.

6 Ways to Copy Files to Remote Hosts with Ansible (Step by Step)

Copying files is a routine activity that most Ansible users do on a daily basis. The Ansible copy module is a well-known technique to copy files and directories using Ansible, although it’s far from the only option. Are you using the correct technique?

You’ll learn how to copy files in Ansible using methods like Module for copying, template module, synchronize module, and more in this article.

Prerequisites

If you want to follow along, make sure you have the following items:

  • At least two Linux servers are required. Any Linux distribution that has Python 3 installed will work. The Ansible control node will be one server, and the managed node will be the other. The Ansible host will use the IP address X.X.X.X as a placeholder.
  • The Ansible control node has Python 3 and pip installed. If you need assistance installing Python 3 or pip, see this post.
  • The ability to SSH into both the control and managed nodes in Ansible. If you need assistance logging into a Linux server, there are many articles available online.
  • To install the program, you’ll need sudo access on each node. Someuser will be the user account for this lesson.

Sudo is a Linux command that lets you perform commands as a superuser. It’s the Linux equivalent of “Run as Administrator.” To understand more about the sudo command, see this article.

Ansible Control Node Configuration

You may skip this part if you already have Ansible installed.

Before you can use Ansible to transfer files, you must first create an Ansible control node. All Ansible instructions are executed from the control node. When a command is sent, the control node uses the SSH protocol to connect to the remote computer and provide commands to setup the Managed Web Hosting.

Ansible Package Installation

For the first task in Ansible Control Node Configuration, you must first install Ansible itself. To do that:

1. Connect to the Linux server that will be your Ansible control node using your chosen SSH client.

2. Make an ansible virtual environment in Python.

A virtual environment is a Python environment that is isolated. You may install certain versions of Ansible in virtual environments without having to use the Linux distribution packaging tools. You may use the dnf or apt commands to install Ansible, however the version that is installed might not be the same as the one in the guide. In the future, newer versions of Ansible may not operate with this tutorial.

3. When you’ve finished creating the virtual environment, enter the command below to start it up. The PATH variable in Linux is set up to utilize your isolated Python environment when you initialize the virtual environment. Instead of impacting the whole system, you may install Python packages in your isolated environment.

ansible/bin/activate source

4. To make sure the pip package is up to date, use the update pip command.

5. Finally, to install Ansible 2.10.6, use the pip command. Ansible is a Python package that can be installed using pip, Python’s package management.

Ansible 2.10.6 is used in this lesson. The examples in this tutorial will function as intended if you use this version of Ansible. Other versions could work, but there is no guarantee.

install ansible==2.10.6 with pip

Testing Connections and Creating the Ansible Inventory File

After you’ve installed Ansible, you’ll need to create a list of Managed Web Hostings for Ansible to target. These are the computers to which you will copy files. An inventory file defines Managed Web Hostings in Ansible.

Assuming you’re still connected to your Ansible controlled node, open your inventory in your preferred editor and add the following line.

ansible ssh extra args=’-o StrictHostKeyChecking=no’ managed node ansible host=X.X.X.X ansible user=someuser

Execute the ping module to verify the connection between the Ansible controller and the managed node after Ansible has discovered your Managed Web Hostings.

managed node -i inventory -m ping ansible

Ansible connection established successfullyAnsible connection established successfully

Module for copying

The most common method to copy files with Ansible is via Module for copying. This Ansible module serves one purpose and one purpose only; to copy files as-is to Managed Web Hostings.

Let’s dig in and walk through out to use Module for copying to copy a file to a Managed Web Hosting.

On the Ansible command node, type:

Create a text.txt file first. This source file will be copied to the Managed Web Hosting by Ansible.

echo “Hello World” > text.txt

2. Copy the file to the controlled host using Ansible’s copy module, as shown below.

The -a parameter has a src and dest key, as you can see. The src value specifies the file on the Ansible control node, while the dest specifies the path to which Ansible will transfer the file.

ansible managed node -i inventory ## specifies the inventory file to utilize, whereas -m copy specifies the module to load. ## characteristics supplied to the module: “src=test.txt dest=/tmp/test.txt”

The following output will appear after the command has completed. The text in the output is yellow, and the top line states “CHANGED.” Ansible has altered the Managed Web Hosting state (added a new file named test.txt to the /tmp directory), as shown by this output.

Changing the managed node using AnsibleChanging the managed node using Ansible

3. Rerun the command by pressing the up arrow. The output text is now green, and the top line of the output reads “SUCCESS.” This implies that the managed node already has a /tmp/test.txt file and that the Managed Web Hosting is in the appropriate condition.

On the Ansible node, no modifications are required.On the Ansible node, no modifications are required.

Run the cat command on the remote system to verify that Ansible transferred the file to the Managed Web Hosting using the shell module.

managed node -i inventory -m shell -a “cat /tmp/text.txt” ansible managed node

If everything is in order, you should see the output below.

A copy of the file has been made to the controlled node.A copy of the file has been made to the controlled node.

Module’s template

Although Module for copying is great for copying existing files, what about files that need values changed at runtime? In that case, you can use Module’s template.

Module’s template is similar to Module for copying, but it lets you define variables inside a text file, transform it, and then copy it to your Managed Web Hostings.

Related:How to Save Configuration Time with Ansible Templates

  1. Create a Jinja2 template first, which is a text file with numerous variables. Run the instructions below to do so.

## Create a text file called template.txt.j2 with “Test Message” as contents echo “Test Message” > template.txt.j2 ## Append the text {{ ata_message }} to the file echo “{{ ata_message }}” >> template.txt.j2

Notice a line in the Jinja2 template file with the string ata message surrounded in curly braces. This string is a Jinja2 variable that enables you to modify the file’s contents during runtime.

2. Run the command below to copy the template.txt.j2 template to the Managed Web Hosting, setting the variable ata message to Hello World Template.

Although the command below seems to be the same as the previous step, it has one extra parameter (-e). Extra variables (-e) is a mechanism to specify variables at runtime.

# Run the Ansible template module against the managed_node in inventory file “inventory” # Module’s template will copy and transform the template.txt.j2 file to the # Managed Web Hosting in the /tmp directory ansible managed_node -i inventory -m template -a “src=template.txt.j2 dest=/tmp/template.txt” -e “ata_message=’Hello World Template’”

Managed Web HostingManaged Web Hosting

3. Finally, execute the shell module again to read the file with the cat command on the Managed Web Hosting.

managed node -i inventory -m shell -a “cat /tmp/template.txt” ansible managed node

Shell Module Execution Shell Module Execution

The Module for Synchronization

Similar to Module for copying covered above, The Module for Synchronization is a bit different. Instead of performing a simple file copy, The Module for Synchronization uses the rsync utility.

For transferring several files at once, the rsync software is a wonderful choice. It utilizes less bandwidth and speeds up file transfers by using the remote-update protocol.

To use The Module for Synchronization, you must ensure you have rsync installed on both the control and managed nodes.

To use Ansible’s synchronize module to copy this tutorial’s test file, invoke The Module for Synchronization as shown below.

# Run the Ansible synchronize module against the managed_node in inventory file “inventory” # The Module for Synchronization will copy the test.txt file to the # Managed Web Hosting in the /tmp directory ansible managed_node -i inventory -m synchronize -a “src=test.txt dest=/tmp/test.txt”

By default, The Module for Synchronization uses rsync’s “push” mode to transfer files which copies files from the control node to the Managed Web Hosting. You can configure rsync to use “pull” mode as well. When the pull mode is used, the managed node pulls the files from the control node.

The output you can see below is much different from the output of Module for copying. Since The Module for Synchronization uses the rsync utility, the output will contain the utility syntax for rsync used to copy the file.

utility syntax for rsyncutility syntax for rsync

The Module get url

Perhaps the file(s) you’d like Ansible to copy to managed nodes isn’t on the Ansible control node. Instead, the file is hosted on a web server somewhere and is available via URL. In that case, use The Module get url. The Module get url pulls a file from a URL and copies it to Managed Web Hostings.

To copy files with The Module get url, invoke The Module get url as shown below. This command downloads the file stored at https://file-examples-com.github.io/uploads/2017/02/file_example_CSV_5000.csv and copies it to /tmp/test.csv.

# Run the Ansible Module get url against the managed_node in inventory file “inventory” # The Module get url will copy the “file_example_CSV_5000.csv” file from https://file-examples-com.github.io # to the Managed Web Hosting in the /tmp directory ansible managed_node -i inventory -m get_url -a “url=’https://file-examples-com.github.io/uploads/2017/02/file_example_CSV_5000.csv’ dest=/tmp/test.csv”

Module get urlModule get url

git’s Module

If you have files store in a source code management (SCM) tool like Git and need Ansible to copy files from that repo to a Managed Web Hosting, use git’s Module.

To use git’s Module, you must have Git installed on the managed node.

To demonstrate using git’s Module to copy files to a managed node, invoke git’s Module as shown below. This command invokes the git clone command on each managed node in the inventory file cloning the Ansible GitHub repo into the /tmp/ansible directory.

# Run the Ansible Module for git against the managed_node in inventory file “inventory” # git’s Module will copy Ansible git repository from GitHub # to the Managed Web Hosting in the /tmp directory ansible managed_node -i inventory -m git -a “repo=https://github.com/ansible/ansible.git dest=/tmp/ansible”

Module for gitModule for git

Now view the contents of the Git repository on the Managed Web Hosting. To do so, invoke the shell module to execute the ls command to read all files in the /tmp/ansible directory. If successful, you should see the contents of the Ansible repository in the output.

ls -ahsl /tmp/ansible ansible managed node -i inventory -m shell -a

Conclusion

Ansible has numerous options for copying files to a remote server. These modules let you construct your automation process in a clear and simple way that you can simply share with your team or company.

Using Ansible, you can copy files to remote hosts. This article will show you how to do it by step. Reference: ansible copy all files in directory.

Related Tags

  • ansible copy delegate_to
  • ansible copy files on remote host
  • ansible copy example
  • ansible copy source not found
  • ansible copy directory to remote