Add docs for cli_parse and fact_diff (#29)

Add docs for cli_parse and fact_diff

Reviewed-by: https://github.com/apps/ansible-zuul
pull/30/head
Ganesh Nalawade 2020-12-08 09:02:45 +05:30 committed by GitHub
parent c9434fa95f
commit f1954296eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 605 additions and 10 deletions

View File

@ -37,6 +37,7 @@ Name | Description
### Modules
Name | Description
--- | ---
[ansible.utils.cli_parse](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.cli_parse_module.rst)|Parse cli output or text using a variety of parsers
[ansible.utils.fact_diff](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.fact_diff_module.rst)|Find the difference between currently set facts
[ansible.utils.update_fact](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.update_fact_module.rst)|Update currently set facts
[ansible.utils.validate](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.validate_module.rst)|Validate data with provided criteria

View File

@ -0,0 +1,3 @@
---
trivial:
- Add docs for cli_parse and fact_diff (https://github.com/ansible-collections/ansible.utils/pull/29)

View File

@ -0,0 +1,460 @@
.. _ansible.utils.cli_parse_module:
***********************
ansible.utils.cli_parse
***********************
**Parse cli output or text using a variety of parsers**
Version added: 1.0.0
.. contents::
:local:
:depth: 1
Synopsis
--------
- Parse cli output or text using a variety of parsers
Parameters
----------
.. raw:: html
<table border=0 cellpadding=0 class="documentation-table">
<tr>
<th colspan="2">Parameter</th>
<th>Choices/<font color="blue">Defaults</font></th>
<th width="100%">Comments</th>
</tr>
<tr>
<td colspan="2">
<div class="ansibleOptionAnchor" id="parameter-"></div>
<b>command</b>
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
<div style="font-size: small">
<span style="color: purple">string</span>
</div>
</td>
<td>
</td>
<td>
<div>The command to run on the host</div>
</td>
</tr>
<tr>
<td colspan="2">
<div class="ansibleOptionAnchor" id="parameter-"></div>
<b>parser</b>
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
<div style="font-size: small">
<span style="color: purple">dictionary</span>
/ <span style="color: red">required</span>
</div>
</td>
<td>
</td>
<td>
<div>Parser specific parameters</div>
</td>
</tr>
<tr>
<td class="elbow-placeholder"></td>
<td colspan="1">
<div class="ansibleOptionAnchor" id="parameter-"></div>
<b>command</b>
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
<div style="font-size: small">
<span style="color: purple">string</span>
</div>
</td>
<td>
</td>
<td>
<div>The command used to locate the parser&#x27;s template</div>
</td>
</tr>
<tr>
<td class="elbow-placeholder"></td>
<td colspan="1">
<div class="ansibleOptionAnchor" id="parameter-"></div>
<b>name</b>
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
<div style="font-size: small">
<span style="color: purple">string</span>
/ <span style="color: red">required</span>
</div>
</td>
<td>
</td>
<td>
<div>The name of the parser to use</div>
</td>
</tr>
<tr>
<td class="elbow-placeholder"></td>
<td colspan="1">
<div class="ansibleOptionAnchor" id="parameter-"></div>
<b>os</b>
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
<div style="font-size: small">
<span style="color: purple">string</span>
</div>
</td>
<td>
</td>
<td>
<div>Provide an operating system value to the parser</div>
<div>For `ntc_templates` parser, this should be in the supported `&lt;vendor&gt;_&lt;os&gt;` format.</div>
</td>
</tr>
<tr>
<td class="elbow-placeholder"></td>
<td colspan="1">
<div class="ansibleOptionAnchor" id="parameter-"></div>
<b>template_path</b>
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
<div style="font-size: small">
<span style="color: purple">string</span>
</div>
</td>
<td>
</td>
<td>
<div>Path of the parser template on the Ansible controller</div>
<div>This can be a relative or an absolute path</div>
</td>
</tr>
<tr>
<td class="elbow-placeholder"></td>
<td colspan="1">
<div class="ansibleOptionAnchor" id="parameter-"></div>
<b>vars</b>
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
<div style="font-size: small">
<span style="color: purple">dictionary</span>
</div>
</td>
<td>
</td>
<td>
<div>Additional parser specific parameters</div>
<div>See the cli_parse user guide for examples of parser specific variables</div>
<div><a href='https://docs.ansible.com/ansible/latest/network/user_guide/cli_parsing.html'>https://docs.ansible.com/ansible/latest/network/user_guide/cli_parsing.html</a></div>
</td>
</tr>
<tr>
<td colspan="2">
<div class="ansibleOptionAnchor" id="parameter-"></div>
<b>set_fact</b>
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
<div style="font-size: small">
<span style="color: purple">string</span>
</div>
</td>
<td>
</td>
<td>
<div>Set the resulting parsed data as a fact</div>
</td>
</tr>
<tr>
<td colspan="2">
<div class="ansibleOptionAnchor" id="parameter-"></div>
<b>text</b>
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
<div style="font-size: small">
<span style="color: purple">string</span>
</div>
</td>
<td>
</td>
<td>
<div>Text to be parsed</div>
</td>
</tr>
</table>
<br/>
Notes
-----
.. note::
- The default search path for a parser template is templates/{{ short_os }}_{{ command }}.{{ extension }}
- => short_os derived from ansible_network_os or ansible_distribution and set to lower case
- => command is the command passed to the module with spaces replaced with _
- => extension is specific to the parser used (native=yaml, textfsm=textfsm, ttp=ttp)
- The default Ansible search path for the templates directory is used for parser templates as well
- Some parsers may have additional configuration options available. See the parsers/vars key and the parser's documentation
- Some parsers require third-party python libraries be installed on the Ansible control node and a specific python version
- e.g. Pyats requires pyats and genie and requires Python 3
- e.g. ntc_templates requires ntc_templates
- e.g. textfsm requires textfsm
- e.g. ttp requires ttp
- e.g. xml requires xml_to_dict
- Support of 3rd party python libraries is limited to the use of their public APIs as documented
- Additional information and examples can be found in the parsing user guide:
- https://docs.ansible.com/ansible/latest/network/user_guide/cli_parsing.html
Examples
--------
.. code-block:: yaml
# 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
# e.g. 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
Return Values
-------------
Common return values are documented `here <https://docs.ansible.com/ansible/latest/reference_appendices/common_return_values.html#common-return-values>`_, the following are the fields unique to this module:
.. raw:: html
<table border=0 cellpadding=0 class="documentation-table">
<tr>
<th colspan="1">Key</th>
<th>Returned</th>
<th width="100%">Description</th>
</tr>
<tr>
<td colspan="1">
<div class="ansibleOptionAnchor" id="return-"></div>
<b>parsed</b>
<a class="ansibleOptionLink" href="#return-" title="Permalink to this return value"></a>
<div style="font-size: small">
<span style="color: purple">dictionary</span>
</div>
</td>
<td>always</td>
<td>
<div>The structured data resulting from the parsing of the text</div>
<br/>
</td>
</tr>
<tr>
<td colspan="1">
<div class="ansibleOptionAnchor" id="return-"></div>
<b>stdout</b>
<a class="ansibleOptionLink" href="#return-" title="Permalink to this return value"></a>
<div style="font-size: small">
<span style="color: purple">string</span>
</div>
</td>
<td>when provided a command</td>
<td>
<div>The output from the command run</div>
<br/>
</td>
</tr>
<tr>
<td colspan="1">
<div class="ansibleOptionAnchor" id="return-"></div>
<b>stdout_lines</b>
<a class="ansibleOptionLink" href="#return-" title="Permalink to this return value"></a>
<div style="font-size: small">
<span style="color: purple">list</span>
</div>
</td>
<td>when provided a command</td>
<td>
<div>The output of the command run split into lines</div>
<br/>
</td>
</tr>
</table>
<br/><br/>
Status
------
Authors
~~~~~~~
- Bradley Thornton (@cidrblock)

View File

@ -7,6 +7,33 @@ from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = """
author: Bradley Thornton (@cidrblock)
name: json
short_description: Define configurable options for C(json) sub-plugin of C(cli_parse) module
description:
- This plugin documentation provides the configurable options that can be passed
to the I(ansible.utils.cli_parse) plugins when I(ansible.utils.json) is used as a value for
I(name) option.
version_added: 1.0.0
"""
EXAMPLES = r"""
- name: "Run command and parse with json"
ansible.utils.cli_parse:
command: "show version | json"
parser:
name: ansible.utils.json
register: nxos_json_command
- name: "Load text and parse with json"
ansible.utils.cli_parse:
text: "{{ lookup('file', './nxos_show_interface_json_text.txt') }}"
parser:
name: ansible.utils.json
register: nxos_json_text
"""
import json
from ansible.module_utils._text import to_native

View File

@ -8,6 +8,34 @@ from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = """
author: Bradley Thornton (@cidrblock)
name: textfsm
short_description: Define configurable options for C(textfsm) sub-plugin of C(cli_parse) module
description:
- This plugin documentation provides the configurable options that can be passed
to the I(ansible.utils.cli_parse) plugins when I(ansible.utils.textfsm) is used as a value for
I(name) option.
version_added: 1.0.0
"""
EXAMPLES = r"""
- name: "Run command and parse with textfsm"
ansible.utils.cli_parse:
command: "show version"
parser:
name: ansible.utils.textfsm
register: nxos_textfsm_command
- name: "Pass text and command"
ansible.utils.cli_parse:
text: "{{ lookup('file', '/home/user/files/nxos_show_version.txt') }}"
parser:
name: ansible.utils.textfsm
template_path: "/home/user/templates/nxos_show_version.textfsm"
register: nxos_textfsm_text
"""
import os
from ansible.module_utils._text import to_native

View File

@ -8,6 +8,34 @@ from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = """
author: Bradley Thornton (@cidrblock)
name: ttp
short_description: Define configurable options for C(ttp) sub-plugin of C(cli_parse) module
description:
- This plugin documentation provides the configurable options that can be passed
to the I(ansible.utils.cli_parse) plugins when I(ansible.utils.ttp) is used as a value for
I(name) option.
version_added: 1.0.0
"""
EXAMPLES = r"""
- name: "Run command and parse with textfsm"
ansible.utils.cli_parse:
command: "show version"
parser:
name: ansible.utils.ttp
register: nxos_ttp_command
- name: "Pass text and command"
ansible.utils.cli_parse:
text: "{{ lookup('file', '/home/user/files/nxos_show_version.txt') }}"
parser:
name: ansible.utils.textfsm
template_path: "/home/user/templates/nxos_show_version.ttp"
register: nxos_ttp_text
"""
import os
from ansible.module_utils._text import to_native

View File

@ -8,6 +8,34 @@ from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = """
author: Bradley Thornton (@cidrblock)
name: xml
short_description: Define configurable options for C(xml) sub-plugin of C(cli_parse) module
description:
- This plugin documentation provides the configurable options that can be passed
to the I(ansible.utils.cli_parse) plugins when I(ansible.utils.xml) is used as a value for
I(name) option.
version_added: 1.0.0
"""
EXAMPLES = r"""
- name: "Run command and parse with xml"
ansible.utils.cli_parse:
command: "show interface | xml"
parser:
name: ansible.utils.xml
register: nxos_xml_command
- name: "Pass text and parse with xml"
ansible.utils.cli_parse:
text: "{{ lookup('file', '/home/user/files/nxos_show_interface.xml') }}"
parser:
name: ansible.utils.xml
os: nxos
register: nxos_xml_text
"""
from ansible.module_utils._text import to_native
from ansible.module_utils.basic import missing_required_lib
from ansible_collections.ansible.utils.plugins.cli_parsers._base import (

View File

@ -7,6 +7,26 @@ from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = """
author: Bradley Thornton (@cidrblock)
name: native
short_description: Define configurable options for C(native) sub-plugin of C(fact_diff) module
description:
- This plugin documentation provides the configurable options that can be passed
to the I(ansible.utils.fact_diff) plugins when I(ansible.utils.native) is used as a value for
I(name) option of the module.
version_added: 1.0.0
"""
EXAMPLES = r"""
- name: Show the difference in json format
ansible.utils.fact_diff:
before: "{{ before }}"
after: "{{ after }}"
plugin:
name: ansible.utils.native
"""
import re
from ansible.plugins.callback import CallbackBase
from ansible_collections.ansible.utils.plugins.module_utils.base_classes.fact_diff import (

View File

@ -12,7 +12,7 @@ from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = """
filter: get_path
name: get_path
author: Bradley Thornton (@cidrblock)
version_added: "1.0.0"
short_description: Retrieve the value in a variable using a path

View File

@ -12,7 +12,7 @@ from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = """
filter: index_of
name: index_of
author: Bradley Thornton (@cidrblock)
version_added: "1.0.0"
short_description: Find the indices of items in a list matching some criteria

View File

@ -13,7 +13,7 @@ __metaclass__ = type
DOCUMENTATION = """
filter: to_paths
name: to_paths
author: Bradley Thornton (@cidrblock)
version_added: "1.0.0"
short_description: Flatten a complex object into a dictionary of paths and values

View File

@ -3,7 +3,7 @@ from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = """
filter: validate
name: validate
author: Ganesh Nalawade (@ganeshrn)
version_added: "1.0.0"
short_description: Validate data with provided criteria

View File

@ -13,7 +13,7 @@ __metaclass__ = type
DOCUMENTATION = """
lookup: get_path
name: get_path
author: Bradley Thornton (@cidrblock)
version_added: "1.0.0"
short_description: Retrieve the value in a variable using a path

View File

@ -13,7 +13,7 @@ __metaclass__ = type
DOCUMENTATION = """
lookup: index_of
name: index_of
author: Bradley Thornton (@cidrblock)
version_added: "1.0.0"
short_description: Find the indices of items in a list matching some criteria

View File

@ -13,7 +13,7 @@ __metaclass__ = type
DOCUMENTATION = """
lookup: to_paths
name: to_paths
author: Bradley Thornton (@cidrblock)
version_added: "1.0.0"
short_description: Flatten a complex object into a dictionary of paths and values

View File

@ -8,7 +8,7 @@ from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = """
lookup: validate
name: validate
author: Ganesh Nalawade (@ganeshrn)
version_added: "1.0.0"
short_description: Validate data with provided criteria

View File

@ -7,7 +7,7 @@ from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = """
test: validate
name: validate
author: Ganesh Nalawade (@ganeshrn)
version_added: "1.0.0"
short_description: Validate data with provided criteria

View File

@ -9,7 +9,7 @@ __metaclass__ = type
DOCUMENTATION = """
author: Ganesh Nalawade (@ganeshrn)
validate: jsonschema
name: jsonschema
short_description: Define configurable options for jsonschema validate plugin
description:
- This plugin documentation provides the configurable options that can be passed