Exemple #1
0
    def create_kube_macs(handle, org):
        from ucsmsdk.mometa.macpool.MacpoolPool import MacpoolPool
        from ucsmsdk.mometa.macpool.MacpoolBlock import MacpoolBlock

        mo = MacpoolPool(parent_mo_or_dn=org,
                         policy_owner="local",
                         descr="KUBAM MAC Pool A",
                         assignment_order="default",
                         name="kubamA")
        MacpoolBlock(parent_mo_or_dn=mo,
                     to="00:25:B5:88:8A:FF",
                     r_from="00:25:B5:88:8A:00")
        handle.add_mo(mo)

        mo = MacpoolPool(parent_mo_or_dn=org,
                         policy_owner="local",
                         descr="KUBAM MAC Pool B",
                         assignment_order="default",
                         name="kubamB")
        MacpoolBlock(parent_mo_or_dn=mo,
                     to="00:25:B5:88:8B:FF",
                     r_from="00:25:B5:88:8B:00")
        handle.add_mo(mo)
        try:
            handle.commit()
        except UcsException as err:
            if err.error_code == "103":
                print "\tKUBAM MAC Pools already exist"
            else:
                return 1, err.error_descr
        return 0, None
Exemple #2
0
def createKubeMacs(handle, org):
    print "Creating Kubernetes MAC Pools"
    from ucsmsdk.mometa.macpool.MacpoolPool import MacpoolPool
    from ucsmsdk.mometa.macpool.MacpoolBlock import MacpoolBlock
    print "org: %s" % org
    mo = MacpoolPool(parent_mo_or_dn=org, policy_owner="local", descr="Kubernetes MAC Pool A", assignment_order="default", name="kubeA")
    mo_1 = MacpoolBlock(parent_mo_or_dn=mo, to="00:25:B5:88:8A:FF", r_from="00:25:B5:88:8A:00")
    handle.add_mo(mo)

    mo = MacpoolPool(parent_mo_or_dn=org, policy_owner="local", descr="Kubernetes MAC Pool B", assignment_order="default", name="kubeB")
    mo_1 = MacpoolBlock(parent_mo_or_dn=mo, to="00:25:B5:88:8B:FF", r_from="00:25:B5:88:8B:00")
    handle.add_mo(mo)
    try: 
        handle.commit()
    except UcsException as err:
        if err.error_code == "103":
            print "\tKubernetes MAC Pools already exist"
def setup_macpool(server, module):
    from ucsmsdk.mometa.macpool.MacpoolPool import MacpoolPool
    from ucsmsdk.mometa.macpool.MacpoolBlock import MacpoolBlock

    ansible = module.params
    args_mo = _get_mo_params(ansible)

    changed = False

    for mac in args_mo['mac_list']:
        exists = False
        dn = args_mo['org_dn'] + '/mac-pool-' + mac['name']
        mo = server.query_dn(dn)
        if mo:
            # check top-level mo props
            kwargs = {}
            kwargs['assignment_order'] = mac['order']
            if (mo.check_prop_match(**kwargs)):
                # top-level props match, check next level props
                if (mac['to'] <> '' and mac['from'] <> ''):
                    block_dn = dn + '/block-' + mac['from'] + '-' + mac['to']
                    mo_1 = server.query_dn(block_dn)
                    if mo_1:
                        exists = True
                else:
                    exists = True

        if ansible['state'] == 'absent':
            if exists:
                changed = True
                if not module.check_mode:
                    server.remove_mo(mo)
                    server.commit()
        else:
            if not exists:
                changed = True
                if not module.check_mode:
                    # create if mo does not already exist
                    if not "description" in mac:
                        mac["description"] = ""
                    mo = MacpoolPool(parent_mo_or_dn=args_mo['org_dn'],
                                     name=mac['name'],
                                     descr=mac['description'],
                                     assignment_order=mac['order'])

                    if (mac['to'] <> '' and mac['from'] <> ''):
                        mo_1 = MacpoolBlock(parent_mo_or_dn=mo,
                                            to=mac['to'],
                                            r_from=mac['from'])
                    server.add_mo(mo, True)
                    server.commit()

    return changed
