dmulyalin / ttp

Template Text Parser
MIT License
350 stars 34 forks source link

Handling a variable that conditionally appears in output to be parsed? #91

Closed netopsengineer closed 1 year ago

netopsengineer commented 2 years ago

@dmulyalin I have looked for examples and just can't seem to find what I'm looking for, but this is a common situation for me, where the router output may or may not contain a value, and it seems that ttp won't record the line if a var is in the template and doesn't show up. It seemed that perhaps you put both possible match statements in the template from examples I saw, but when I do that

One possible output:

line vty 0 4
 access-class ACL-V4-MANAGEMENT-IN in
< .. snipped >

Second possible output:

line vty 0 4
 access-class ACL-V4-MANAGEMENT-IN in vrf-also
< .. snipped >

ttp template(s) I have tried:

line {{ line | re(".+") | record(line) }}
 access-class {{ acl_in }} in {{ vrf_mgmt }}
 access-class {{ acl_out }} out
 transport input {{ input }}
 transport output {{ output }}
 password {{ password }}

This partially works, but splits the data structure up into something that is not ideal.

line {{ line | re(".+") | record(line) }}
 access-class {{ acl_in | _start_ }} in
 access-class {{ acl_in | _start_ }} in {{ vrf_mgmt }}
 access-class {{ acl_out }} out
 transport input {{ input }}
 transport output {{ output }}
 password {{ password }}
dmulyalin commented 2 years ago

Could you try this template:

line {{ line | re(".+") | record(line) }}
 access-class {{ acl_in }} in
 access-class {{ acl_in }} in {{ vrf_mgmt }}
 access-class {{ acl_out }} out
 transport input {{ input }}
 transport output {{ output }}
 password {{ password }}

start of the group line is always the same, which is line vty 0 4, no need to mark other template lines with _start_ indicator.

netopsengineer commented 1 year ago

This worked out well, thank you!