Backing up Juniper vMX Configuration Files with Ansible

  • September 19, 2016

Objective

Every time you spin up a new topology at labs.networktocode.com, you get a fresh clean install with a bare config. This tutorial walks through how to backup the configurations of each Junos device such that you can re-load them the next you launch a new lab topology.

For this workflow, we'll use Ansible to backup the configs to the jump host that is included every topology.

Topology

This tutorial was written using the Juniper vMX 5-node topology.

Create Backups Directory

First, create a new directory called backups - this is where all of the backups will be saved once they're backed up.

Create Ansible Playbook

Create a playbook called backup_configs.yml. This will be used to automatically backup all Junos devices in the topology.

---

- name: BACKUP CONFIGS OF JUNOS DEVICES
  hosts: all
  connection: local
  gather_facts: False

  tasks:
  - name: GET CONFIG
    junos_get_config: 
      user: "{{ un }}"
      passwd: "{{ pwd }}" 
      host: "{{ inventory_hostname }}"
      dest: "backups/{{ inventory_hostname }}.conf"

This playbook assumes the credentials are stored in the inventory file and that the inventory file has only Junos devices. Should there be other device types, replace hosts: all in the playbook with hosts: junos.

The inventory file we are using for this playbook is as follows:

[all:vars]
un=ntc
pwd=ntc123

[junos]
vmx1
vmx2
vmx3
vmx4
vmx5

Execute Playbook

Now execute the playbook:

ntc@ntc:~/junos$ ansible-playbook -i inventory backup_config.yml

PLAY [BACKUP CONFIGS TO JUNOS DEVICES] **************************************

TASK [GET CONFIG] **************************************************************
changed: [vmx2]
changed: [vmx3]
changed: [vmx1]
changed: [vmx5]
changed: [vmx4]

PLAY RECAP *********************************************************************
vmx1                       : ok=1    changed=1    unreachable=0    failed=0
vmx2                       : ok=1    changed=1    unreachable=0    failed=0
vmx3                       : ok=1    changed=1    unreachable=0    failed=0
vmx4                       : ok=1    changed=1    unreachable=0    failed=0
vmx5                       : ok=1    changed=1    unreachable=0    failed=0

Verify Backups

Navigate to the backups directory and view your configuration files.

ntc@ntc:~$ ls backups/
vmx1.conf  vmx2.conf  vmx3.conf  vmx4.conf  vmx5.conf
ntc@ntc:~$

Retrieve Configurations

Once the configurations are backed up, you can now copy them from the jump host onto your local machine.

For MAC or Linux Users:

scp ntc@<jumphost-public-ipaddr>:./backups/*.conf ./

Windows Users:

SCP Client such as WinSCP.

Since each device has a public IP address, you can also run the playbook on your Linux machine (local Ansible installation) to mitigate the need for the final copy from the jump host to your machine.