Post

Juniper Network Automation: Ansible, PyEZ, JSNAPy, and SaltStack

Juniper Network Automation: Ansible, PyEZ, JSNAPy, and SaltStack

Juniper Network Automation: Ansible, PyEZ, JSNAPy, and SaltStack

In modern networking, automation is a requirement.
Juniper’s ecosystem provides mature tools to reduce manual effort, minimize errors, validate intent, and accelerate delivery.

Table of Contents


1. Ansible juniper.device Collection

Official Ansible collection for Junos (agentless, NETCONF or local execution).

Docs: https://ansible-juniper-collection.readthedocs.io/

Key Capabilities

  • Facts collection (system / RE / chassis)
  • Config load (merge / replace / set), diff & commit
  • Operational CLI commands
  • NETCONF RPC execution
  • JSNAPy test invocation
  • Software image upgrades
  • Local (on-box) PyEZ path or remote NETCONF

Installation

1
2
ansible-galaxy collection install juniper.device
pip install junos-eznc ncclient

Inventory (INI)

1
2
3
4
5
6
7
8
9
[junos_devices]
router1 ansible_host=192.0.2.1
router2 ansible_host=192.0.2.2

[junos_devices:vars]
ansible_user=netadmin
ansible_password=juniper123
ansible_connection=local
ansible_python_interpreter=/usr/bin/python3

Example: Gather Facts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
---
- name: Gather Junos facts
  hosts: junos_devices
  gather_facts: no
  collections:
    - juniper.device
  tasks:
    - name: Retrieve facts
      juniper.device.facts:
      register: facts_output

    - name: Show facts
      debug:
        var: facts_output

Notable Modules

ModulePurpose
factsGather system / RE / chassis facts
configLoad, diff, commit configurations
commandRun CLI operational commands
rpcExecute NETCONF RPCs
jsnapyRun JSNAPy validation suites
softwareManage Junos OS upgrades
pingTest reachability from the device

2. PyEZ

Python library (junos-eznc) for programmatic Junos interaction; best for custom logic.

Features

  • Open NETCONF session (Device)
  • Structured facts access
  • Invoke RPCs (XML / JSON)
  • Load / compare / commit configs (Config)
  • Table/View abstraction for structured data
  • Embed in Python services / CI tooling

Install

1
pip install junos-eznc

Example

1
2
3
4
5
6
7
8
9
10
11
from jnpr.junos import Device
from jnpr.junos.utils.config import Config

dev = Device(host='192.0.2.1', user='netadmin', password='juniper123')
dev.open()
print(dev.facts)
with Config(dev, mode='exclusive') as cu:
    cu.load('set system host-name demo-router', format='set')
    if cu.diff():
        cu.commit()
dev.close()

3. JSNAPy

State validation & snapshot comparison framework.

Use Cases

  • Pre / post change validation
  • Compliance & golden-state checks
  • Regression testing
  • Intent assurance

Install

1
pip install jsnapy

Workflow

  1. Define tests (e.g. test_bgp.yml)
  2. Pre-change snapshot:
    1
    
    jsnapy --snap pre_change -f test_bgp.yml -t hosts.yml
    
  3. Apply changes
  4. Post-change snapshot + compare:
    1
    
    jsnapy --check pre_change post_change -f test_bgp.yml -t hosts.yml
    

Sample Test (Excerpt)

1
2
3
4
5
6
7
8
tests:
  - test_bgp_neighbors:
      rpc: get-bgp-summary-information
      iterate:
        id: peer-address
        xpath: //bgp-peer
        tests:
          - is-equal: peer-state, Established

4. SaltStack

Event-driven automation & orchestration using Junos proxy minions.

Strengths

  • Real-time reactor (event -> action)
  • Scales to many devices centrally
  • Declarative state enforcement
  • Extensible modules & returners

Minimal Libraries

1
pip install junos-eznc jxmlease

Example

1
salt 'router1' junos.cli "show version"

5. Comparison Matrix

AspectAnsible (juniper.device)PyEZJSNAPySaltStack
StyleDeclarative playbooksImperative PythonValidation snapshotsEvent / state driven
TransportNETCONF / localNETCONFNETCONFNETCONF via proxy
Best ForMulti-step workflowsCustom toolingIntent & state validationReal-time orchestration
Learning CurveLow–MediumMediumLowMedium–High
Diff SupportConfig diff built-inManual (script)Snapshot comparisonState reports
Multi-VendorYesNoLimited (Junos focus)Limited (focus on managed types)
ExtensibilityRoles / collectionsFull PythonYAML testsModules / reactors / engines
CI/CD IntegrationCommonCommonValidation stagePossible (extra setup)
Event HandlingIndirectScriptedNone (batch)Native

6. When to Use Which Tool

GoalRecommended Tool
Fast repeatable config rolloutAnsible
Deep custom device logicPyEZ
Pre/post change assuranceJSNAPy
Event-triggered remediationSaltStack
Unified multi-vendor automationAnsible
Python service integrationPyEZ
Compliance & drift detectionJSNAPy (+ Ansible)
Large scale realtime orchestrationSaltStack

7. Sample End-to-End Pipeline

  1. JSNAPy: pre-change validation
  2. Ansible: deploy config (juniper.device.config)
  3. JSNAPy: post-change comparison (intent confirmation)
  4. PyEZ: custom inventory export or complex logic
  5. SaltStack: reactor watches for anomalies & triggers rollback / alert

8. Summary

Use each where it excels:

  • Ansible: orchestration & standardized config
  • PyEZ: granular programmable control
  • JSNAPy: confidence & compliance validation
  • SaltStack: scale & event-driven operations

Combined they form a robust Junos automation toolchain.

This post is licensed under CC BY 4.0 by the author.