PyEZ

Dernière modification le 11 janvier 2020

PyEZ, de son vrai nom junos-eznc, est un micro framework proposé en Open Source par Juniper afin de faciliter l’accès, à l’aide du protocole NETCONF, aux équipement tournant Junos . Comme le montre l’image, indépendamment de l’accès direct par le langage Python, ce micro framework est utilisé par d’autres outils comme Ansible, Salt ou JSNAPy.

Junos PyEZ est un package jnpr.junos qui contient les modules suivants :

  • device: Defines the Device class, which represents the device running Junos OS and enables you to connect to and retrieve facts from the device.
  • exception: Defines exceptions encountered when accessing, configuring, and managing devices running Junos OS.
  • factory: Contains code pertaining to Tables and Views, including the loadyaml() function, which is used to load custom Tables and Views.
  • facts: A dictionary-like object of read-only facts about the device. These facts are accessed using the facts attribute of a Device object instance.
  • op: Includes predefined operational Tables and Views that can be used to filter output for common operational commands.
  • resources: Includes predefined configuration Tables and Views representing specific configuration resources, which can be used to programmatically configure devices running Junos OS.
  • transport: Contains code used by the Device class to support the different connection types.
  • utils: Includes configuration utilities, file system utilities, shell utilities, software installation utilities, and secure copy utilities.

Outre son usage avec Ansible ou Salt, ma façon préférée de l’utiliser est au travers de NAPALM, lui même piloté par Nornir. Sur la base d’une simulation Vagrant de deux Routing Engine vQFX, voici un exemple de lab.

Fichier de configuration Nornir :

---
core:
    num_workers: 100
inventory:
    plugin: nornir.plugins.inventory.simple.SimpleInventory
    options:
        host_file: 'inventory/hosts.yaml'
        group_file: 'inventory/groups.yaml'
        defaults_file: 'inventory/defaults.yaml'

Fichiers inventory/ :

---
vqfx01:
    hostname: 127.0.0.1
    port: 2222
    groups:
        - vqfx
vqfx02:
    hostname: 127.0.0.1
    port: 2200
    groups:
        - vqfx
---
vqfx:
    platform: 'junos'
    data:
        vlans:
            - id: 10
              name: 'test10'
            - id: 20
              name: 'test20'
            - id: 30
              name: 'test30'
            - id: 40
              name: 'test40'
---
username: 'root'
password: 'Juniper'

Fichier templates/ :

vlans {
{% for vlan in host['vlans'] %}
    {{ vlan.name }} {
        vlan-id {{ vlan.id }};
    }
{% endfor %}
}

Fichiers scripts :

#!/usr/bin/env python3
from nornir import InitNornir
from nornir.plugins.tasks.networking import napalm_get
from nornir.plugins.functions.text import print_result
import sys

# Limit traceback details
sys.tracebacklimit = 0

nr = InitNornir(config_file='config.yaml')

result = nr.run(
    napalm_get,
    getters=['facts']
)
print_result(result)
#!/usr/bin/env python3
from nornir import InitNornir
from nornir.plugins.tasks import networking, text
from nornir.plugins.functions.text import print_title, print_result
import sys

# Limit traceback details
sys.tracebacklimit = 0

nr = InitNornir(config_file='config.yaml')

def vlans_configuration(task):
    # Transform inventory data to configuration via a template file
    r = task.run(task=text.template_file,
                 name='VLANs Configuration',
                 template='vlans.j2',
                 path=f'templates/')
    # Save the compiled configuration into a host variable
    task.host['config'] = r.result
    # Deploy that configuration to the device using NAPALM
    task.run(task=networking.napalm_configure,
             name='Loading VLANs Configuration on the device',
             replace=False,
             configuration=task.host['config'])

print_title('Playbook to configure the VLANs')
result = nr.run(task=vlans_configuration)
print_result(result)