ansible.utils.cli_parse 模块 – 使用各种解析器解析 CLI 输出或文本

注意

此模块是 ansible.utils 集合 (版本 5.1.2) 的一部分。

如果您使用的是 ansible 包,则可能已经安装了此集合。它不包含在 ansible-core 中。要检查它是否已安装,请运行 ansible-galaxy collection list

要安装它,请使用: ansible-galaxy collection install ansible.utils

要在剧本中使用它,请指定: ansible.utils.cli_parse

ansible.utils 1.0.0 中的新功能

概要

  • 使用各种解析器解析 CLI 输出或文本

注意

此模块具有相应的 action 插件

参数

参数

注释

command

字符串

要在主机上运行的命令

parser

字典 / 必需

解析器特定参数

command

字符串

用于查找解析器模板的命令

name

字符串 / 必需

要使用的解析器的名称

os

字符串

向解析器提供操作系统值。

对于 `ntc_templates` 解析器,这应该采用受支持的 `_` 格式。

template_path

字符串

Ansible 控制器上解析器模板的路径

这可以是相对路径或绝对路径

vars

字典

其他解析器特定参数

有关解析器特定变量的示例,请参阅 cli_parse 用户指南

https://docs.ansible.org.cn/ansible/latest/network/user_guide/cli_parsing.html

set_fact

字符串

将生成的解析数据设置为事实

text

字符串

要解析的文本

备注

注意

  • 解析器模板的默认搜索路径为 templates/{{ short_os }}_{{ command }}.{{ extension }}

  • => short_os 来自 ansible_network_os 或 ansible_distribution,并设置为小写

  • => command 是传递给模块的命令,空格替换为 _

  • => extension 是所用解析器特有的 (native=yaml, textfsm=textfsm, ttp=ttp)

  • 默认的 Ansible 模板目录搜索路径也用于解析器模板

  • 某些解析器可能具有其他可用的配置选项。请参阅 parsers/vars 键和解析器的文档

  • 某些解析器需要在 Ansible 控制节点上安装第三方 python 库和特定的 python 版本

  • 例如,Pyats 需要 pyats 和 genie,并需要 Python 3

  • 例如,ntc_templates 需要 ntc_templates

  • 例如,textfsm 需要 textfsm

  • 例如,ttp 需要 ttp

  • 例如,xml 需要 xml_to_dict

  • 对第三方 python 库的支持仅限于使用其已记录的公共 API

  • 可以在解析用户指南中找到更多信息和示例

  • https://docs.ansible.org.cn/ansible/latest/network/user_guide/cli_parsing.html

示例

# Using the native parser

# -------------
# templates/nxos_show_interface.yaml
# - example: Ethernet1/1 is up
#   getval: '(?P<name>\S+) is (?P<oper_state>\S+)'
#   result:
#     "{{ name }}":
#         name: "{{ name }}"
#         state:
#         operating: "{{ oper_state }}"
#   shared: True
#
# - example: admin state is up, Dedicated Interface
#   getval: 'admin state is (?P<admin_state>\S+)'
#   result:
#     "{{ name }}":
#         name: "{{ name }}"
#         state:
#         admin: "{{ admin_state }}"
#
# - example: "  Hardware: Ethernet, address: 0000.5E00.5301 (bia 0000.5E00.5301)"
#   getval: '\s+Hardware: (?P<hardware>.*), address: (?P<mac>\S+)'
#   result:
#     "{{ name }}":
#         hardware: "{{ hardware }}"
#         mac_address: "{{ mac }}"

- name: Run command and parse with native
  ansible.utils.cli_parse:
    command: "show interface"
    parser:
      name: ansible.netcommon.native
    set_fact: interfaces_fact


- name: Pass text and template_path
  ansible.utils.cli_parse:
    text: "{{ previous_command['stdout'] }}"
    parser:
      name: ansible.netcommon.native
      template_path: "{{ role_path }}/templates/nxos_show_interface.yaml"


# Using the ntc_templates parser

# -------------
# The ntc_templates use 'vendor_platform' for the file name
# it will be derived from ansible_network_os if not provided
# example cisco.ios.ios => cisco_ios

