TextFSM Template
TextFSM templates should be placed in the ./templates directory and should adhere to the following NTC-Templates style. The TextFSM template name should be in the following format:
Naming
The template should be named using: {{ vendor_os }}_{{ command_with_underscores }}.textfsm
Ex: cisco_ios_show_cdp_neighbors.textfsm
Note: The vendor name must be valid from the os_choices tests, which is primarily based on Netmiko list of supported vendors. New vendors added should adhere to vendor_os.
Ex: vmware_nsx
Values
The capture group names should be in UPPERCASE.
An example of the proper format is shown below.
Value TIME (\d+:\d+:\d+)
Value TIMEZONE (\S+)
Value DAYWEEK (\w+)
Value MONTH (\d+)
Value DAY (\d+)
Value YEAR (\d+)
Start
^${TIME}\s+${TIMEZONE}\s+${DAYWEEK}\s+${DAY}/${MONTH}/${YEAR} -> Record
^. -> Error
States
If the raw output has a heading, the Start state should match on the column headings and then transition to another state that will match the device's output table with the capture groups. This helps ensure the regex patterns for the capture groups are attempting to match the correct information, and allows templates to easily add additional States for tables that have different headings. Example:
Raw Output
Total number of APs.............................. 1
Number of APs
Initiated....................................... 0
Downloading..................................... 0
Predownloading.................................. 0
Completed predownloading........................ 1
Not Supported................................... 0
Failed to Predownload........................... 0
Predownload Predownload Flexconnect
AP Name Primary Image Backup Image Status Version Next Retry Time Retry Count Predownload
------------------ -------------- -------------- --------------- -------------- ---------------- ------------ --------------
AAB1-LAB-AP1 8.8.130.0 8.5.160.0 Complete 8.8.130.0 NA NA
cisco_wlc_ssh_show_ap_image_all.textfsm
Value AP_NAME (\S+)
Value PRIMARY_IMAGE ([0-9\.]+)
Value BACKUP_IMAGE ([0-9\.]+)
Value PREDOWNLOAD_STATUS (\S+)
Value PREDOWNLOAD_VERSION ([0-9\.]+)
Value NEXT_RETRY_TIME (\S+)
Value RETRY_COUNT (\S+)
#Value FLEXCON_PREDOWN (\s+)
Start
^${AP_NAME}\s+${PRIMARY_IMAGE}\s+${BACKUP_IMAGE}\s+${PREDOWNLOAD_STATUS}\s+${PREDOWNLOAD_VERSION}\s+${NEXT_RETRY_TIME}\s+${RETRY_COUNT}\s*$$ -> Record
^Total\s+number\s+of\s+APs
^Number\s+of\s+APs
^.+\.+
^\s*$$
^Predownload\s+Predownload\s+Flexconnect
^AP\s+Name\s+Primary\s+Image\s+Backup\s+Image\s+Status\s+Version\s+Next\s+Retry\s+Time\s+Retry\s+Count\s+Predownload\s*$$
^-+
^. -> Error
Sample Template
Start
# Checking for header
^\s*Network\s+Next(?:\s+|-)[Hh]op\s+Metric\s+LocPrf\s+Weight\s+Path\s*$$ -> BGPTable
BGPTable
... omitted
Each state should end with ^. -> Error. This helps to ensure we're accounting for every line within the raw output for the command. This doesn't mean we have to capture all the data as a Value, but we do have to account for it. In addition, it is also good to provide an expression to match blank lines, ^\s*$$
An example would be the following raw output:
NAME: "3640 chassis", DESCR: "3640 chassis"
PID: , VID: 0xFF, SN: FF1045C5
The template would be the following:
Value NAME (.*)
Value DESCRIPTION (.*)
Start
^NAME:\s+"${NAME}",\s*DESCR:\s+"${DESCRIPTION}"
^PID:\s*,\s*VID:\s*\S+,\s*SN:\s*\S+
^\s*$$
^. -> Error
Taking a look at the example template above, you notice that we're using \s* and \s+. These are the preferred way to match space characters, and should be used over the literal space character. For example, This\s+is\s+preferred\s*$$ vs This is not preferred$$
\s* accounts for zero or more spaces (use when the output may or may not have a space between characters)
\s+ accounts for one or more spaces (use when output will have a space, but could have more than one space)