Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

Use Ansible to Transfer Files to or from Junos Devices

Use the Juniper Networks Ansible modules to copy files between the Ansible control node and Junos devices.

file_copy Module Overview

You can use the juniper.device.file_copy Ansible module to transfer files between the Ansible control node and Junos devices. Table 1 outlines the module arguments. You must include the action argument to specify the direction of transfer. You must also specify the local and remote directories as well as the filename of the file to transfer.

Table 1: file_copy Arguments

Module argument

Description

Default

action

Specify whether to copy a file to or from the remote device. Supported values:

  • get

  • put

file

Filename of the file to copy.

local_dir

Directory on the local Ansible control node.

remote_dir

Directory on the remote device.

The file_copy module reports a successful transfer if the module copies the file to the destination directory and the checksum of the copied file matches the checksum of the original file. In some cases, the transfer is successful but the module reports that the task failed because the checksums do not match. This might happen if the file is corrupted during transfer. It can also happen if you transfer a large log file that is updated frequently. In this case, if the file is updated as it is being transferred, the transferred file can differ slightly from the original file causing a checksum mismatch.

Transfer Files from the Remote Device

You can use the juniper.device.file_copy module to copy a file from a Junos device to the Ansible control node. For example, you might want to periodically archive the configuration file or a log file on a device. To transfer a file from the remote device, specify action: get.

The following playbook transfers the messages log file from each device in the inventory group into a host-specific directory on the Ansible control node.

When you execute the playbook, it creates a logs/hostname directory for each host in the inventory group. The playbook then copies the messages log file from each remote host to the respective destination directory for that host. Although the copy task appears to fail for all hosts, the file transfer is actually successful. The Junos device constantly updates the messages log file. So, in this case, the Junos device is updating the log file as the transfer occurs. As a result, the checksum comparison between the local file and the remote file fails because the original and copied files differ slightly.

When you review the logs directory, the messages log is archived for each host.

Transfer Files to the Remote Device

You can use the juniper.device.file_copy module to copy a file from the Ansible control node to a Junos device. To transfer the file to the remote device, specify action: put.

The following playbook copies the bgp.slax script from the Ansible control node to each host in the specified inventory group. The script is copied from the scripts directory within the playbook directory to the /var/db/scripts/op directory on the Junos device.