Netmiko pour OmniSwitch

Dernière modification le 23 mars 2020

En 2016 j’ai écrit le module alcatel_aos pour Netmiko. Depuis, ce module Python fonctionne pour AOS6 et AOS8 et il est maintenu par Kirk Byers lorsque des changements majeurs sont effectués sur la librairie Netmiko.

Lorsqu’il s’agit de faire appel à Netmiko pour réaliser de l’automatisation sur OmniSwitch ALE, j’y adjoins le framework d’automatisation Nornir écrit en Python.

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/ :

---
ale_lab_1:
    hostname: 192.168.1.24
    port: 22
    groups:
        - lab
ale_lab_2:
    hostname: 192.168.1.23
    port: 22
    groups:
        - lab
---
lab:
    platform: 'alcatel_aos'
    data:
        radius:
            - name: rad1
              ip: 192.168.1.100
            - name: rad2
              ip: 192.168.1.110
---
username: 'admin'
password: 'switch'

Fichier templates/ :

{% for rad in host['radius'] %}
aaa radius-server "{{ rad.name }}" host {{ rad.ip }} key radiuskey retransmit 3 timeout 2 auth-port 1812 acct-port 1813
{% endfor %}

Fichiers scripts :

#!/usr/bin/env python3
import nornir
import nornir.plugins.tasks.networking as nptn
import nornir.plugins.functions.text as npft
import sys

# Limit traceback details
sys.tracebacklimit = 0


def get_facts():
    nr = nornir.InitNornir(config_file='config.yaml')
    npft.print_title('Playbook to display running-directory status')
    result = nr.run(
        task=nptn.netmiko_send_command,
        command_string="show running-directory"
            )
    npft.print_result(result)


if __name__ == "__main__":
    get_facts()
#!/usr/bin/env python3
import nornir
import nornir.plugins.tasks.networking as nptn
import nornir.plugins.tasks.text as nptt
import nornir.plugins.functions.text as npft
import sys

# Limit traceback details
sys.tracebacklimit = 0


def radius_configuration(task):
    # Transform inventory data to configuration via a template file
    r = task.run(task=nptt.template_file,
                 name='Radius Configuration',
                 template='radius.j2',
                 path=f'templates/')
    # Save the compiled configuration into a host variable
    task.host['config'] = r.result
    # Deploy that configuration to the device using Netmiko
    task.run(task=nptn.netmiko_send_config,
             name='Loading Radius Configuration on the device',
             config_commands=task.host['config'])


def config_radius():
    nr = nornir.InitNornir(config_file='config.yaml')
    npft.print_title('Playbook to configure the Radius IPs')
    result = nr.run(task=radius_configuration)
    npft.print_result(result)


if __name__ == "__main__":
    config_radius()
#!/usr/bin/env python3
import nornir
import nornir.plugins.tasks.networking as nptn
import nornir.plugins.functions.text as npft
import sys

# Limit traceback details
sys.tracebacklimit = 0


def config_save():
    nr = nornir.InitNornir(config_file='config.yaml')
    # Save running configuration using Netmiko
    npft.print_title('Playbook to save running configuration')
    result = nr.run(task=nptn.netmiko_save_config)
    npft.print_result(result)


if __name__ == "__main__":
    config_save()
Programme Python Netmiko get_facts
Résultat du get_facts.py
Programme Python Netmiko config_radius
Résultat du config_radius.py
Programme Python Netmiko config_save
Résultat du config_save.py
Automation on OmniSwitch
Netmiko for OmniSwitch – ALE web site