Use Ansible to Transfer Files to or from Junos Devices

Use the juniper.device.file_copy module to copy a file between the Ansible control node and Junos devices.

Juniper Networks provides an Ansible module that you can use to transfer a file between the Ansible control node and a Junos device. Table 1 outlines the available module. Ansible also provides standard built-in modules to handle file operations. This topic discusses how to use the juniper.device.file_copy module.

Table 1: Modules to Copy a File

Collection

Module Set

Module Name

juniper.device

juniper.device

juniper.device.file_copy

junipernetworks.junos

–

juniper.device.file_copy Module Overview

You can use the juniper.device.file_copy module to transfer a file between the Ansible control node and a Junos device. Table 2 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. You can optionally include the transfer protocol and destination filename.

Table 2: juniper.device.file_copy Arguments

Module Argument

Description

action

Action to perform. Specify whether to copy the file to or from the remote device.

  • Values:

    • get—Copy a file from the managed device to the Ansible control node.

    • put—Copy a file from the Ansible control node to the managed device.

checksum

(Optional) Specify whether to validate the MD5 checksum of the file.

  • Default: true

  • Values:

    • true—Validate the file's MD5 checksum, thus verifying the integrity of the copied file.

    • false—Skip the checksum validation.

file

Filename of the file to copy.

local_dir

Directory on the local Ansible control node.

protocol

(Optional) Protocol to use for the file transfer.

  • Default: scp

  • Values:

    • ftp

    • scp

remote_dir

Directory on the remote device.

transfer_filename

(Optional) Filename of the destination file. If you omit this parameter, the destination file uses the same filename as the source file.

By default, the juniper.device.file_copy module uses the SCP protocol and validates the file checksum to verify the integrity of the copied file. 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. A checksum mismatch 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. In these cases, you can set checksum: false to skip the checksum validation. However, we recommend keeping the checksum validation for file transfers.

Transfer a File 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 logs directory on the Ansible control node. The module arguments explicitly specify the transfer protocol as scp, which is the default. The module uses a unique host-specific filename for each destination file so that each copied file doesn't overwrite the previous file.

When you execute the playbook, it first creates a logs directory. The playbook then copies the messages log file from each device to the destination directory and saves each file with a unique filename. Although the copy task appears to fail for all hosts, the file transfer is actually successful. In this case, the Junos devices continue to update 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.

A review of the logs directory indicates that the messages log is archived for each host.

In cases where you know the checksum validation will fail, you can optionally include checksum: false to skip the validation. If you execute the previous playbook and skip the checksum validation, the task reports "changed": true for all hosts.

Transfer a File 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 playbook's scripts directory to the /var/db/scripts/op directory on the Junos device.