Exemple #4
0
def mac_pool_create(handle,
                    name,
                    assignment_order,
                    r_from,
                    to,
                    descr="",
                    parent_dn="org-root"):
    """
    Creates MAC Pool

    Args:
        handle (UcsHandle)
        name (String) : Network Control Policy Name
        assignment_order (String) : ["default", "sequential"]
        r_from (String) : Beginning MAC Address
        to (String) : Ending MAC Address
        descr (String) :
        parent_dn (String) :

    Returns:
        MacpoolPool: Managed Object

    Raises:
        ValueError: If OrgOrg is not present

    Example:
        mac_pool_create(handle, "sample_mac_pool", "default",
                    "00:25:B5:00:00:00", "00:25:B5:00:00:03")
    """
    from ucsmsdk.mometa.macpool.MacpoolPool import MacpoolPool
    from ucsmsdk.mometa.macpool.MacpoolBlock import MacpoolBlock

    obj = handle.query_dn(parent_dn)
    if obj:
        mo = MacpoolPool(parent_mo_or_dn=obj,
                         policy_owner="local",
                         descr=descr,
                         assignment_order=assignment_order,
                         name=name)
        MacpoolBlock(parent_mo_or_dn=mo, to=to, r_from=r_from)

        handle.add_mo(mo, modify_present=True)
        handle.commit()
        return mo
    else:
        raise ValueError("org '%s' is not available" % parent_dn)
Exemple #5
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=['description', 'descrption']),
        order=dict(type='str',
                   default='default',
                   choices=['default', 'sequential']),
        first_addr=dict(type='str'),
        last_addr=dict(type='str'),
        state=dict(default='present',
                   choices=['present', 'absent'],
                   type='str'),
    )
    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.macpool.MacpoolPool import MacpoolPool
    from ucsmsdk.mometa.macpool.MacpoolBlock import MacpoolBlock

    changed = False
    try:
        mo_exists = False
        props_match = False
        # dn is <org_dn>/mac-pool-<name>
        dn = module.params['org_dn'] + '/mac-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']:
                        # mac address block specified, check properties
                        block_dn = dn + '/block-' + module.params[
                            'first_addr'].upper(
                            ) + '-' + module.params['last_addr'].upper()
                        mo_1 = ucs.login_handle.query_dn(block_dn)
                        if mo_1:
                            props_match = True
                    else:
                        # no MAC address block specified, but top-level props matched
                        props_match = True

            if not props_match:
                if not module.check_mode:
                    # create if mo does not already exist
                    mo = MacpoolPool(
                        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 = MacpoolBlock(
                            parent_mo_or_dn=mo,
                            to=module.params['last_addr'],
                            r_from=module.params['first_addr'],
                        )

                    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)
Exemple #6
0
handle.add_mo(mo)
handle.commit()

#Create Power Policy
mo = PowerPolicy(parent_mo_or_dn=my_Full_Path_Org, fan_speed="any", policy_owner="local", name="No_Cap", prio="no-cap", descr="No Cap")
handle.add_mo(mo)
handle.commit()

#Create IP Pool
mo = IppoolPool(parent_mo_or_dn=my_Full_Path_Org, is_net_bios_enabled="disabled", name="ext_mgmt", descr="KVM", policy_owner="local", ext_managed="internal", supports_dhcp="disabled", assignment_order="sequential")
mo_1 = IppoolBlock(parent_mo_or_dn=mo, prim_dns=my_Primary_DNS, r_from=my_kvm_pool_first, def_gw=my_KVM_Gateway, sec_dns=my_Secondary_DNS, to=my_kvm_last_addr)
handle.add_mo(mo)
handle.commit()

#Create MAC Pools
mo = MacpoolPool(parent_mo_or_dn=my_Full_Path_Org, policy_owner="local", descr="Management FI-A", assignment_order="sequential", name="MGMT-A")
mo_1 = MacpoolBlock(parent_mo_or_dn=mo, to="00:25:B5:A0:00:0F", r_from="00:25:B5:A0:00:00")
handle.add_mo(mo)
handle.commit()

mo = MacpoolPool(parent_mo_or_dn=my_Full_Path_Org, policy_owner="local", descr="Management FI-B", assignment_order="sequential", name="MGMT-B")
mo_1 = MacpoolBlock(parent_mo_or_dn=mo, to="00:25:B5:B0:00:0F", r_from="00:25:B5:B0:00:00")
handle.add_mo(mo)
handle.commit()

mo = MacpoolPool(parent_mo_or_dn=my_Full_Path_Org, policy_owner="local", descr="Production FI-A", assignment_order="sequential", name="VM-A")
mo_1 = MacpoolBlock(parent_mo_or_dn=mo, to="00:25:B5:A1:00:0F", r_from="00:25:B5:A1:00:00")
handle.add_mo(mo)
handle.commit()

mo = MacpoolPool(parent_mo_or_dn=my_Full_Path_Org, policy_owner="local", descr="Production FI-B", assignment_order="sequential", name="VM-B")
Exemple #7
0
                   sec_dns="10.10.10.200",
                   to="10.10.10.17")
handle.add_mo(mo)

handle.commit()
##### End-Of-PythonScript #####

#Create MAC Pool
##### Start-Of-PythonScript #####

from ucsmsdk.mometa.macpool.MacpoolPool import MacpoolPool
from ucsmsdk.mometa.macpool.MacpoolBlock import MacpoolBlock

mo = MacpoolPool(parent_mo_or_dn="org-root/org-Sub_Org_Test",
                 policy_owner="local",
                 descr="Mamagement FI-A",
                 assignment_order="sequential",
                 name="MGMT-A")
mo_1 = MacpoolBlock(parent_mo_or_dn=mo,
                    to="00:25:B5:A0:00:0F",
                    r_from="00:25:B5:A0:00:00")
handle.add_mo(mo)
handle.commit()

mo = MacpoolPool(parent_mo_or_dn="org-root/org-Sub_Org_Test",
                 policy_owner="local",
                 descr="Mamagement FI-B",
                 assignment_order="sequential",
                 name="MGMT-B")
mo_1 = MacpoolBlock(parent_mo_or_dn=mo,
                    to="00:25:B5:B0:00:0F",
Exemple #8
0
        for row in csv_reader:
            row_num += 1

            # set org (org-root as default)
            if row['org']:
                org = row['org']
            else:
                org = 'org-root'
            if row['MAC_pool'] and row['MAC_From'] and row['MAC_To']:
                # create MAC pool
                from ucsmsdk.mometa.macpool.MacpoolPool import MacpoolPool
                from ucsmsdk.mometa.macpool.MacpoolBlock import MacpoolBlock

                mo = MacpoolPool(parent_mo_or_dn=org,
                                 policy_owner="local",
                                 descr="",
                                 assignment_order="default",
                                 name=row['MAC_pool'])
                mo_1 = MacpoolBlock(parent_mo_or_dn=mo,
                                    to=row['MAC_To'],
                                    r_from=row['MAC_From'])
                handle.add_mo(mo)

                handle.commit()
            if row['VLAN'] and row['VLAN_ID']:
                # create vLANs
                from ucsmsdk.mometa.fabric.FabricVlan import FabricVlan

                # create a range if needed based on all VLANs given in VLAN_ID
                vlanList = row['VLAN_ID'].split('-')
                vlansStart = int(vlanList[0])
Exemple #9
0
def mac_pool(input):
    name = input['name']
    descr = input['descr']
    to = input['to']
    r_from = input['r_from']
    state = input['state']
    ip = input['ip']
    username = input['username']
    password = input['password']
    mo = ""
    mo_block = ""
    results = {}
    ucs_handle = pickle.loads(str(ucs_login.main(ip, username, password)))
    ###-------CHECK IF MO EXISTS---------------------------------

    try:
        mo = ucs_handle.query_dn("org-root/mac-pool-" + name)
        mo_block = ucs_handle.query_dn("org-root/mac-pool-" + name +
                                       "/block-" + r_from + "-" + to)
    except:
        print("Could not query children of macpool")

###----if expected state is "present"------------------------

    if state == "present":
        if mo:
            if (mo.descr == descr):
                if (to <> "" and r_from <> ""):
                    if (mo_block):
                        results['name'] = name
                        results['expected'] = True
                        results['changed'] = False
                        results['present'] = True
                    else:
                        #modified_mo =  MacpoolPool(parent_mo_or_dn="org-root", name=name, descr=descr)
                        mo_1 = MacpoolBlock(parent_mo_or_dn=mo,
                                            to=to,
                                            r_from=r_from)
                        ucs_handle.add_mo(mo, True)
                        ucs_handle.commit()
                        results['name'] = name
                        results['present'] = True
                        results['removed'] = False
                        results['changed'] = True
                else:
                    results['name'] = name
                    results['expected'] = True
                    results['changed'] = False
                    results['present'] = True

            else:
                try:
                    modified_mo = MacpoolPool(parent_mo_or_dn="org-root",
                                              name=name,
                                              descr=descr)
                    if (to <> "" and r_from <> ""):
                        mo_1 = MacpoolBlock(parent_mo_or_dn=modified_mo,
                                            to=to,
                                            r_from=r_from)
                    ucs_handle.add_mo(modified_mo, True)
                    ucs_handle.commit()
                    results['name'] = name
                    results['present'] = True
                    results['removed'] = False
                    results['changed'] = True

                except Exception, e:
                    print(e)

###----------if not, create boot policy with desired config ----------------

        else:
            try:
                mo = MacpoolPool(parent_mo_or_dn="org-root",
                                 name=name,
                                 descr=descr)
                if (to <> "" and r_from <> ""):
                    mo_1 = MacpoolBlock(parent_mo_or_dn=mo,
                                        to=to,
                                        r_from=r_from)
                ucs_handle.add_mo(mo)
                ucs_handle.commit()
                results['name'] = name
                results['present'] = False
                results['created'] = True
                results['changed'] = True

            except:
                print("Mac-pool creation failed")
Exemple #10
0
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(mac_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.macpool.MacpoolPool import MacpoolPool
    from ucsmsdk.mometa.macpool.MacpoolBlock import MacpoolBlock

    changed = False
    try:
        for mac in module.params['mac_list']:
            exists = False
            dn = module.params['org_dn'] + '/mac-pool-' + mac['name']
            mo = ucs.login_handle.query_dn(dn)
            if mo:
                # check top-level mo props
                kwargs = {}
                if 'order' in mac:
                    kwargs['assignment_order'] = mac['order']
                if 'descr' in mac:
                    kwargs['descr'] = mac['descr']
                if (mo.check_prop_match(**kwargs)):
                    # top-level props match, check next level mo/props
                    if 'last_addr' in mac and 'first_addr' in mac:
                        block_dn = dn + '/block-' + mac['first_addr'].upper(
                        ) + '-' + mac['last_addr'].upper()
                        mo_1 = ucs.login_handle.query_dn(block_dn)
                        if mo_1:
                            exists = True
                    else:
                        exists = True

            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
                        if 'order' not in mac:
                            mac['order'] = 'default'
                        if 'descr' not in mac:
                            mac['descr'] = ''
                        mo = MacpoolPool(
                            parent_mo_or_dn=module.params['org_dn'],
                            name=mac['name'],
                            descr=mac['descr'],
                            assignment_order=mac['order'])

                        if 'last_addr' in mac and 'first_addr' in mac:
                            mo_1 = MacpoolBlock(parent_mo_or_dn=mo,
                                                to=mac['last_addr'],
                                                r_from=mac['first_addr'])
                        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)