Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

header-navigation
keyboard_arrow_up
close
keyboard_arrow_left
Ansible for Junos OS Developer Guide
Table of Contents Expand all
list Table of Contents
file_download PDF
{ "lLangCode": "en", "lName": "English", "lCountryCode": "us", "transcode": "en_US" }
English
keyboard_arrow_right

Use Ansible to Transfer Files to or from Junos Devices

date_range 03-Oct-24

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.

content_copy zoom_out_map
---
- name: Archive the messages log file
  hosts: junos
  connection: local
  gather_facts: false

  vars:
    host_log_dir: "logs/{{ inventory_hostname }}"

  tasks:
    - name: Create the log directory for the host
      ansible.builtin.file:
        path: "{{ host_log_dir }}"
        state: directory

    - name: Copy the log file from remote device
      juniper.device.file_copy:
        action: get
        file: messages
        local_dir: "{{ host_log_dir }}"
        remote_dir: /var/log

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.

content_copy zoom_out_map
user@ansible-cn:~$ ansible-playbook ansible-pb-archive-logs.yaml 

PLAY [Archive the messages log file] **********************************************

TASK [Create the log directory for the host] **************************************
ok: [r3]
ok: [r2]
ok: [r1]

TASK [Copy the log file from remote device] ***************************************
fatal: [r2]: FAILED! => {"changed": false, "msg": "Transfer failed (different MD5 between local and remote) 0b36d9e93cfd523b79eee5927ed42b68 | 3ea1421a5f68476c853180213df96686"}
fatal: [r3]: FAILED! => {"changed": false, "msg": "Transfer failed (different MD5 between local and remote) 7bac31566e5ec8da16d2a199dda628a6 | 40acb378a5e2b7aeacda3eb0337b5bec"}
fatal: [r1]: FAILED! => {"changed": false, "msg": "Transfer failed (different MD5 between local and remote) 7566d1efa73b50081dfee04aa4dbde57 | 2fce0d8a7272fe7e528786dade8cbe1f"}

PLAY RECAP ************************************************************************
r1                         : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   
r2                         : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   
r3                         : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

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

content_copy zoom_out_map
user@ansible-cn:~$ ls -l logs/*
logs/r1:
total 452
-rw-rw-r-- 1 user admin 459462 Sep 10 21:10 messages

logs/r2:
total 368
-rw-rw-r-- 1 user admin 373848 Sep 10 21:10 messages

logs/r3:
total 368
-rw-rw-r-- 1 user admin 374433 Sep 10 21:10 messages

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.

content_copy zoom_out_map
---
- name: Copy script to the Junos device
  hosts: junos
  connection: local
  gather_facts: false

  tasks:
    - name: Copy a local script to the Junos device
      juniper.device.file_copy:
        action: put
        file: bgp.slax
        local_dir: scripts
        remote_dir: /var/db/scripts/op
footer-navigation