- name: Run command and parse with ntc_templates
  ansible.utils.cli_parse:
    command: "show interface"
    parser:
      name: ansible.netcommon.ntc_templates
  register: parser_output

- name: Pass text and command
  ansible.utils.cli_parse:
    text: "{{ previous_command['stdout'] }}"
    parser:
      name: ansible.netcommon.ntc_templates
      command: show interface
  register: parser_output


# Using the pyats parser

# -------------
# The pyats parser uses 'os' to locate the appropriate parser
# it will be derived from ansible_network_os if not provided
# in the case of pyats: cisco.ios.ios => iosxe

- name: Run command and parse with pyats
  ansible.utils.cli_parse:
    command: "show interface"
    parser:
      name: ansible.netcommon.pyats
  register: parser_output

- name: Pass text and command
  ansible.utils.cli_parse:
    text: "{{ previous_command['stdout'] }}"
    parser:
      name: ansible.netcommon.pyats
      command: show interface
  register: parser_output

- name: Provide an OS to pyats to use an ios parser
  ansible.utils.cli_parse:
    text: "{{ previous_command['stdout'] }}"
    parser:
      name: ansible.netcommon.pyats
      command: show interface
      os: ios
  register: parser_output


# Using the textfsm parser

# -------------
# templates/nxos_show_version.textfsm
#
# Value UPTIME ((\d+\s\w+.s.,?\s?){4})
# Value LAST_REBOOT_REASON (.+)
# Value OS (\d+.\d+(.+)?)
# Value BOOT_IMAGE (.*)
# Value PLATFORM (\w+)
#
# Start
#   ^\s+(NXOS: version|system:\s+version)\s+${OS}\s*$$
#   ^\s+(NXOS|kickstart)\s+image\s+file\s+is:\s+${BOOT_IMAGE}\s*$$
#   ^\s+cisco\s+${PLATFORM}\s+[cC]hassis
#   ^\s+cisco\s+Nexus\d+\s+${PLATFORM}
#   # Cisco N5K platform
#   ^\s+cisco\s+Nexus\s+${PLATFORM}\s+[cC]hassis
#   ^\s+cisco\s+.+-${PLATFORM}\s*
#   ^Kernel\s+uptime\s+is\s+${UPTIME}
#   ^\s+Reason:\s${LAST_REBOOT_REASON} -> Record

- name: Run command and parse with textfsm
  ansible.utils.cli_parse:
    command: "show version"
    parser:
      name: ansible.utils.textfsm
  register: parser_output

- name: Pass text and command
  ansible.utils.cli_parse:
    text: "{{ previous_command['stdout'] }}"
    parser:
      name: ansible.utils.textfsm
      command: show version
  register: parser_output

# Using the ttp parser

# -------------
# templates/nxos_show_interface.ttp
#
# {{ interface }} is {{ state }}
# admin state is {{ admin_state }}{{ ignore(".*") }}

- name: Run command and parse with ttp
  ansible.utils.cli_parse:
    command: "show interface"
    parser:
      name: ansible.utils.ttp
    set_fact: new_fact_key

- name: Pass text and template_path
  ansible.utils.cli_parse:
    text: "{{ previous_command['stdout'] }}"
    parser:
      name: ansible.utils.ttp
      template_path: "{{ role_path }}/templates/nxos_show_interface.ttp"
  register: parser_output

# Using the XML parser

# -------------
- name: Run command and parse with xml
  ansible.utils.cli_parse:
    command: "show interface | xml"
    parser:
      name: ansible.utils.xml
  register: parser_output

- name: Pass text and parse with xml
  ansible.utils.cli_parse:
    text: "{{ previous_command['stdout'] }}"
    parser:
      name: ansible.utils.xml
  register: parser_output

返回值

此处记录了常见的返回值 此处,以下是此模块独有的字段

描述

parsed

字典

文本解析后生成的结构化数据

返回:始终

stdout

字符串

运行命令的输出

返回:提供命令时

stdout_lines

列表 / 元素=字符串

运行命令的输出,分成多行

返回:提供命令时

作者

  • Bradley Thornton (@cidrblock)