Skip to content

Instantly share code, notes, and snippets.

View cidrblock's full-sized avatar

Bradley A. Thornton cidrblock

View GitHub Profile
@cidrblock
cidrblock / parser_defintion.yml
Created April 13, 2022 10:14
Parse output using native parser
x1 ➜ collection_development ansible-navigator run parse.yml -m stdout --ee false
PLAY [localhost] ***************************************************************
TASK [Define some data] ********************************************************
ok: [localhost]
TASK [Pass text and template_path] *********************************************
ok: [localhost]
@cidrblock
cidrblock / site.yml
Created April 12, 2022 20:17
Validate IPv4 address in lists of interfaces
- hosts: localhost
gather_facts: false
tasks:
- name: Define some data
ansible.builtin.set_fact:
data:
wan:
- name: LAN3
ipv4-address: 100.100.100.1
subnet-mask: 255.255.255.248
@cidrblock
cidrblock / site.yaml
Last active April 27, 2022 06:02
Examples for ansible.utils.consolidate
- hosts: localhost
gather_facts: false
tasks:
- name: Define some test data
ansible.builtin.set_fact:
values:
- name: a
value: 1
- name: b
value: 2
@cidrblock
cidrblock / go.py
Created March 31, 2022 15:02
Consolidate lists of lists of dictionaries based on a key
import itertools
import yaml
# - name: Build the data structure
# set_fact:
# data_sources:
# - data: interfaces.results
# match_key: name
@cidrblock
cidrblock / equal.py
Created February 21, 2022 13:46
Test the equality of iterables
"""Tests for an interable_equality function."""
from typing import Iterable
from typing import Generator
def iterable_equality(iterable_one: Iterable, iterable_two: Iterable) -> bool:
"""Check the equality of 2 iterables.
Convert both to a list and check length and items.
@cidrblock
cidrblock / go.py
Last active November 18, 2021 21:39
positive look ahead
import re
from pprint import pprint
str="""Name: toml
Version: 0.10.2
Summary: Python Library for Tom's Obvious, Minimal Language
Home-page: https://github.com/uiri/toml
Author: William Pearson
Author-email: uiri@xqz.ca
License: MIT
@cidrblock
cidrblock / try_runner.py
Created October 8, 2021 15:46
Sample runner code
""" Sample runner script
"""
import datetime
import os
from typing import Dict
from typing import List
import ansible_runner
class Ansible:
- name: Generate a random number
ansible.builtin.set_fact:
num: "{{ pick_from | random(start=1) }}"
- name: Set the condition flag
ansible.builtin.set_fact:
condition_met: "{{ num|int == number_to_match }}"
- name: Get out quick when condition is met
import ipaddress
from typing import List
def get_peers(address: str) -> List[str]:
"""Return possible peers when given an address"""
addr = ipaddress.ip_interface(address)
hosts = list(addr.network.hosts())
try:
{% macro render_neighbors(parent, indent) %}
{% if 'neighbors' in parent %}
{% for neighbor_id, neighbor in parent['neighbors']|sort_by_ip_as_int() %}
{# ############################################## activate #}
{% if 'activate' in neighbor and neighbor['activate'] %}
- "{{indent}}neighbor {{ neighbor['ipv4_address'] }} activate"
{% endif %}
{# ############################################## remote_as #}
{% if 'remote_as' in neighbor %}
- "{{indent}}neighbor {{ neighbor['ipv4_address'] }} remote-as {{ neighbor['remote_as'] }}"