def configure_lan_connectivity(ucs, module, dn): from ucsmsdk.mometa.vnic.VnicLanConnPolicy import VnicLanConnPolicy from ucsmsdk.mometa.vnic.VnicEther import VnicEther from ucsmsdk.mometa.vnic.VnicIScsiLCP import VnicIScsiLCP from ucsmsdk.mometa.vnic.VnicVlan import VnicVlan if not module.check_mode: try: # create if mo does not already exist mo = VnicLanConnPolicy( parent_mo_or_dn=module.params['org_dn'], name=module.params['name'], descr=module.params['description'], ) if module.params.get('vnic_list'): for vnic in module.params['vnic_list']: if vnic['state'] == 'absent': child_dn = dn + '/ether-' + vnic['name'] mo_1 = ucs.login_handle.query_dn(child_dn) if mo_1: ucs.login_handle.remove_mo(mo_1) else: # state == 'present' mo_1 = VnicEther( addr='derived', parent_mo_or_dn=mo, name=vnic['name'], adaptor_profile_name=vnic['adapter_policy'], nw_templ_name=vnic['vnic_template'], order=vnic['order'], ) if module.params.get('iscsi_vnic_list'): for iscsi_vnic in module.params['iscsi_vnic_list']: if iscsi_vnic['state'] == 'absent': child_dn = dn + '/iscsi-' + iscsi_vnic['name'] mo_1 = ucs.login_handle.query_dn(child_dn) if mo_1: ucs.login_handle.remove_mo(mo_1) else: # state == 'present' mo_1 = VnicIScsiLCP( parent_mo_or_dn=mo, name=iscsi_vnic['name'], adaptor_profile_name=iscsi_vnic[ 'iscsi_adapter_policy'], vnic_name=iscsi_vnic['overlay_vnic'], addr=iscsi_vnic['mac_address'], ) VnicVlan( parent_mo_or_dn=mo_1, vlan_name=iscsi_vnic['vlan_name'], ) ucs.login_handle.add_mo(mo, True) ucs.login_handle.commit() except Exception as e: # generic Exception handling because SDK can throw a variety of exceptions ucs.result['msg'] = "setup error: %s " % str(e) module.fail_json(**ucs.result) ucs.result['changed'] = True
def lan_conn_policy_create(handle, name, descr="", parent_dn="org-root"): """ Creates LAN Connectivity Policy Args: handle (UcsHandle) name (String) : LAN Connectivity Policy name descr (String) : parent_dn (String) : Returns: None Example: lan_conn_policy_create(handle, "samp_conn_pol2") add_vnic(handle, "org-root/lan-conn-pol-samp_conn_pol2", "test_vnic", "vinbs_nw", "ANY", "any", "default", "wqdwq", "A", "", "1500") """ from ucsmsdk.mometa.vnic.VnicLanConnPolicy import VnicLanConnPolicy obj = handle.query_dn(parent_dn) if obj: mo = VnicLanConnPolicy(parent_mo_or_dn=obj, policy_owner="local", name=name, descr=descr) handle.add_mo(mo, modify_present=True) handle.commit() else: log.info(parent_dn + " MO is not available")
def createLanConnPolicy(handle, org): print "Creating Kubernetes LAN connectivity policy" from ucsmsdk.mometa.vnic.VnicLanConnPolicy import VnicLanConnPolicy from ucsmsdk.mometa.vnic.VnicEther import VnicEther mo = VnicLanConnPolicy(parent_mo_or_dn=org, policy_owner="local", name="kube", descr="Kubernetes LAN Connectivity Policy") mo_1 = VnicEther(parent_mo_or_dn=mo, addr="derived", nw_ctrl_policy_name="", admin_vcon="any", stats_policy_name="default", switch_id="A", pin_to_group_name="", mtu="1500", qos_policy_name="", adaptor_profile_name="Linux", ident_pool_name="", order="1", nw_templ_name="kubeA", name="eth0") mo_2 = VnicEther(parent_mo_or_dn=mo, addr="derived", nw_ctrl_policy_name="", admin_vcon="any", stats_policy_name="default", switch_id="A", pin_to_group_name="", mtu="1500", qos_policy_name="", adaptor_profile_name="Linux", ident_pool_name="", order="2", nw_templ_name="kubeB", name="eth1") try: handle.add_mo(mo) handle.commit() except UcsException as err: if err.error_code == "103": print "\tLAN connectivity policy 'kube' already exists"
def setup_lan_connectivity(server, module): from ucsmsdk.mometa.vnic.VnicLanConnPolicy import VnicLanConnPolicy from ucsmsdk.mometa.vnic.VnicEther import VnicEther ansible = module.params args_mo = _get_mo_params(ansible) changed = False for lan_conn in args_mo['lan_conn_list']: exists = False mo = server.query_dn(args_mo['org_dn']+'/lan-conn-pol-'+lan_conn['name']) if mo: 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 mo = VnicLanConnPolicy(parent_mo_or_dn=args_mo['org_dn'], name=lan_conn['name']) for vnic in lan_conn['vnic_list']: mo_2 = VnicEther(parent_mo_or_dn=mo, addr="derived", adaptor_profile_name=vnic['adapter_policy'], order=vnic['order'], name=vnic['name'], nw_templ_name=vnic['vnic_template']) server.add_mo(mo, True) server.commit() return changed
def lan_conn_policy_create(handle, name, descr="", parent_dn="org-root"): """ Creates LAN Connectivity Policy Args: handle (UcsHandle) name (String) : LAN Connectivity Policy name descr (String) : description parent_dn (String) : org dn Returns: VnicLanConnPolicy: Managed Object Raises: ValueError: If OrgOrg object is not present Example: lan_conn_policy_create(handle, "samp_conn_pol2") add_vnic(handle, "org-root/lan-conn-pol-samp_conn_pol2", "test_vnic", "vinbs_nw", "ANY", "any", "default", "wqdwq", "A", "", "1500") """ from ucsmsdk.mometa.vnic.VnicLanConnPolicy import VnicLanConnPolicy obj = handle.query_dn(parent_dn) if obj is None: raise ValueError("Org %s not found" % parent_dn) mo = VnicLanConnPolicy(parent_mo_or_dn=obj, policy_owner="local", name=name, descr=descr) handle.add_mo(mo, modify_present=True) handle.commit() return mo
def test_001_policy(): from ucsmsdk.mometa.flowctrl.FlowctrlItem import FlowctrlItem # ##################################### # Create a Flow Control Policy # ##################################### mo = FlowctrlItem(parent_mo_or_dn="fabric/lan/flowctrl", snd="off", rcv="off", name="test", prio="on") handle.add_mo(mo) handle.commit() # ##################################### # Delete Flow Control Policy # ##################################### obj = handle.query_dn("fabric/lan/flowctrl/policy-test") handle.remove_mo(obj) handle.commit() # ##################################### # Create Dynamic vNIC Connection Policy # ##################################### from ucsmsdk.mometa.vnic.VnicDynamicConPolicy import VnicDynamicConPolicy mo = VnicDynamicConPolicy(parent_mo_or_dn="org-root", name="test", descr="test", adaptor_profile_name="Linux", policy_owner="local", protection="protected-pref-b", dynamic_eth="54") handle.add_mo(mo) handle.commit() # ##################################### # Delete Dynamic vNIC Connection Policy # ##################################### obj = handle.query_dn("org-root/dynamic-con-test") handle.remove_mo(obj) handle.commit() # ##################################### # Create LACP Policy # ##################################### from ucsmsdk.mometa.fabric.FabricLacpPolicy import FabricLacpPolicy mo = FabricLacpPolicy(parent_mo_or_dn="org-root", fast_timer="fast", policy_owner="local", suspend_individual="true", name="test", descr="") handle.add_mo(mo) handle.commit() # ##################################### # Modify LACP Policy # ##################################### obj = handle.query_dn("org-root/lacp-test") obj.fast_timer = "normal" obj.suspend_individual = "false" obj.policy_owner = "local" obj.descr = "" handle.set_mo(obj) handle.commit() # ##################################### # Delete LACP Policy # ##################################### obj = handle.query_dn("org-root/lacp-test") handle.remove_mo(obj) handle.commit() # ##################################### # Create LAN Connectivity Policy # ##################################### from ucsmsdk.mometa.vnic.VnicLanConnPolicy import VnicLanConnPolicy from ucsmsdk.mometa.vnic.VnicEther import VnicEther from ucsmsdk.mometa.vnic.VnicEtherIf import VnicEtherIf mo = VnicLanConnPolicy(parent_mo_or_dn="org-root", policy_owner="local", name="test", descr="test_policy") mo_1 = VnicEther(parent_mo_or_dn=mo, nw_ctrl_policy_name="default", name="test", admin_host_port="ANY", admin_vcon="any", stats_policy_name="default", admin_cdn_name="", switch_id="A", pin_to_group_name="", mtu="1500", qos_policy_name="qos-1", adaptor_profile_name="Linux", ident_pool_name="mac-pool-1", order="1", nw_templ_name="", addr="derived") mo_1_1 = VnicEtherIf(parent_mo_or_dn=mo_1, default_net="yes", name="default") handle.add_mo(mo) handle.commit() # ##################################### # Delete LAN Connectivity Policy # ##################################### obj = handle.query_dn("org-root/lan-conn-pol-test") handle.remove_mo(obj) handle.commit() # ##################################### # Create QoS Policy # ##################################### from ucsmsdk.mometa.epqos.EpqosDefinition import EpqosDefinition from ucsmsdk.mometa.epqos.EpqosEgress import EpqosEgress mo = EpqosDefinition(parent_mo_or_dn="org-root", policy_owner="local", name="test", descr="") mo_1 = EpqosEgress(parent_mo_or_dn=mo, rate="line-rate", host_control="none", name="", prio="best-effort", burst="10240") handle.add_mo(mo) handle.commit() # ##################################### # Delete QoS Policy # ##################################### obj = handle.query_dn("org-root/ep-qos-test") handle.remove_mo(obj) handle.commit()
def main(): argument_spec = ucs_argument_spec argument_spec.update( org_dn=dict(type='str', default='org-root'), name=dict(type='str', required=True), description=dict(type='str', aliases=['descr'], default=''), vnic_list=dict(type='list'), state=dict(type='str', default='present', choices=['present', 'absent']), ) module = AnsibleModule( argument_spec, supports_check_mode=True, ) ucs = UCSModule(module) err = False # UCSModule creation above verifies ucsmsdk is present and exits on failure. Additional imports are done below. from ucsmsdk.mometa.vnic.VnicLanConnPolicy import VnicLanConnPolicy from ucsmsdk.mometa.vnic.VnicEther import VnicEther from ucsmsdk.mometa.vnic.VnicEtherIf import VnicEtherIf changed = False try: mo_exists = False props_match = False # dn is <org_dn>/lan-conn-pol-<name> dn = module.params['org_dn'] + '/lan-conn-pol-' + module.params['name'] mo = ucs.login_handle.query_dn(dn) if mo: mo_exists = True if module.params['state'] == 'absent': # mo must exist but all properties do not have to match if mo_exists: if not module.check_mode: ucs.login_handle.remove_mo(mo) ucs.login_handle.commit() changed = True else: # set default params. Done here to set values for lists which can't be done in the argument_spec if module.params.get('vnic_list'): for vnic in module.params['vnic_list']: if not vnic.get('adapter_policy'): vnic['adapter_policy'] = '' if not vnic.get('order'): vnic['order'] = 'unspecified' if mo_exists: # check top-level mo props kwargs = dict(descr=module.params['description']) if (mo.check_prop_match(**kwargs)): # top-level props match, check next level mo/props if not module.params.get('vnic_list'): props_match = True else: # check vnicEther props for vnic in module.params['vnic_list']: child_dn = dn + '/ether-' + vnic['name'] mo_1 = ucs.login_handle.query_dn(child_dn) if mo_1: kwargs = dict( adaptor_profile_name=vnic['adapter_policy'] ) kwargs['order'] = vnic['order'] kwargs['nw_templ_name'] = vnic['vnic_template'] if (mo_1.check_prop_match(**kwargs)): props_match = True if not props_match: if not module.check_mode: # create if mo does not already exist mo = VnicLanConnPolicy( parent_mo_or_dn=module.params['org_dn'], name=module.params['name'], descr=module.params['description'], ) if module.params.get('vnic_list'): for vnic in module.params['vnic_list']: mo_1 = VnicEther( addr='derived', parent_mo_or_dn=mo, name=vnic['name'], adaptor_profile_name=vnic['adapter_policy'], nw_templ_name=vnic['vnic_template'], order=vnic['order'], ) 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)
name=vlan) handle.add_mo(mo) handle.commit() if row['LAN_Policy'] and row['vNIC_Name'] and row[ 'vNIC_Order'] and row['LAN_Policy_vNIC_Template']: # LAN Connectivity policy from ucsmsdk.mometa.vnic.VnicLanConnPolicy import VnicLanConnPolicy from ucsmsdk.mometa.vnic.VnicEther import VnicEther # if policy doesn't exist, create it dn = org + '/lan-conn-pol-' + row['LAN_Policy'] mo = handle.query_dn(dn) if not mo: mo = VnicLanConnPolicy(parent_mo_or_dn=org, policy_owner="local", name=row['LAN_Policy'], descr="") # add vnic and set vcon and vcon order. Policy is always "Linux" mo_1 = VnicEther(parent_mo_or_dn=mo, cdn_prop_in_sync="yes", nw_ctrl_policy_name="", admin_host_port="ANY", admin_vcon="1", stats_policy_name="default", admin_cdn_name="", switch_id="NONE", pin_to_group_name="", name=row['vNIC_Name'], order=row['vNIC_Order'], qos_policy_name="", adaptor_profile_name="Linux",