community.yang.generate_spec (1.1.0) — module

Generate JSON/XML schema and tree representation for given YANG model

Authors: Rohit Thakur (@rohitthakur2590)

This plugin has a corresponding action plugin.

Install collection

Install with ansible-galaxy collection install community.yang:==1.1.0


Add to requirements.yml

  collections:
    - name: community.yang
      version: 1.1.0

Description

The module will be read the given Yang model and generate the corresponding JSON, XML schema and the YANG tree representation (as per RFC 8340) of the model and return in the result and optionally store it in this individual files on control node.


Requirements

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: generate spec from openconfig interface data and in result
  community.yang.generate_spec:
    file: "openconfig/public/release/models/interfaces/openconfig-interfaces.yang"
    search_path: "{{ playbook_dir }}/openconfig/public/release/models:pyang/modules"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: generate spec from openconfig interface config data and store it in file
  community.yang.generate_spec:
    file: "openconfig/public/release/models/interfaces/openconfig-interfaces.yang"
    search_path: "{{ playbook_dir }}/openconfig/public/release/models:pyang/modules"
    doctype: config
    json_schema:
      path: "~/.ansible/yang/spec/{{ inventory_hostname }}/openconfig-interfaces-config.json"
      defaults: True
    xml_schema:
      path: "~/.ansible/yang/spec/{{ inventory_hostname }}/openconfig-interfaces-config.xml"
      defaults: True
      annotations: True
    tree_schema:
      path: "~/.ansible/yang/spec/{{ inventory_hostname }}/openconfig-interfaces-config.tree"

Inputs

    
file:
    description:
    - The file path of the top level YANG model for the spec should be generated. This
      option is mutually-exclusive with C(content) option.
    elements: path
    type: list

content:
    description:
    - The text content of the top level YANG model for the the should be generated. This
      option is mutually-exclusive with C(path) option.
    type: str

doctype:
    choices:
    - config
    - data
    default: config
    description:
    - Identifies the root node of the configuration skeleton. If value is C(config) only
      configuration data will be present in skeleton, if value is C(data) both config
      and state data fields will be present in output.

xml_schema:
    description: The options to control the way XML schema is generated
    suboptions:
      annotations:
        default: false
        description:
        - The boolean flag identifies if the XML skeleton should have comments describing
          the field or not.
        type: bool
      defaults:
        default: false
        description:
        - This boolean flag indicates if the generated XML configuration schema should
          have fields initialized with default values or not.
        type: bool
      path:
        description: The file path to which the generated XML schema should be stored.
        type: path
    type: dict

json_schema:
    description: The options to control the way JSON schema is generated
    suboptions:
      defaults:
        default: false
        description:
        - This boolean flag indicates if the generated JSON configuration schema should
          have fields initialized with default values or not.
        type: bool
      path:
        description: The file path to which the generated JSON schema should be stored.
        type: path
    type: dict

search_path:
    default: ~/.ansible/yang/spec
    description:
    - is a colon C(:) separated list of directories to search for imported yang modules
      in the yang file mentioned in C(path) option. If the value is not given it will
      search in the default directory path.
    type: path

tree_schema:
    description: The options to control the way tree schema is generated
    suboptions:
      path:
        description: The file path to which the generated tree schema should be stored.
        type: path
    type: dict

Outputs

json_schema:
  description: The json schema generated from yang document
  returned: always
  sample: "{\n    \"openconfig-interfaces:interfaces\": {\n        \"interface\":\
    \ [\n            {\n                \"hold-time\": {\n                    \"config\"\
    : {\n                        \"down\": \"\",\n                        \"up\":\
    \ \"\"\n                    }\n                },\n                \"config\"\
    : {\n                    \"description\": \"\",\n                    \"type\"\
    : \"\",\n                    \"enabled\": \"\",\n                    \"mtu\":\
    \ \"\",\n                    \"loopback-mode\": \"\",\n                    \"\
    name\": \"\"\n                },\n                \"name\": \"\",\n          \
    \      \"subinterfaces\": {\n                    \"subinterface\": [\n       \
    \                 {\n                            \"index\": \"\",\n          \
    \                  \"config\": {\n                                \"index\": \"\
    \",\n                                \"enabled\": \"\",\n                    \
    \            \"description\": \"\"\n                            }\n          \
    \              }\n                    ]\n                }\n            }\n  \
    \      ]\n    }\n"
  type: dict
tree_schema:
  description: The tree schema representation of yang scehma as per RFC 8340
  returned: always
  sample: "module: openconfig-interfaces\n  +--rw interfaces\n     +--rw interface*\
    \ [name]\n        +--rw name             -> ../config/name\n        +--rw config\n\
    \        |  +--rw name?            string\n        |  +--rw type             identityref\n\
    \        |  +--rw mtu?             uint16\n        |  +--rw loopback-mode?   boolean\n\
    \        |  +--rw description?     string\n        |  +--rw enabled?         boolean\n\
    \        +--ro state\n        |  +--ro name?            string\n        |  +--ro\
    \ type             identityref\n        |  +--ro mtu?             uint16\n   \
    \     |  +--ro loopback-mode?   boolean\n        |  +--ro description?     string\n\
    \        |  +--ro enabled?         boolean\n        |  +--ro ifindex?        \
    \ uint32\n        |  +--ro admin-status     enumeration\n        |  +--ro oper-status\
    \      enumeration\n        |  +--ro last-change?     oc-types:timeticks64\n"
  type: dict
xml_schema:
  description: The xml configuration schema generated from yang document
  returned: always
  sample: "<config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n  <interfaces\
    \ xmlns=\"http://openconfig.net/yang/interfaces\">\n    <interface>\n      <name/>\n\
    \      <config>\n        <name/>\n        <type/>\n        <mtu/>\n        <loopback-mode></loopback-mode>\n\
    \        <description/>\n        <enabled>True</enabled>\n      </config>\n  \
    \    <hold-time>\n        <config>\n          <up></up>\n          <down></down>\n\
    \        </config>\n      </hold-time>\n      <subinterfaces>\n        <subinterface>\n\
    \          <index/>\n          <config>\n            <index></index>\n       \
    \     <description/>\n            <enabled></enabled>\n          </config>\n \
    \       </subinterface>\n      </subinterfaces>\n    </interface>\n  </interfaces>\n\
    </config>\n"
  type: dict