Skip to content

Instantly share code, notes, and snippets.

View ktbyers's full-sized avatar

Kirk Byers ktbyers

View GitHub Profile
@ktbyers
ktbyers / ansible_j2.py
Last active August 18, 2021 23:28
Use Ansible Jinja2 Filters in Python
from jinja2.environment import Environment
from jinja2 import FileSystemLoader, StrictUndefined
from ansible_collections.ansible.netcommon.plugins.filter.ipaddr import ipmath
from ansible_collections.ansible.netcommon.plugins.filter.ipaddr import ipaddr
env = Environment(undefined=StrictUndefined)
env.loader = FileSystemLoader(['.', './templates/'])
env.filters["ipmath"] = ipmath
# Create new virtual environment
# Activate virtual environment
source nxos_ssh_test/bin/activate
# Install napalm libraries
pip install -q napalm-base
pip install git+https://github.com/napalm-automation/napalm-nxos@nxos_ssh
# If using napalm-ansible
@ktbyers
ktbyers / whatever.py
Created April 26, 2017 23:47
Python line wrapping
from snmp_helper import snmp_extract, snmp_get_oid_v3
last_changed = snmp_extract(snmp_get_oid_v3(snmp_device,
snmp_user, oid=oid,
auth_proto=auth_protocol,
encrypt_proto=encrypt_protocol,
display_errors=False))
@ktbyers
ktbyers / getters.py
Created April 19, 2017 17:42
NAPALM transition signatures
class BaseTestGetters(object):
"""Base class for testing drivers."""
def test_method_signatures(self):
"""Test that all methods have the same signature."""
# Method signatures that are migrating from old to new state (temporary state)
TRANSITION_SIGNATURES = [
'commit_config',
'commit_confirm',
tasks:
- ios_facts:
provider: "{{ creds }}"
- ios_config:
provider: "{{ creds }}"
lines:
- ip helper-address 1.1.1.1
parents: interface {{ item }}
with_items: "{{ dhcp_interfaces }}"
new_output:
changed: false
msg: "All items completed"
results:
- item: Vlan99
stdout: ["Building configuration...\n\nCurrent configuration : 66 bytes\n!\ninterface Vlan99\n no ip address\n ip helper-address 1.1.1.1\nend\n",]
warnings: []
- item: Vlan1
stdout: ["Building configuration...\n\nCurrent configuration : 66 bytes\n!\ninterface Vlan99\n no ip address\n ip helper-address 1.1.1.1\nend\n",]
warnings: []
@ktbyers
ktbyers / gist:7372e272000389e38f287482140898d5
Created March 27, 2017 18:08
EOS NAPALM get_optics data
{u'Ethernet3/1/1': {u'physical_channels': {u'channel': [{u'index': 0,
u'state': {u'input_power': {u'avg': 0.0,
u'instant': -1.8025870272698974,
u'max': 0.0,
u'min': 0.0},
u'laser_bias_current': {u'avg': 0.0,
u'instant': 6.306,
u'max': 0.0,
u'min': 0.0},
u'output_power': {u'avg': 0.0,
@ktbyers
ktbyers / arista_class.py
Created March 23, 2017 18:14
Arista Class
import socket
import jsonrpclib
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
class AristaSwitch(object):
def __init__(self, switch, username, password):
self.switch = switch
self.username = username
@ktbyers
ktbyers / threading_csv.py
Created February 24, 2017 20:10
Netmiko Threading from a CSV file
import csv
import threading
from Queue import Queue
from getpass import getpass
from netmiko import ConnectHandler
from datetime import datetime
USER = 'pyclass'
PASSWORD = getpass()
- name: NAPALM vlan configuration
hosts: pynet-sw1
gather_facts: False
tasks:
- napalm_install_config:
hostname: "{{ eapi_hostname }}"
username: "{{ eapi_username }}"
password: "{{ eapi_password }}"
dev_os: eos
config_file: pynet-sw1-merge.cfg