I'm trying to set up a TextFSM template for NTC ansible which will only pull the Vlans allowed on the trunk from the output of a "show interface trunk" command and can't seem to get what I want. It is giving me all the lines instead of just the single line that I want. The output of the command looks like this:
switch#sh int g9/17 trunk
Port Mode Encapsulation Status Native vlan
Gi9/17 on 802.1q trunking 1
Port Vlans allowed on trunk
Gi9/17 501,503,513,540,950-957
Port Vlans allowed and active in management domain
Gi9/17 501,503,513,540,950-957
Port Vlans in spanning tree forwarding state and not pruned
Gi9/17 501,503,513,540,950-957
In this output, I only want to return the line below "Vlans allowed on trunk" and not the other repeat lines that have the same info. My template looks like this:
Value PORT (\S+)
Value VLANS (.*)
Start
^Port.*Vlans allowed on trunk -> Begin
Begin
^(?=\s{0,9}${PORT})\s+${VLANS} -> Record
^Port.*Vlans allowed and active in management domain -> End
Make regular expressions more specific and get the desired result (maybe :).
import io
import textfsm
template = io.StringIO("""\
Value Port (\S+(/\d+)?)
Value Vlans (\d+([-,]\d+)+)
Start
^Port\s+Vlans allowed on trunk$$ -> Begin
Begin
^${Port}\s+${Vlans}$$ -> Record
^Port\s+Vlans allowed and active in management domain$$ -> End
""")
fsm = textfsm.TextFSM(template)
result = fsm.ParseText("""\
switch#sh int g9/17 trunk
Port Mode Encapsulation Status Native vlan
Gi9/17 on 802.1q trunking 1
Port Vlans allowed on trunk
Gi9/17 501,503,513,540,950-957
Port Vlans allowed and active in management domain
Gi9/17 501,503,513,540,950-957
Port Vlans in spanning tree forwarding state and not pruned
Gi9/17 501,503,513,540,950-957
""")
print(result)
[['Gi9/17', '501,503,513,540,950-957']]