def main():
    """entry point for module execution
    """
    argument_spec = dict(
        rpc=dict(type="str", required=True),
        xmlns=dict(type="str"),
        content=dict(),
        display=dict(choices=['json', 'pretty', 'xml'])
    )

    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=True)

    rpc = module.params['rpc']
    xmlns = module.params['xmlns']
    content = module.params['content']
    display = module.params['display']

    if rpc is None:
        module.fail_json(msg='argument `rpc` must not be None')

    rpc = rpc.strip()
    if len(rpc) == 0:
        module.fail_json(msg='argument `rpc` must not be empty')

    if rpc in ['close-session']:
        # explicit close-session is not allowed, as this would make the next
        # NETCONF operation to the same host fail
        module.fail_json(msg='unsupported operation `%s`' % rpc)

    if display == 'json' and not HAS_JXMLEASE:
        module.fail_json(msg='jxmlease is required to display response in json format'
                             'but does not appear to be installed. '
                             'It can be installed using `pip install jxmlease`')

    xml_req = get_xml_request(module, rpc, xmlns, content)
    response = dispatch(module, xml_req)

    xml_resp = tostring(response)
    output = None

    if display == 'xml':
        output = remove_namespaces(xml_resp)
    elif display == 'json':
        try:
            output = jxmlease.parse(xml_resp)
        except Exception:
            raise ValueError(xml_resp)
    elif display == 'pretty':
        output = tostring(response, pretty_print=True)

    result = {
        'stdout': xml_resp,
        'output': output
    }

    module.exit_json(**result)
Beispiel #2
0
def main():
    """entry point for module execution
    """
    argument_spec = dict(
        rpc=dict(type="str", required=True),
        xmlns=dict(type="str"),
        content=dict(),
        display=dict(choices=["json", "pretty", "xml"]),
    )

    module = AnsibleModule(
        argument_spec=argument_spec, supports_check_mode=True
    )

    rpc = module.params["rpc"]
    xmlns = module.params["xmlns"]
    content = module.params["content"]
    display = module.params["display"]

    if rpc is None:
        module.fail_json(msg="argument `rpc` must not be None")

    rpc = rpc.strip()
    if len(rpc) == 0:
        module.fail_json(msg="argument `rpc` must not be empty")

    if rpc in ["close-session"]:
        # explicit close-session is not allowed, as this would make the next
        # NETCONF operation to the same host fail
        module.fail_json(msg="unsupported operation `%s`" % rpc)

    if display == "json" and not HAS_JXMLEASE:
        module.fail_json(
            msg="jxmlease is required to display response in json format"
            "but does not appear to be installed. "
            "It can be installed using `pip install jxmlease`"
        )

    xml_req = get_xml_request(module, rpc, xmlns, content)
    response = dispatch(module, xml_req)

    xml_resp = tostring(response)
    output = None

    if display == "xml":
        output = remove_namespaces(xml_resp)
    elif display == "json":
        try:
            output = jxmlease.parse(xml_resp)
        except Exception:
            raise ValueError(xml_resp)
    elif display == "pretty":
        output = tostring(response, pretty_print=True)

    result = {"stdout": xml_resp, "output": output}

    module.exit_json(**result)