Esempio n. 1
0
def print_response(xapi, options):
    if options['print_xml']:
        if options['print_result']:
            s = xapi.xml_result()
        else:
            s = xapi.xml_root()
        if s is not None:
            print(s)

    if options['print_python'] or options['print_json']:
        if options['print_result']:
            if (xapi.element_result is None or
                    not len(xapi.element_result)):
                return
            elem = list(xapi.element_result)[0]
        else:
            if xapi.element_root is None:
                return
            elem = xapi.element_root

        try:
            conf = pan.config.PanConfig(config=elem)
        except pan.config.PanConfigError as msg:
            print('pan.config.PanConfigError:', msg, file=sys.stderr)
            sys.exit(1)

        d = conf.python()

        if d:
            if options['print_python']:
                print('var1 =', pprint.pformat(d))
            if options['print_json']:
                print(json.dumps(d, sort_keys=True, indent=2))
Esempio n. 2
0
def export_async(module, xapi, category, filename, interval=60, timeout=600):

    # Submit job, get resulting job id
    xapi.export(category=category)
    job_result = ET.fromstring(xapi.xml_root())

    job_id = None
    if job_result.find('.//job') is not None:
        job_id = job_result.find('.//job').text

    end_time = time.time() + timeout

    while True:
        # Check job progress
        xapi.export(category=category,
                    extra_qs={
                        'action': 'status',
                        'job-id': job_id
                    })
        poll_result = ET.fromstring(xapi.xml_root())

        status = poll_result.find('.//status')
        if status.text == "FIN":
            break

        if time.time() > end_time:
            module.fail_json(msg='Timeout')

        time.sleep(interval)

    # Get completed job
    xapi.export(category=category,
                extra_qs={
                    'action': 'get',
                    'job-id': job_id
                })
    export_binary(module, xapi, filename)
Esempio n. 3
0
def export_text(module, xapi, category, filename):
    xapi.export(category=category)

    f = None

    try:
        f = open(filename, 'w')
    except IOError as msg:
        module.fail_json(msg=msg)
    else:
        if category == 'configuration':
            f.write(xapi.xml_root())
        elif category in HTML_EXPORTS:
            f.write(xapi.text_document)

        f.close()
Esempio n. 4
0
def print_response(xapi, options):
    if options['print_xml']:
        if options['print_result']:
            s = xapi.xml_result()
        else:
            s = xapi.xml_root()
        if s is not None:
            print(s.lstrip('\r\n').rstrip())

    if options['print_python'] or options['print_json']:
        d = xml_python(xapi, options['print_result'])
        if d:
            if options['print_python']:
                print('var1 =', pprint.pformat(d))
            if options['print_json']:
                print(json.dumps(d, sort_keys=True, indent=2))

    if options['print_text'] and xapi.text_document is not None:
        print(xapi.text_document, end='')
Esempio n. 5
0
def print_response(xapi, options):
    if options['print_xml']:
        if options['print_result']:
            s = xapi.xml_result()
        else:
            s = xapi.xml_root()
        if s is not None:
            print(s)

    if options['print_python'] or options['print_json']:
        d = xml_python(xapi, options['print_result'])
        if d:
            if options['print_python']:
                print('var1 =', pprint.pformat(d))
            if options['print_json']:
                print(json.dumps(d, sort_keys=True, indent=2))

    if options['print_text'] and xapi.text_document is not None:
        print(xapi.text_document, end='')
def get_response(xapi):
    if options['print_xml']:
        if options['print_result']:
            s = xapi.xml_result()
        else:
            s = xapi.xml_root()
        if s is not None:
            return s.lstrip('\r\n').rstrip()

    if options['print_python'] or options['print_json']:
        d = xml_python(xapi, options['print_result'])
        if d:
            if options['print_python']:
                return 'var1 =', pprint.pformat(d)
            if options['print_json']:
                return json.dumps(d, sort_keys=True, indent=2)

    if options['print_text'] and xapi.text_document is not None:
        return xapi.text_document
Esempio n. 7
0
def print_response(xapi, options):
    if options['print_xml']:
        if options['print_result']:
            s = xapi.xml_result()
        else:
            s = xapi.xml_root()
        if s is not None:
            print(s)

    if options['print_python'] or options['print_json']:
        try:
            d = xapi.xml_python(options['print_result'])
        except pan.xapi.PanXapiError as msg:
            print('pan.xapi.PanXapi:', msg, file=sys.stderr)
            sys.exit(1)

        if d:
            if options['print_python']:
                print('var1 =', pprint.pformat(d))
            if options['print_json']:
                print(json.dumps(d, sort_keys=True, indent=2))
def main():
    argument_spec = dict(ip_address=dict(required=True, type='str'),
                         password=dict(fallback=(env_fallback,
                                                 ['ANSIBLE_NET_PASSWORD']),
                                       no_log=True),
                         username=dict(fallback=(env_fallback,
                                                 ['ANSIBLE_NET_USERNAME']),
                                       default="admin"),
                         interval=dict(default=0.5),
                         timeout=dict(),
                         sync=dict(type='bool', default=True),
                         description=dict(type='str'),
                         commit_changes_by=dict(type='list'),
                         commit_vsys=dict(type='list'))
    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=False)

    if not HAS_LIB:
        module.fail_json(msg='pan-python is required for this module')

    ip_address = module.params["ip_address"]
    if not ip_address:
        module.fail_json(msg="ip_address should be specified")

    password = module.params["password"]
    if not password:
        module.fail_json(msg="password is required")

    username = module.params['username']
    if not username:
        module.fail_json(msg="username is required")

    interval = module.params['interval']
    timeout = module.params['timeout']
    sync = module.params['sync']

    xapi = pan.xapi.PanXapi(hostname=ip_address,
                            api_username=username,
                            api_password=password)

    cmd = "<commit>"

    description = module.params["description"]
    if description:
        cmd += "<description>" + description + "</description>"

    commit_changes_by = module.params["commit_changes_by"]
    commit_vsys = module.params["commit_vsys"]

    if commit_changes_by or commit_vsys:

        cmd += "<partial>"

        if commit_changes_by:
            cmd += "<admin>"
            for admin in commit_changes_by:
                cmd += "<member>" + admin + "</member>"
            cmd += "</admin>"

        if commit_vsys:
            cmd += "<vsys>"
            for vsys in commit_vsys:
                cmd += "<member>" + vsys + "</member>"
            cmd += "</vsys>"

        cmd += "</partial><force></force>"

    cmd += "</commit>"

    xapi.commit(cmd=cmd, sync=sync, interval=interval, timeout=timeout)

    try:
        result = xapi.xml_root().encode('utf-8')
        root = etree.fromstring(result)
        job_id = root.find('./result/job/id').text
    except AttributeError:
        job_id = None

    panos_commit_details = dict(status_text=xapi.status,
                                status_code=xapi.status_code,
                                status_detail=xapi.status_detail,
                                job_id=job_id)

    if "Commit failed" in xapi.status_detail:
        module.fail_json(msg=xapi.status_detail,
                         panos_commit=panos_commit_details)

    if job_id:
        module.exit_json(changed=True,
                         msg="Commit successful.",
                         panos_commit=panos_commit_details)
    else:
        module.exit_json(changed=False,
                         msg="No changes to commit.",
                         panos_commit=panos_commit_details)