コード例 #1
0
def main():
    helper = get_connection(
        template=True,
        template_stack=True,
        with_classic_provider_spec=True,
        argument_spec=dict(name=dict(), ),
    )
    module = AnsibleModule(
        argument_spec=helper.argument_spec,
        supports_check_mode=False,
        required_one_of=helper.required_one_of,
    )

    # Verify imports, build pandevice object tree.
    parent = helper.get_pandevice_parent(module)

    name = module.params['name']
    if name is None:
        try:
            listing = VirtualRouter.refreshall(parent)
        except PanDeviceError as e:
            module.fail_json(msg='Failed refreshall: {0}'.format(e))

        vrlist = helper.to_module_dict(listing)
        module.exit_json(changed=False, vrlist=vrlist)

    vr = VirtualRouter(name)
    parent.add(vr)
    try:
        vr.refresh()
    except PanDeviceError as e:
        module.fail_json(msg='Failed refresh: {0}'.format(e))

    spec = helper.to_module_dict(vr)
    module.exit_json(changed=False, spec=spec)
コード例 #2
0
    def populate_facts(self):
        listing = VirtualRouter.refreshall(self.parent, add=False)

        virtual_routers = []
        for vr in listing:
            info = {
                "vr_name": vr.name,
                "vr_iflist": vr.interface or [],
                "vr_asn": None,
                "vr_routerid": None,
            }
            for child in vr.children:
                if isinstance(child, Bgp):
                    info["vr_asn"] = child.local_as
                    info["vr_routerid"] = child.router_id
            virtual_routers.append(info)

        self.facts.update({"virtual_routers": virtual_routers})
コード例 #3
0
def main():
    helper = get_connection(
        vsys_importable=True,
        template=True,
        template_stack=True,
        with_state=True,
        with_classic_provider_spec=True,
        argument_spec=setup_args(),
    )

    module = AnsibleModule(
        argument_spec=helper.argument_spec,
        supports_check_mode=True,
        required_one_of=helper.required_one_of,
    )

    # Verify imports, build pandevice object tree.
    parent = helper.get_pandevice_parent(module)

    # Exclude non-object items from kwargs passed to the object.
    exclude_list = [
        'ip_address', 'username', 'password', 'api_key', 'state', 'commit',
        'provider', 'template', 'template_stack', 'vsys', 'port',
    ]

    # Generate the kwargs for network.VirtualRouter.
    obj_spec = dict((k, module.params[k]) for k in helper.argument_spec.keys() if k not in exclude_list)

    name = module.params['name']
    state = module.params['state']
    commit = module.params['commit']

    # Retrieve current virtual routers.
    try:
        vr_list = VirtualRouter.refreshall(parent, add=False)
    except PanDeviceError as e:
        module.fail_json(msg='Failed refresh: {0}'.format(e))

    # Create the new state object.
    virtual_router = VirtualRouter(**obj_spec)
    parent.add(virtual_router)

    reference_params = {
        'refresh': True,
        'update': not module.check_mode,
        'return_type': 'bool',
    }
    changed = False
    if state == 'present':
        for item in vr_list:
            if item.name != name:
                continue
            if not item.equal(virtual_router, compare_children=False):
                changed = True
                virtual_router.extend(item.children)
                if not module.check_mode:
                    try:
                        virtual_router.apply()
                    except PanDeviceError as e:
                        module.fail_json(msg='Failed apply: {0}'.format(e))
            break
        else:
            changed = True
            if not module.check_mode:
                try:
                    virtual_router.create()
                except PanDeviceError as e:
                    module.fail_json(msg='Failed apply: {0}'.format(e))

        changed |= virtual_router.set_vsys(
            module.params['vsys'], **reference_params)
    else:
        changed |= virtual_router.set_vsys(
            None, **reference_params)
        if name in [x.name for x in vr_list]:
            changed = True
            if not module.check_mode:
                try:
                    virtual_router.delete()
                except PanDeviceError as e:
                    module.fail_json(msg='Failed delete: {0}'.format(e))

    if commit and changed:
        helper.commit(module)

    if not changed:
        msg = 'no changes required.'
    elif module.check_mode:
        msg = 'Changes are required.'
    else:
        msg = 'Virtual router update successful.'

    module.exit_json(msg=msg, changed=changed)
コード例 #4
0
def main():
    helper = get_connection(
        template=True,
        template_stack=True,
        with_state=True,
        with_classic_provider_spec=True,
        argument_spec=dict(
            name=dict(required=True),
            destination=dict(),
            nexthop_type=dict(
                default='ip-address',
                choices=['ip-address', 'discard', 'none', 'next-vr'],
            ),
            nexthop=dict(),
            admin_dist=dict(),
            metric=dict(type='int', default=10),
            virtual_router=dict(default='default'),
            interface=dict(),
        ),
    )

    module = AnsibleModule(
        argument_spec=helper.argument_spec,
        supports_check_mode=True,
        required_one_of=helper.required_one_of,
    )

    spec = {
        'name': module.params['name'],
        'destination': module.params['destination'],
        'nexthop_type': module.params['nexthop_type'],
        'nexthop': module.params['nexthop'],
        'interface': module.params['interface'],
        'admin_dist': module.params['admin_dist'],
        'metric': module.params['metric'],
    }

    parent = helper.get_pandevice_parent(module)
    virtual_router = module.params['virtual_router']

    # Allow None for nexthop_type.
    if spec['nexthop_type'] == 'none':
        spec['nexthop_type'] = None

    try:
        vr_list = VirtualRouter.refreshall(parent, add=False, name_only=True)
    except PanDeviceError as e:
        module.fail_json(msg='Failed vr refresh: {0}'.format(e))

    # Find the virtual router.
    for vr in vr_list:
        if vr.name == virtual_router:
            parent.add(vr)
            break
    else:
        module.fail_json(msg='Virtual router "{0}" does not exist'.format(virtual_router))

    # Get the listing.
    try:
        listing = StaticRoute.refreshall(vr, add=False)
    except PanDeviceError as e:
        module.fail_json(msg='Failed refresh: {0}'.format(e))

    # Create the object and attach it to the object tree.
    obj = StaticRoute(**spec)
    vr.add(obj)

    # Apply the state.
    changed, diff = helper.apply_state(obj, listing, module)

    module.exit_json(changed=changed, diff=diff)