dmulyalin / ttp

Template Text Parser
MIT License
350 stars 34 forks source link

Documentation issue - Templates lookup #88

Closed liposs closed 10 months ago

liposs commented 2 years ago

I noticed that the following code in the documentation is NOT providing the correct output:

 template_1 = """
<template name="interfaces">
<input load="text">
interface FastEthernet2.13
 description Customer CPE interface
 ip address 10.12.13.1 255.255.255.0
 vrf forwarding CPE-VRF
!
interface GigabitEthernet2.13
 description Customer CPE interface
 ip address 10.12.14.1 255.255.255.0
 vrf forwarding CUST1
!
</input>

<group name="{{ interface }}">
interface {{ interface }}
 description {{ description | ORPHRASE }}
 ip address {{ subnet | PHRASE | to_ip | network | to_str }}
 vrf forwarding {{ vrf }}
</group>
</template>

<template name="arp">
<input load="text">
Protocol  Address     Age (min)  Hardware Addr   Type   Interface
Internet  10.12.13.2        98   0950.5785.5cd1  ARPA   FastEthernet2.13
Internet  10.12.14.3       131   0150.7685.14d5  ARPA   GigabitEthernet2.13
</input>

<group lookup="interface, template='interfaces', update=True">
Internet  {{ ip }}  {{ age | DIGIT }}   {{ mac }}  ARPA   {{ interface }}
</group>
</template>
"""

The output should be:

        [
            {
                "FastEthernet2.13": {
                    "description": "Customer CPE interface",
                    "subnet": "10.12.13.0/24",
                    "vrf": "CPE-VRF",
                },
                "GigabitEthernet2.13": {
                    "description": "Customer CPE interface",
                    "subnet": "10.12.14.0/24",
                    "vrf": "CUST1",
                },
            }
        ],
        [
            [
                {
                    "age": "98",
                    "description": "Customer CPE interface",
                    "interface": "FastEthernet2.13",
                    "ip": "10.12.13.2",
                    "mac": "0950.5785.5cd1",
                    "subnet": "10.12.13.0/24",
                    "vrf": "CPE-VRF",
                },
                {
                    "age": "131",
                    "description": "Customer CPE interface",
                    "interface": "GigabitEthernet2.13",
                    "ip": "10.12.14.3",
                    "mac": "0150.7685.14d5",
                    "subnet": "10.12.14.0/24",
                    "vrf": "CUST1",
                },
            ]

But instead it's:

[
    {
        "FastEthernet2.13": {
            "description": "Customer CPE interface",
            "subnet": "10.12.13.0/24",
            "vrf": "CPE-VRF"
        },
        "GigabitEthernet2.13": {
            "description": "Customer CPE interface",
            "subnet": "10.12.14.0/24",
            "vrf": "CUST1"
        }
    },
    {}
]

How to do the correct lookup between templates? What's the correct code for the documentation?

Tested: https://textfsm.nornir.tech/#

dmulyalin commented 2 years ago

Hello, thank you for rasing this issue, it seems work fine if I run it locally:

C:\>more test.txt
<template name="interfaces">
<input load="text">
interface FastEthernet2.13
 description Customer CPE interface
 ip address 10.12.13.1 255.255.255.0
 vrf forwarding CPE-VRF
!
interface GigabitEthernet2.13
 description Customer CPE interface
 ip address 10.12.14.1 255.255.255.0
 vrf forwarding CUST1
!
</input>

<group name="{{ interface }}">
interface {{ interface }}
 description {{ description | ORPHRASE }}
 ip address {{ subnet | PHRASE | to_ip | network | to_str }}
 vrf forwarding {{ vrf }}
</group>
</template>

<template name="arp">
<input load="text">
Protocol  Address     Age (min)  Hardware Addr   Type   Interface
Internet  10.12.13.2        98   0950.5785.5cd1  ARPA   FastEthernet2.13
Internet  10.12.14.3       131   0150.7685.14d5  ARPA   GigabitEthernet2.13
</input>

<group lookup="interface, template='interfaces', update=True">
Internet  {{ ip }}  {{ age | DIGIT }}   {{ mac }}  ARPA   {{ interface }}
</group>
</template>

C:\>ttp -t test.txt -o json
[
    [
        {
            "FastEthernet2.13": {
                "description": "Customer CPE interface",
                "subnet": "10.12.13.0/24",
                "vrf": "CPE-VRF"
            },
            "GigabitEthernet2.13": {
                "description": "Customer CPE interface",
                "subnet": "10.12.14.0/24",
                "vrf": "CUST1"
            }
        }
    ],
    [
        [
            {
                "age": "98",
                "description": "Customer CPE interface",
                "interface": "FastEthernet2.13",
                "ip": "10.12.13.2",
                "mac": "0950.5785.5cd1",
                "subnet": "10.12.13.0/24",
                "vrf": "CPE-VRF"
            },
            {
                "age": "131",
                "description": "Customer CPE interface",
                "interface": "GigabitEthernet2.13",
                "ip": "10.12.14.3",
                "mac": "0150.7685.14d5",
                "subnet": "10.12.14.0/24",
                "vrf": "CUST1"
            }
        ]
    ]
]

C:\s>ttp -h
Template Text Parser, version 0.9.0