Skip to content

Instantly share code, notes, and snippets.

@cidrblock
Created July 23, 2020 21:41
Show Gist options
  • Save cidrblock/6f9ebde33856a45be679fa0dd5fa5445 to your computer and use it in GitHub Desktop.
Save cidrblock/6f9ebde33856a45be679fa0dd5fa5445 to your computer and use it in GitHub Desktop.
Sample parser
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.network_template import (
NetworkTemplate,
)
class MyTemplate(NetworkTemplate):
def __init__(self, lines=None):
super(MyTemplate, self).__init__(lines=lines, tmplt=self)
PARSERS = [
{
"example": "FastEthernet0/0 is up, line protocol is up",
"getval": r"(?P<name>\S+) is (?P<admin_status>\S+), line protocol is (?P<oper_status>\S+)",
"result": {
"{{ name }}": {
"name": "{{ name }}",
"status": {
"operating": "{{ oper_status }}",
"administrative": "{{ admin_status }}"
}
}
},
"shared": True,
},
{
"example": "Hardware is Ethernet, address is 5254.0033.62f8 (bia 5254.0033.62f8)",
"getval": r"\s+Hardware is (?P<hardware>\S+), address is (?P<mac_address>\S+)",
"result": {
"{{ name }}": {
"hardware": "{{ hardware }}",
"mac": "{{ mac_address }}"
}
},
},
]
def parse(lines):
return MyTemplate(lines=lines).parse()
class FilterModule(object):
def filters(self):
return {"parse_eos_show_interface": parse}
(venv) ➜ parser_plugin ansible-playbook site.yml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [localhost] ***************************************************************************************************************************************************
TASK [set_fact] ****************************************************************************************************************************************************
ok: [localhost]
TASK [set_fact] ****************************************************************************************************************************************************
ok: [localhost]
TASK [debug] *******************************************************************************************************************************************************
ok: [localhost] => {
"result": {
"Ethernet1": {
"hardware": "Ethernet",
"mac": "5254.0033.62f8",
"name": "Ethernet1",
"status": {
"administrative": "up",
"operating": "up"
}
},
"Ethernet2": {
"hardware": "Ethernet",
"mac": "5254.0017.143f",
"name": "Ethernet2",
"status": {
"administrative": "up",
"operating": "up"
}
},
"Management1": {
"hardware": "Ethernet",
"mac": "5254.0074.620b",
"name": "Management1",
"status": {
"administrative": "up",
"operating": "up"
}
}
}
}
PLAY RECAP *********************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
(venv) ➜ parser_plugin
- hosts: localhost
gather_facts: False
tasks:
- set_fact:
output: >
Ethernet1 is up, line protocol is up (connected)
Hardware is Ethernet, address is 5254.0033.62f8 (bia 5254.0033.62f8)
Ethernet2 is up, line protocol is up (connected)
Hardware is Ethernet, address is 5254.0017.143f (bia 5254.0017.143f)
Management1 is up, line protocol is up (connected)
Hardware is Ethernet, address is 5254.0074.620b (bia 5254.0074.620b)
- set_fact:
result: "{{ output.splitlines()|parse_eos_show_interface }}"
- debug:
var: result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment