def update_ip_block(ucs, mo, ip_block, ip_version):

    remove_ip_block(ucs, mo.dn, ip_block, ip_version)
    if not ip_block['state'] == 'absent':
        if ip_version == 'v6':
            from ucsmsdk.mometa.ippool.IppoolIpV6Block import IppoolIpV6Block
            IppoolIpV6Block(parent_mo_or_dn=mo,
                            to=ip_block['ipv6_last_addr'],
                            r_from=ip_block['ipv6_first_addr'],
                            prefix=ip_block['ipv6_prefix'],
                            def_gw=ip_block['ipv6_default_gw'],
                            prim_dns=ip_block['ipv6_primary_dns'],
                            sec_dns=ip_block['ipv6_secondary_dns'])
            ucs.login_handle.add_mo(mo, True)
            ucs.login_handle.commit()
        else:
            from ucsmsdk.mometa.ippool.IppoolBlock import IppoolBlock
            IppoolBlock(parent_mo_or_dn=mo,
                        to=ip_block['last_addr'],
                        r_from=ip_block['first_addr'],
                        subnet=ip_block['subnet_mask'],
                        def_gw=ip_block['default_gw'],
                        prim_dns=ip_block['primary_dns'],
                        sec_dns=ip_block['secondary_dns'])
            ucs.login_handle.add_mo(mo, True)
            ucs.login_handle.commit()
Example #2
0
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(
        org_dn=dict(type='str', default='org-root'),
        name=dict(type='str', required=True),
        descr=dict(type='str', default='', aliases=['descrption', 'description']),
        order=dict(type='str', default='default', choices=['default', 'sequential']),
        first_addr=dict(type='str'),
        last_addr=dict(type='str'),
        subnet_mask=dict(type='str', default='255.255.255.0'),
        default_gw=dict(type='str', default='0.0.0.0'),
        primary_dns=dict(type='str', default='0.0.0.0'),
        secondary_dns=dict(type='str', default='0.0.0.0'),
        ipv6_first_addr=dict(type='str'),
        ipv6_last_addr=dict(type='str'),
        ipv6_prefix=dict(type='str', default='64'),
        ipv6_default_gw=dict(type='str', default='::'),
        ipv6_primary_dns=dict(type='str', default='::'),
        ipv6_secondary_dns=dict(type='str', default='::'),
        state=dict(type='str', default='present', choices=['present', 'absent']),
    )

    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
    )
    # UCSModule verifies ucsmsdk is present and exits on failure.  Imports are below ucs object creation.
    ucs = UCSModule(module)

    err = False

    from ucsmsdk.mometa.ippool.IppoolPool import IppoolPool
    from ucsmsdk.mometa.ippool.IppoolBlock import IppoolBlock
    from ucsmsdk.mometa.ippool.IppoolIpV6Block import IppoolIpV6Block

    changed = False
    try:
        mo_exists = False
        props_match = False
        # dn is <org_dn>/ip-pool-<name>
        dn = module.params['org_dn'] + '/ip-pool-' + module.params['name']

        mo = ucs.login_handle.query_dn(dn)
        if mo:
            mo_exists = True

        if module.params['state'] == 'absent':
            if mo_exists:
                if not module.check_mode:
                    ucs.login_handle.remove_mo(mo)
                    ucs.login_handle.commit()
                changed = True
        else:
            if mo_exists:
                # check top-level mo props
                kwargs = dict(assignment_order=module.params['order'])
                kwargs['descr'] = module.params['descr']
                if (mo.check_prop_match(**kwargs)):
                    # top-level props match, check next level mo/props
                    if module.params['last_addr'] and module.params['first_addr']:
                        # ipv4 block specified, check properties
                        block_dn = dn + '/block-' + module.params['first_addr'] + '-' + module.params['last_addr']
                        mo_1 = ucs.login_handle.query_dn(block_dn)
                        if mo_1:
                            kwargs = dict(subnet=module.params['subnet_mask'])
                            kwargs['def_gw'] = module.params['default_gw']
                            kwargs['prim_dns'] = module.params['primary_dns']
                            kwargs['sec_dns'] = module.params['secondary_dns']
                            if (mo_1.check_prop_match(**kwargs)):
                                # ipv4 block exists and properties match
                                props_match = True
                    else:
                        # no ipv4 block specified, but top-level props matched
                        props_match = True

                    # only check ipv6 props if the top-level and ipv4 props matched
                    if props_match and module.params['ipv6_last_addr'] and module.params['ipv6_first_addr']:
                        # ipv6 block specified, check properties
                        block_dn = dn + '/v6block-' + module.params['ipv6_first_addr'].lower() + '-' + module.params['ipv6_last_addr'].lower()
                        mo_1 = ucs.login_handle.query_dn(block_dn)
                        if mo_1:
                            kwargs = dict(prefix=module.params['ipv6_prefix'])
                            kwargs['def_gw'] = module.params['ipv6_default_gw']
                            kwargs['prim_dns'] = module.params['ipv6_primary_dns']
                            kwargs['sec_dns'] = module.params['ipv6_secondary_dns']
                            if (mo_1.check_prop_match(**kwargs)):
                                # ipv6 block exists and properties match
                                props_match = True
                        else:
                            # no ipv6 block specified, but previous checks matched
                            props_match = True

            if not props_match:
                if not module.check_mode:
                    # create if mo does not already exist
                    mo = IppoolPool(
                        parent_mo_or_dn=module.params['org_dn'],
                        name=module.params['name'],
                        descr=module.params['descr'],
                        assignment_order=module.params['order'],
                    )

                    if module.params['last_addr'] and module.params['first_addr']:
                        mo_1 = IppoolBlock(
                            parent_mo_or_dn=mo,
                            to=module.params['last_addr'],
                            r_from=module.params['first_addr'],
                            subnet=module.params['subnet_mask'],
                            def_gw=module.params['default_gw'],
                            prim_dns=module.params['primary_dns'],
                            sec_dns=module.params['secondary_dns'],
                        )

                    if module.params['ipv6_last_addr'] and module.params['ipv6_first_addr']:
                        mo_1 = IppoolIpV6Block(
                            parent_mo_or_dn=mo,
                            to=module.params['ipv6_last_addr'],
                            r_from=module.params['ipv6_first_addr'],
                            prefix=module.params['ipv6_prefix'],
                            def_gw=module.params['ipv6_default_gw'],
                            prim_dns=module.params['ipv6_primary_dns'],
                            sec_dns=module.params['ipv6_secondary_dns'],
                        )

                    ucs.login_handle.add_mo(mo, True)
                    ucs.login_handle.commit()

                changed = True

    except Exception as e:
        err = True
        ucs.result['msg'] = "setup error: %s " % str(e)

    ucs.result['changed'] = changed
    if err:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)
def main():
    from ansible.module_utils.basic import AnsibleModule
    from ansible_collections.cisco.ucs.plugins.module_utils.ucs import UCSModule, ucs_argument_spec

    ipv4_configuration_spec = dict(
        first_addr=dict(type='str'),
        last_addr=dict(type='str'),
        subnet_mask=dict(type='str', default='255.255.255.0'),
        default_gw=dict(type='str', default='0.0.0.0'),
        primary_dns=dict(type='str', default='0.0.0.0'),
        secondary_dns=dict(type='str', default='0.0.0.0'),
        state=dict(type='str',
                   default='present',
                   choices=['present', 'absent']),
    )
    ipv6_configuration_spec = dict(
        ipv6_first_addr=dict(type='str'),
        ipv6_last_addr=dict(type='str'),
        ipv6_prefix=dict(type='str', default='64'),
        ipv6_default_gw=dict(type='str', default='::'),
        ipv6_primary_dns=dict(type='str', default='::'),
        ipv6_secondary_dns=dict(type='str', default='::'),
        state=dict(type='str',
                   default='present',
                   choices=['present', 'absent']),
    )

    argument_spec = ucs_argument_spec
    argument_spec.update(
        org_dn=dict(type='str', default='org-root'),
        name=dict(type='str', required=True),
        descr=dict(type='str', default='', aliases=['description']),
        order=dict(type='str',
                   default='default',
                   choices=['default', 'sequential']),
        first_addr=dict(type='str'),
        last_addr=dict(type='str'),
        subnet_mask=dict(type='str', default='255.255.255.0'),
        default_gw=dict(type='str', default='0.0.0.0'),
        primary_dns=dict(type='str', default='0.0.0.0'),
        secondary_dns=dict(type='str', default='0.0.0.0'),
        ipv6_first_addr=dict(type='str'),
        ipv6_last_addr=dict(type='str'),
        ipv6_prefix=dict(type='str', default='64'),
        ipv6_default_gw=dict(type='str', default='::'),
        ipv6_primary_dns=dict(type='str', default='::'),
        ipv6_secondary_dns=dict(type='str', default='::'),
        state=dict(type='str',
                   default='present',
                   choices=['present', 'absent']),
        ipv4_blocks=dict(type='list',
                         default=None,
                         elements='dict',
                         options=ipv4_configuration_spec),
        ipv6_blocks=dict(type='list',
                         default=None,
                         elements='dict',
                         options=ipv6_configuration_spec),
    )

    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
    )
    # UCSModule verifies ucsmsdk is present and exits on failure.  Imports are below ucs object creation.
    ucs = UCSModule(module)

    err = False

    from ucsmsdk.mometa.ippool.IppoolBlock import IppoolBlock
    from ucsmsdk.mometa.ippool.IppoolIpV6Block import IppoolIpV6Block

    changed = False
    try:
        mo_exists = False
        ipv4_props_match = True
        ipv6_props_match = True
        # dn is <org_dn>/ip-pool-<name>
        dn = module.params['org_dn'] + '/ip-pool-' + module.params['name']

        mo = ucs.login_handle.query_dn(dn)
        if mo:
            mo_exists = True
        if module.params['state'] == 'absent':
            if mo_exists:
                if not module.check_mode:
                    ucs.login_handle.remove_mo(mo)
                    ucs.login_handle.commit()
                changed = True
        else:
            if not mo_exists:
                if not module.check_mode:
                    mo = update_ip_pool(ucs, module)
                changed = True
            if mo_exists:
                # check top-level mo props
                kwargs = dict(assignment_order=module.params['order'])
                kwargs['descr'] = module.params['descr']
                if not mo.check_prop_match(**kwargs):
                    if not module.check_mode:
                        mo = update_ip_pool(ucs, module)
                    changed = True
                    # top-level props match, check next level mo/props
            if module.params['ipv4_blocks']:
                for ipv4_block in module.params['ipv4_blocks']:
                    if not match_existing_ipv4_block(ucs, dn, ipv4_block):
                        if not module.check_mode:
                            update_ip_block(ucs, mo, ipv4_block, 'v4')
                        changed = True
            elif module.params['last_addr'] and module.params['first_addr']:
                # ipv4 block specified, check properties
                mo_1 = get_ip_block(ucs, dn, module.params['first_addr'],
                                    module.params['last_addr'], 'v4')
                if mo_1:
                    kwargs = dict(subnet=module.params['subnet_mask'])
                    kwargs['def_gw'] = module.params['default_gw']
                    kwargs['prim_dns'] = module.params['primary_dns']
                    kwargs['sec_dns'] = module.params['secondary_dns']
                    if not mo_1.check_prop_match(**kwargs):
                        # ipv4 block exists and properties match
                        ipv4_props_match = False
                else:
                    ipv4_props_match = False

            # only check ipv6 props if the top-level and ipv4 props matched
            if module.params['ipv6_blocks']:
                for ipv6_block in module.params['ipv6_blocks']:
                    if not match_existing_ipv6_block(ucs, dn, ipv6_block):
                        if not module.check_mode:
                            update_ip_block(ucs, mo, ipv6_block, 'v6')
                        changed = True
            elif module.params['ipv6_last_addr'] and module.params[
                    'ipv6_first_addr']:
                # ipv6 block specified, check properties
                block_dn = dn + '/v6block-' + module.params[
                    'ipv6_first_addr'].lower(
                    ) + '-' + module.params['ipv6_last_addr'].lower()
                mo_1 = ucs.login_handle.query_dn(block_dn)
                if mo_1:
                    kwargs = dict(prefix=module.params['ipv6_prefix'])
                    kwargs['def_gw'] = module.params['ipv6_default_gw']
                    kwargs['prim_dns'] = module.params['ipv6_primary_dns']
                    kwargs['sec_dns'] = module.params['ipv6_secondary_dns']
                    if not mo_1.check_prop_match(**kwargs):
                        # ipv6 block exists and properties match
                        ipv6_props_match = False
                else:
                    ipv6_props_match = False

        if not ipv4_props_match or not ipv6_props_match:
            if not module.check_mode:
                if module.params['last_addr'] and module.params['first_addr']:
                    IppoolBlock(
                        parent_mo_or_dn=mo,
                        to=module.params['last_addr'],
                        r_from=module.params['first_addr'],
                        subnet=module.params['subnet_mask'],
                        def_gw=module.params['default_gw'],
                        prim_dns=module.params['primary_dns'],
                        sec_dns=module.params['secondary_dns'],
                    )

                if module.params['ipv6_last_addr'] and module.params[
                        'ipv6_first_addr']:
                    IppoolIpV6Block(
                        parent_mo_or_dn=mo,
                        to=module.params['ipv6_last_addr'],
                        r_from=module.params['ipv6_first_addr'],
                        prefix=module.params['ipv6_prefix'],
                        def_gw=module.params['ipv6_default_gw'],
                        prim_dns=module.params['ipv6_primary_dns'],
                        sec_dns=module.params['ipv6_secondary_dns'],
                    )

            ucs.login_handle.add_mo(mo, True)
            ucs.login_handle.commit()

            changed = True

    except Exception as e:
        err = True
        ucs.result['msg'] = "setup error: %s " % str(e)

    ucs.result['changed'] = changed
    if err:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)
Example #4
0
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(ip_list=dict(required=True, type='list'),
                         org_dn=dict(type='str', default='org-root'),
                         state=dict(default='present',
                                    choices=['present', 'absent'],
                                    type='str'))
    module = AnsibleModule(argument_spec, supports_check_mode=True)
    ucs = UCSModule(module)

    err = False

    from ucsmsdk.mometa.ippool.IppoolPool import IppoolPool
    from ucsmsdk.mometa.ippool.IppoolBlock import IppoolBlock
    from ucsmsdk.mometa.ippool.IppoolIpV6Block import IppoolIpV6Block

    changed = False
    try:
        for ip in module.params['ip_list']:
            exists = False
            dn = module.params['org_dn'] + '/ip-pool-' + ip['name']
            # set default params
            if 'order' not in ip:
                ip['order'] = 'default'
            if 'descr' not in ip:
                ip['descr'] = ''
            if 'subnet_mask' not in ip:
                ip['subnet_mask'] = '255.255.255.0'
            if 'default_gw' not in ip:
                ip['default_gw'] = '0.0.0.0'
            if 'primary_dns' not in ip:
                ip['primary_dns'] = '0.0.0.0'
            if 'secondary_dns' not in ip:
                ip['secondary_dns'] = '0.0.0.0'
            if 'ipv6_prefix' not in ip:
                ip['ipv6_prefix'] = '64'
            if 'ipv6_default_gw' not in ip:
                ip['ipv6_default_gw'] = '::'
            if 'ipv6_primary_dns' not in ip:
                ip['ipv6_primary_dns'] = '::'
            if 'ipv6_secondary_dns' not in ip:
                ip['ipv6_secondary_dns'] = '::'

            mo = ucs.login_handle.query_dn(dn)
            if mo:
                # check top-level mo props
                kwargs = {}
                kwargs['assignment_order'] = ip['order']
                kwargs['descr'] = ip['descr']
                if (mo.check_prop_match(**kwargs)):
                    # top-level props match, check next level mo/props
                    exists = True
                    if 'last_addr' in ip and 'first_addr' in ip:
                        block_dn = dn + '/block-' + ip[
                            'first_addr'] + '-' + ip['last_addr']
                        mo_1 = ucs.login_handle.query_dn(block_dn)
                        if mo_1:
                            kwargs = {}
                            kwargs['subnet'] = ip['subnet_mask']
                            kwargs['def_gw'] = ip['default_gw']
                            kwargs['prim_dns'] = ip['primary_dns']
                            kwargs['sec_dns'] = ip['secondary_dns']
                            if (not mo_1.check_prop_match(**kwargs)):
                                exists = False
                        else:
                            exists = False

                    if 'ipv6_last_addr' in ip and 'ipv6_first_addr' in ip:
                        block_dn = dn + '/v6block-' + ip[
                            'ipv6_first_addr'].lower(
                            ) + '-' + ip['ipv6_last_addr'].lower()
                        mo_1 = ucs.login_handle.query_dn(block_dn)
                        if mo_1:
                            kwargs = {}
                            kwargs['prefix'] = ip['ipv6_prefix']
                            kwargs['def_gw'] = ip['ipv6_default_gw']
                            kwargs['prim_dns'] = ip['ipv6_primary_dns']
                            kwargs['sec_dns'] = ip['ipv6_secondary_dns']
                            if (not mo_1.check_prop_match(**kwargs)):
                                exists = False
                        else:
                            exists = False

            if module.params['state'] == 'absent':
                if exists:
                    if not module.check_mode:
                        ucs.login_handle.remove_mo(mo)
                        ucs.login_handle.commit()
                    changed = True
            else:
                if not exists:
                    if not module.check_mode:
                        # create if mo does not already exist
                        mo = IppoolPool(
                            parent_mo_or_dn=module.params['org_dn'],
                            name=ip['name'],
                            descr=ip['descr'],
                            assignment_order=ip['order'])

                        if 'last_addr' in ip and 'first_addr' in ip:
                            mo_1 = IppoolBlock(parent_mo_or_dn=mo,
                                               to=ip['last_addr'],
                                               r_from=ip['first_addr'],
                                               subnet=ip['subnet_mask'],
                                               def_gw=ip['default_gw'],
                                               prim_dns=ip['primary_dns'],
                                               sec_dns=ip['secondary_dns'])

                        if 'ipv6_last_addr' in ip and 'ipv6_first_addr' in ip:
                            mo_1 = IppoolIpV6Block(
                                parent_mo_or_dn=mo,
                                to=ip['ipv6_last_addr'],
                                r_from=ip['ipv6_first_addr'],
                                prefix=ip['ipv6_prefix'],
                                def_gw=ip['ipv6_default_gw'],
                                prim_dns=ip['ipv6_primary_dns'],
                                sec_dns=ip['ipv6_secondary_dns'])
                        ucs.login_handle.add_mo(mo, True)
                        ucs.login_handle.commit()
                    changed = True

    except Exception as e:
        err = True
        ucs.result['msg'] = "setup error: %s " % str(e)

    ucs.result['changed'] = changed
    if err:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)