ansible-templatepython-textfsm

Trying to combine lines using TextFSM


I'm trying to figure out a textfsm parser to process cisco ios object-lists for use with an Ansible playbook task. (Background: There isn't an ios module for object-groups, so I'm trying to do this manually. Yes, there is a ios.ios_acls, but it doesn't do object-gruops. Yes, the ASA has an object-group module but its not compatible.)

The example input and parser below comes close but I can't get the "LINES" to be combined in a list without some other aspect to break or align incorrectly.

My current parser:

Value Required,Filldown NAME ((network|service|v6-network|v6-service)\s(\S+))
Value List LINES (.*)

Start
  ^object-group -> Continue.Clearall
  ^object-group ${NAME}
  ^ ${LINES} -> Record

EOF

Example input data:

object-group network TEST-obj-1
host 192.168.1.11
host 192.168.1.12
host 192.168.1.13
host 192.168.1.14
object-group network TEST-obj-2
host 192.168.2.21
host 192.168.2.22
host 192.168.2.33
object-group service TEST-ports
tcp eq 22
tcp eq www
tcp eq 443
udp eq tftp
tcp eq telnet
udp eq 3389

Example output:

[
{
    "NAME": "network TEST-obj-1",
    "LINES": [
        "host 192.168.1.11"
    ]
},
{
    "NAME": "network TEST-obj-1",
    "LINES": [
        "host 192.168.1.12"
    ]
},
{
    "NAME": "network TEST-obj-1",
    "LINES": [
        "host 192.168.1.13"
    ]
},
{
    "NAME": "network TEST-obj-1",
    "LINES": [
        "host 192.168.1.14"
    ]
},
{
    "NAME": "network TEST-obj-2",
    "LINES": [
        "host 192.168.2.21"
    ]
},
{
    "NAME": "network TEST-obj-2",
    "LINES": [
        "host 192.168.2.22"
    ]
},
{
    "NAME": "network TEST-obj-2",
    "LINES": [
        "host 192.168.2.33"
    ]
},
{
    "NAME": "service TEST-ports",
    "LINES": [
        "tcp eq 22"
    ]
},
{
    "NAME": "service TEST-ports",
    "LINES": [
        "tcp eq www"
    ]
},
{
    "NAME": "service TEST-ports",
    "LINES": [
        "tcp eq 443"
    ]
},
{
    "NAME": "service TEST-ports",
    "LINES": [
        "udp eq tftp"
    ]
},
{
    "NAME": "service TEST-ports",
    "LINES": [
        "tcp eq telnet"
    ]
},
{
    "NAME": "service TEST-ports",
    "LINES": [
        "udp eq 3389"
    ]
}
]

Desired output, which should be easier to use in a Ansible ios.cli_command task:

[
{
    "NAME": "network TEST-obj-1",
    "LINES": [
        "host 192.168.1.11",
    "host 192.168.1.12",
        "host 192.168.1.13",
        "host 192.168.1.14"
    ]
},
{
    "NAME": "network TEST-obj-2",
    "LINES": [
        "host 192.168.2.21",
        "host 192.168.2.22",
        "host 192.168.2.23"

    ]
},
{
    "NAME": "service TEST-ports",
    "LINES": [
        "tcp eq 22",
        "tcp eq www",
        "tcp eq 443",
        "udp eq tftp",
        "tcp eq telnet",
        "udp eq 3389",
    ]
},
{
]

Solution

  • In the interest of saving some future person my pain; here is the solution that worked for me:

    Value GROUPNAME (object-group (network|service|v6-network|v6-service).*)
    Value List LINES ((host|tcp|udp).*)
    
    Start
      ^object-group -> Continue.Record
      ^${GROUPNAME}
      ^\s*${LINES}