In [228]:
# The line below can be ignored but I didn't set up my environment properly
import sys ; sys.path.append('/home/mjuenemann/.virtualenvs/ciscoconfparse/lib/python3.6/site-packages')
In [229]:
import ciscoconfparse
In [230]:
ansible_net_config = """
!
hostname router01
!
tacacs-server host 192.0.2.34
tacacs-server key cheezit
!
interface Ethernet2/0
description Unprotected interface, facing towards Internet
ip address 192.0.2.14 255.255.255.240
no ip unreachables
ntp disable
no mop enable
mtu 900
!
interface Ethernet2/1
description Protected interface, facing towards DMZ
ip address 192.0.2.17 255.255.255.240
no mop enable
ip access-list extended LIST1
allow ip host 10.1.1.1 host 10.99.99.99
allow ip host 10.9.9.9 host 10.99.99.99
allow ip host 10.1.1.1 host 192.168.1.1
!
ip access-list extended LIST2
allow ip host 10.1.1.1 host 10.99.99.99
allow ip host 10.1.1.1 host 172.16.1.1
"""
In [231]:
def _parse_config(config):
return ciscoconfparse.CiscoConfParse(config.split('\n'))
def ioscfg_lines(config, lineregex):
cfg = _parse_config(config)
return [ioscfgline.text for ioscfgline in cfg.find_objects(lineregex)]
def ioscfg_lines_with_child(config, parentregex, childregex):
cfg = _parse_config(config)
return [ioscfgline.text for ioscfgline in cfg.find_objects_w_child(parentregex, childregex)]
def ioscfg_lines_and_child(config, parentregex, childregex):
import re
cfg = _parse_config(config)
return [(ioscfgline.text, [child.text for child in ioscfgline.children
if re.search(childregex, child.text)], )
for ioscfgline in cfg.find_objects_w_child(parentregex, childregex)]
def ioscfg_lines_without_child(config, parentregex, childregex):
cfg = _parse_config(config)
return [ioscfgline.text for ioscfgline in cfg.find_objects_wo_child(parentregex, childregex)]
def ioscfg_lines_with_children(config, parentregex, childregexes):
cfg = _parse_config(config)
return [ioscfgline.text for ioscfgline in cfg.find_objects_w_all_children(parentregex, childregexes)]
def ioscfg_lines_without_children(config, parentregex, childregexes):
cfg = _parse_config(config)
return [ioscfgline.text for ioscfgline in cfg.find_objects_w_missing_children(parentregex, childregexes)]
def ioscfg_lines_with_parents(config, parentregex, childregex):
cfg = _parse_config(config)
return [ioscfgline.text for ioscfgline in cfg.find_objects_w_parents(parentregex, childregex)]
class FilterModule(object):
def filters(self):
return {
'ioscfg_lines': ioscfg_lines,
'ioscfg_lines_with_child': ioscfg_lines_with_child,
'ioscfg_lines_and_child': ioscfg_lines_and_child,
'ioscfg_lines_without_child': ioscfg_lines_without_child,
'ioscfg_lines_with_children': ioscfg_lines_with_children,
'ioscfg_lines_without_children': ioscfg_lines_without_children,
'ioscfg_lines_with_parents': ioscfg_lines_with_parents
}
In [232]:
ioscfg_lines(ansible_net_config, r'^tacacs')
Out[232]:
In [233]:
ioscfg_lines_with_child(ansible_net_config, r'^interface', r'no mop enable')
Out[233]:
In [234]:
ioscfg_lines_and_child(ansible_net_config, r'^ip access-list extended', r'10.99.99.99')
Out[234]:
In [235]:
ioscfg_lines_without_child(ansible_net_config, r'^interface', r'no ip unreachables')
Out[235]:
In [236]:
ioscfg_lines_with_children(ansible_net_config, r'^interface', [r'no ip unreachables', r'no mop enable'])
Out[236]:
In [237]:
ioscfg_lines_without_children(ansible_net_config, r'^interface', [r'no ip unreachables', r'no mop enable'])
Out[237]:
In [238]:
ioscfg_lines_with_parents(ansible_net_config, r'^interface', r'no mop enable')
Out[238]:
In [ ]: