def main(): argument_spec = ucs_argument_spec argument_spec.update( class_ids=dict(type='str'), distinguished_names=dict(type='str'), delegate_to=dict(type='str', default='localhost'), ) module = AnsibleModule( argument_spec, supports_check_mode=False, mutually_exclusive=[ ['class_ids', 'distinguished_names'], ], ) # UCSModule verifies ucsmsdk is present and exits on failure. # Imports are below for UCS object creation. ucs = UCSModule(module) err = False query_result = {} try: if module.params['class_ids']: class_ids = [ x.strip() for x in module.params['class_ids'].split(',') ] for class_id in class_ids: query_result[class_id] = [] ucs_mos = retrieve_class_id(class_id, ucs) if ucs_mos: for ucs_mo in ucs_mos: query_result[class_id].append(make_mo_dict(ucs_mo)) ucs.result['objects'] = query_result elif module.params['distinguished_names']: distinguished_names = [ x.strip() for x in module.params['distinguished_names'].split(',') ] for distinguished_name in distinguished_names: query_result[distinguished_name] = {} ucs_mo = retrieve_distinguished_name(distinguished_name, ucs) if ucs_mo: query_result[distinguished_name] = make_mo_dict(ucs_mo) ucs.result['objects'] = query_result except Exception as e: err = True ucs.result['msg'] = "setup error: %s " % str(e) if err: module.fail_json(**ucs.result) module.exit_json(**ucs.result)
def main(): argument_spec = ucs_argument_spec argument_spec.update( fabric=dict(type='str', default='common', choices=['common', 'A', 'B']), pattern=dict(type='str'), vlanid=dict(type='str') ) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, required_one_of=[['pattern', 'vlanid']] ) ucs = UCSModule(module) filtls = ['(cloud,"ethlan")'] if module.params['fabric'] != 'common': filtls.append('(switch_id,"' + module.params['fabric'] + '")') if module.params['vlanid']: filtls.append('(id,"' + module.params['vlanid'] + '")') else: filtls.append('(name,"' + module.params['pattern'] + '")') object_dict = ucs.login_handle.query_classid("fabricVlan", filter_str=' and '.join(filtls)) if object_dict is None: module.fail_json(msg="Failed to query vlan objects") vlnlist = [] for ob in object_dict: vlnlist.append(dict(name=ob.name, id=ob.id)) module.exit_json(changed=False, vlan_list=vlnlist)
def main(): object_dict = dict( module=dict(type='str', required=True), class_name=dict(type='str', aliases=['class'], required=True), properties=dict(type='dict', required=True), children=dict(type='list'), ) argument_spec = ucs_argument_spec argument_spec.update( objects=dict(type='list', elements='dict', options=object_dict, required=True), state=dict(type='str', choices=['present', 'absent'], default='present'), ) module = AnsibleModule( argument_spec, supports_check_mode=True, ) if not HAS_IMPORT_MODULE: module.fail_json(msg='import_module is required for this module') ucs = UCSModule(module) # note that all objects specified in the object list report a single result (including a single changed). ucs.result['changed'] = False for managed_object in module.params['objects']: traverse_objects(module, ucs, managed_object) # single commit for object and any children if not module.check_mode and ucs.result['changed']: try: ucs.login_handle.commit() except Exception as e: # generic Exception because UCSM can throw a variety of exceptions ucs.result['msg'] = "setup error: %s " % str(e) module.fail_json(**ucs.result) module.exit_json(**ucs.result)
def main(): argument_spec = ucs_argument_spec argument_spec.update( vlangroup=dict(type='str', required=True), vlanname=dict(type='str', required=True), state=dict(default='present', choices=['present', 'absent'], type='str'), ) module = AnsibleModule( argument_spec, supports_check_mode=True, required_if=[ ['state', 'present', ['vlangroup', 'vlanname']], ], ) ucs = UCSModule(module) err = False from ucsmsdk.mometa.fabric.FabricNetGroup import FabricNetGroup from ucsmsdk.mometa.fabric.FabricPooledVlan import FabricPooledVlan changed = False try: dnpooled1_exists = False # dn = fabric/lan/net-group-VLANGROUP # Check for VLAN Group dngroup = 'fabric/lan/net-group-' + module.params['vlangroup'] dngroup1 = ucs.login_handle.query_dn(dngroup) # dn = fabric/lan/net-VLANNAME # Check for VLAN dnvlan = 'fabric/lan/net-' + module.params['vlanname'] dnvlan1 = ucs.login_handle.query_dn(dnvlan) # dn = fabric/lan/net-group-VLANGROUP/net-VLANNAME # Check for VLAN within VLAN Group dnpooled = 'fabric/lan/net-group-' + module.params[ 'vlangroup'] + '/net-' + module.params['vlanname'] dnpooled1 = ucs.login_handle.query_dn(dnpooled) # Configuration MOs. Couldn't really get this to work off the DNs, so I built additional objects. mo = FabricNetGroup(parent_mo_or_dn="fabric/lan", name=module.params['vlangroup']) mo_1 = FabricPooledVlan(parent_mo_or_dn=mo, name=module.params['vlanname']) if not dngroup1: # Error out if the VLAN Group is missing err = True ucs.result['msg'] = module.params[ 'vlangroup'] + " VLAN Group not configured in UCS" if not dnvlan1: # Error out if VLAN is missing err = True ucs.result['msg'] = module.params[ 'vlanname'] + " VLAN not configured in UCS. Use ucs_vlans module to create the VLAN first" if dnpooled1: dnpooled1_exists = True if module.params['state'] == 'absent': if dnpooled1_exists: if not module.check_mode: ucs.login_handle.remove_mo(mo_1) ucs.login_handle.commit() changed = True if module.params['state'] == 'present': if not dnpooled1_exists: if not module.check_mode: 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(): argument_spec = ucs_argument_spec argument_spec.update( name=dict(type='str'), vsan_id=dict(type='str'), vlan_id=dict(type='str'), fc_zoning=dict(type='str', default='disabled', choices=['disabled', 'enabled']), fabric=dict(type='str', default='common', choices=['common', 'A', 'B']), state=dict(type='str', default='present', choices=['present', 'absent']), vsan_list=dict(type='list'), ) # Note that use of vsan_list is an experimental feature which allows multiple resource updates with a single UCSM connection. # Support for vsan_list may change or be removed once persistent UCS connections are supported. # Either vsan_list or name/vsan_id/vlan_id is required (user can specify either a list or single resource). module = AnsibleModule( argument_spec, supports_check_mode=True, required_one_of=[ ['vsan_list', 'name'] ], mutually_exclusive=[ ['vsan_list', 'name'] ], ) ucs = UCSModule(module) err = False from ucsmsdk.mometa.fabric.FabricVsan import FabricVsan changed = False try: # Only documented use is a single resource, but to also support experimental # feature allowing multiple updates all params are converted to a vsan_list below. if module.params['vsan_list']: # directly use the list (single resource and list are mutually exclusive vsan_list = module.params['vsan_list'] else: # single resource specified, create list from the current params vsan_list = [module.params] for vsan in vsan_list: mo_exists = False props_match = False # set default params. Done here to set values for lists which can't be done in the argument_spec if not vsan.get('fc_zoning'): vsan['fc_zoning'] = 'disabled' if not vsan.get('fabric'): vsan['fabric'] = 'common' # dn is fabric/san/net-<name> for common vsans or fabric/san/[A or B]/net-<name> for A or B dn_base = 'fabric/san' if vsan['fabric'] != 'common': dn_base += '/' + vsan['fabric'] dn = dn_base + '/net-' + vsan['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: if mo_exists: # check top-level mo props kwargs = dict(id=vsan['vsan_id']) kwargs['fcoe_vlan'] = vsan['vlan_id'] kwargs['zoning_state'] = vsan['fc_zoning'] if (mo.check_prop_match(**kwargs)): props_match = True if not props_match: if not module.check_mode: # create if mo does not already exist mo = FabricVsan( parent_mo_or_dn=dn_base, name=vsan['name'], id=vsan['vsan_id'], fcoe_vlan=vsan['vlan_id'], zoning_state=vsan['fc_zoning'], ) 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(): argument_spec = ucs_argument_spec argument_spec.update( name=dict(type='str', required=True), multicast_policy=dict(type='str', default=''), fabric=dict(type='str', default='common', choices=['common', 'A', 'B']), id=dict(type='str'), sharing=dict(type='str', default='none', choices=['none', 'primary', 'isolated', 'community']), native=dict(type='str', default='no', choices=['yes', 'no']), state=dict(type='str', default='present', choices=['present', 'absent']), ) module = AnsibleModule( argument_spec, supports_check_mode=True, required_if=[ ['state', 'present', ['id']], ], ) ucs = UCSModule(module) err = False # UCSModule creation above verifies ucsmsdk is present and exits on failure, so additional imports are done below. from ucsmsdk.mometa.fabric.FabricVlan import FabricVlan changed = False try: mo_exists = False props_match = False # dn is fabric/lan/net-<name> for common vlans or fabric/lan/[A or B]/net-<name> for A or B dn_base = 'fabric/lan' if module.params['fabric'] != 'common': dn_base += '/' + module.params['fabric'] dn = dn_base + '/net-' + 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: if mo_exists: # check top-level mo props kwargs = dict(id=module.params['id']) kwargs['default_net'] = module.params['native'] kwargs['sharing'] = module.params['sharing'] kwargs['mcast_policy_name'] = module.params['multicast_policy'] if mo.check_prop_match(**kwargs): props_match = True if not props_match: if not module.check_mode: # create if mo does not already exist mo = FabricVlan( parent_mo_or_dn=dn_base, name=module.params['name'], id=module.params['id'], default_net=module.params['native'], sharing=module.params['sharing'], mcast_policy_name=module.params['multicast_policy'], ) 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(): 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'), 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)
def main(): argument_spec = ucs_argument_spec argument_spec.update( org_dn=dict(type='str', default='org-root'), name=dict(type='str', required=True), bios_policy=dict(type='str', default=''), boot_policy=dict(type='str', default='default'), description=dict(type='str', aliases=['descr'], default=''), mgmt_ip_state=dict(type='str', default='pooled'), mgmt_ip_pool=dict(type='str', default='ext-mgmt'), graphics_card_policy=dict(type='str', default=''), host_firmware_package=dict(type='str', default=''), uuid_pool=dict(type='str', default='default'), kvm_mgmt_policy=dict(type='str', default=''), local_disk_policy=dict(type='str', default=''), maintenance_policy=dict(type='str', default=''), ipmi_access_profile=dict(type='str', default=''), power_control_policy=dict(type='str', default='default'), power_sync_policy=dict(type='str', default=''), scrub_policy=dict(type='str', default=''), sol_policy=dict(type='str', default=''), threshold_policy=dict(type='str', default='default'), template_type=dict(type='str', default='initial-template', choices=['initial-template', 'updating-template']), user_label=dict(type='str', default=''), vmedia_policy=dict(type='str', default=''), storage_profile=dict(type='str', default=''), lan_connectivity_policy=dict(type='str', default=''), iqn_pool=dict(type='str', default=''), san_connectivity_policy=dict(type='str', default=''), server_pool=dict(type='str', default=''), server_pool_qualification=dict(type='str', default=''), power_state=dict(type='str', default='up', choices=['up', 'down']), mgmt_interface_mode=dict(type='str', default='', choices=['', 'in-band']), mgmt_vnet_name=dict(type='str', default=''), mgmt_inband_pool_name=dict(type='str', default=''), state=dict(type='str', default='present', choices=['present', 'absent']), ) module = AnsibleModule( argument_spec, supports_check_mode=True, ) ucs = UCSModule(module) # UCSModule creation above verifies ucsmsdk is present and exits on failure. # Additional imports are done below or in called functions. ucs.result['changed'] = False props_match = False # dn is <org_dn>/ls-<name> dn = module.params['org_dn'] + '/ls-' + module.params['name'] mo = ucs.login_handle.query_dn(dn) if mo: if module.params['state'] == 'absent': # mo must exist but all properties do not have to match if not module.check_mode: ucs.login_handle.remove_mo(mo) ucs.login_handle.commit() ucs.result['changed'] = True else: # state == 'present' props_match = check_serivce_profile_templates_props( ucs, module, mo, dn) if module.params['state'] == 'present' and not props_match: configure_service_profile_template(ucs, module) 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)
def main(): argument_spec = ucs_argument_spec argument_spec.update( org_name=dict(type='str', aliases=['name']), parent_org_path=dict(type='str', default='root'), description=dict(type='str', aliases=['descr']), state=dict(type='str', default='present', choices=['present', 'absent']), delegate_to=dict(type='str', default='localhost'), ) module = AnsibleModule( argument_spec, supports_check_mode=True, required_if=[ ['state', 'present', ['org_name']], ], ) # UCSModule verifies ucsmsdk is present and exits on failure. # Imports are below for UCS object creation. ucs = UCSModule(module) from ucsmsdk.mometa.org.OrgOrg import OrgOrg err = False changed = False requested_state = module.params['state'] kwargs = dict() if module.params['description'] is not None: kwargs['descr'] = module.params['description'] try: parent_org_dn = 'org-' + module.params['parent_org_path'].replace( '/', '/org-') dn = parent_org_dn + '/org-' + module.params['org_name'] mo = ucs.login_handle.query_dn(dn) # Determine state change if mo: # Object exists, if it should exist has anything changed? if requested_state == 'present': # Do some or all Object properties not match, that is a change if not mo.check_prop_match(**kwargs): changed = True # Object does not exist but should, that is a change else: if requested_state == 'present': changed = True # Object exists but should not, that is a change if mo and requested_state == 'absent': changed = True # Apply state if not check_mode if changed and not module.check_mode: if requested_state == 'absent': ucs.login_handle.remove_mo(mo) else: kwargs['parent_mo_or_dn'] = parent_org_dn kwargs['name'] = module.params['org_name'] if module.params['description'] is not None: kwargs['descr'] = module.params['description'] mo = OrgOrg(**kwargs) ucs.login_handle.add_mo(mo, modify_present=True) ucs.login_handle.commit() 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(): argument_spec = ucs_argument_spec argument_spec.update( org_dn=dict(type='str', default='org-root'), service_profile_name=dict(type='str', required=True), server_assignment=dict(type='str', choices=['server', 'pool']), server_dn=dict(type='str'), server_pool_name=dict(type='str'), restrict_migration=dict(type='str', default='no', choices=['yes', 'no']), state=dict(default='present', choices=['present', 'absent'], type='str'), ) module = AnsibleModule( argument_spec, supports_check_mode=True, required_if=[ ['state', 'present', ['server_assignment']], ['server_assignment', 'server', ['server_dn']], ['server_assignment', 'pool', ['server_pool_name']], ], mutually_exclusive=[ ['server_dn', 'server_pool_name'], ], ) # UCSModule verifies ucsmsdk is present and exits on failure. Imports are below ucs object creation. ucs = UCSModule(module) err = False from ucsmsdk.mometa.ls.LsRequirement import LsRequirement from ucsmsdk.mometa.ls.LsBinding import LsBinding from ucsmsdk.mometa.ls.LsServer import LsServer changed = False ucs.result['assign_state'] = 'unassigned' ucs.result['assoc_state'] = 'unassociated' try: ls_mo_exists = False pn_mo_exists = False pn_req_mo_exists = False props_match = False # logical server distinguished name is <org>/ls-<name> and physical node dn appends 'pn' or 'pn-req' ls_dn = module.params['org_dn'] + '/ls-' + module.params[ 'service_profile_name'] ls_mo = ucs.login_handle.query_dn(ls_dn) if ls_mo: ls_mo_exists = True pn_dn = ls_dn + '/pn' pn_mo = ucs.login_handle.query_dn(pn_dn) if pn_mo: pn_mo_exists = True pn_req_dn = ls_dn + '/pn-req' pn_req_mo = ucs.login_handle.query_dn(pn_req_dn) if pn_req_mo: pn_req_mo_exists = True if module.params['state'] == 'absent': if ls_mo_exists and ls_mo.assign_state != 'unassigned': if pn_mo_exists: if not module.check_mode: ucs.login_handle.remove_mo(pn_mo) ucs.login_handle.commit() changed = True elif pn_req_mo_exists: if not module.check_mode: ucs.login_handle.remove_mo(pn_req_mo) ucs.login_handle.commit() changed = True elif ls_mo_exists: # check if logical server is assigned and associated ucs.result['assign_state'] = ls_mo.assign_state ucs.result['assoc_state'] = ls_mo.assoc_state if module.params[ 'server_assignment'] == 'pool' and pn_req_mo_exists: # check the current pool kwargs = dict(name=module.params['server_pool_name']) kwargs['restrict_migration'] = module.params[ 'restrict_migration'] if pn_req_mo.check_prop_match(**kwargs): props_match = True elif pn_mo_exists: kwargs = dict(pn_dn=module.params['server_dn']) kwargs['restrict_migration'] = module.params[ 'restrict_migration'] if pn_mo.check_prop_match(**kwargs): props_match = True if not props_match: if not module.check_mode: # create if mo does not already exist in desired state mo = LsServer( parent_mo_or_dn=module.params['org_dn'], name=module.params['service_profile_name'], ) if module.params['server_assignment'] == 'pool': if pn_mo_exists: ucs.login_handle.remove_mo(pn_mo) mo_1 = LsRequirement( parent_mo_or_dn=mo, name=module.params['server_pool_name'], restrict_migration=module. params['restrict_migration'], ) else: mo_1 = LsBinding( parent_mo_or_dn=mo, pn_dn=module.params['server_dn'], restrict_migration=module. params['restrict_migration'], ) ucs.login_handle.add_mo(mo_1, True) ucs.login_handle.commit() pn_req_mo = ucs.login_handle.query_dn(pn_req_dn) if pn_req_mo: # profiles from templates will add a server pool, so remove and add the server again ucs.login_handle.remove_mo(pn_req_mo) ucs.login_handle.add_mo(mo_1, True) ucs.login_handle.commit() ls_mo = ucs.login_handle.query_dn(ls_dn) if ls_mo: ucs.result['assign_state'] = ls_mo.assign_state ucs.result['assoc_state'] = ls_mo.assoc_state 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(): 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=''), fabric=dict(type='str', default='A', choices=['A', 'B', 'A-B', 'B-A']), redundancy_type=dict(type='str', default='none', choices=['none', 'primary', 'secondary']), peer_redundancy_template=dict(type='str', aliases=['peer_redundancy_templ'], default=''), target=dict(type='str', default='adapter', choices=['adapter', 'vm']), template_type=dict(type='str', default='initial-template', choices=['initial-template', 'updating-template']), vlans_list=dict(type='list'), cdn_source=dict(type='str', default='vnic-name', choices=['vnic-name', 'user-defined']), cdn_name=dict(type='str', default=''), mtu=dict(type='str', default='1500'), mac_pool=dict(type='str', default=''), qos_policy=dict(type='str', default=''), network_control_policy=dict(type='str', default=''), pin_group=dict(type='str', default=''), stats_policy=dict(type='str', default='default'), state=dict(type='str', default='present', choices=['present', 'absent']), ) module = AnsibleModule( argument_spec, supports_check_mode=True, required_if=[ ['cdn_source', 'user-defined', ['cdn_name']], ], ) 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.VnicLanConnTempl import VnicLanConnTempl from ucsmsdk.mometa.vnic.VnicEtherIf import VnicEtherIf changed = False try: mo_exists = False props_match = False # dn is <org_dn>/lan-conn-templ-<name> dn = module.params['org_dn'] + '/lan-conn-templ-' + 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 for lists which can't be done in the argument_spec if module.params.get('vlans_list'): for vlan in module.params['vlans_list']: if not vlan.get('native'): vlan['native'] = 'no' if not vlan.get('state'): vlan['state'] = 'present' # for target 'adapter', change to internal UCS Manager spelling 'adaptor' if module.params['target'] == 'adapter': module.params['target'] = 'adaptor' if mo_exists: # check top-level mo props kwargs = dict(descr=module.params['description']) kwargs['switch_id'] = module.params['fabric'] kwargs['redundancy_pair_type'] = module.params[ 'redundancy_type'] kwargs['peer_redundancy_templ_name'] = module.params[ 'peer_redundancy_template'] kwargs['ident_pool_name'] = module.params['mac_pool'] # do not check shared props if this is a secondary template if module.params['redundancy_type'] != 'secondary': kwargs['target'] = module.params['target'] kwargs['templ_type'] = module.params['template_type'] kwargs['cdn_source'] = module.params['cdn_source'] kwargs['admin_cdn_name'] = module.params['cdn_name'] kwargs['mtu'] = module.params['mtu'] kwargs['qos_policy_name'] = module.params['qos_policy'] kwargs['nw_ctrl_policy_name'] = module.params[ 'network_control_policy'] kwargs['pin_to_group_name'] = module.params['pin_group'] kwargs['stats_policy_name'] = module.params['stats_policy'] if mo.check_prop_match(**kwargs): # top-level props match, check next level mo/props if not module.params.get('vlans_list'): props_match = True else: # check vlan props for vlan in module.params['vlans_list']: child_dn = dn + '/if-' + str(vlan['name']) mo_1 = ucs.login_handle.query_dn(child_dn) if vlan['state'] == 'absent': if mo_1: props_match = False break else: if mo_1: kwargs = dict(default_net=vlan['native']) if mo_1.check_prop_match(**kwargs): props_match = True else: props_match = False break if not props_match: if not module.check_mode: # create if mo does not already exist # secondary template only sets non shared props if module.params['redundancy_type'] == 'secondary': mo = VnicLanConnTempl( parent_mo_or_dn=module.params['org_dn'], name=module.params['name'], descr=module.params['description'], switch_id=module.params['fabric'], redundancy_pair_type=module. params['redundancy_type'], peer_redundancy_templ_name=module. params['peer_redundancy_template'], ident_pool_name=module.params['mac_pool'], ) else: mo = VnicLanConnTempl( parent_mo_or_dn=module.params['org_dn'], name=module.params['name'], descr=module.params['description'], switch_id=module.params['fabric'], redundancy_pair_type=module. params['redundancy_type'], peer_redundancy_templ_name=module. params['peer_redundancy_template'], target=module.params['target'], templ_type=module.params['template_type'], cdn_source=module.params['cdn_source'], admin_cdn_name=module.params['cdn_name'], mtu=module.params['mtu'], ident_pool_name=module.params['mac_pool'], qos_policy_name=module.params['qos_policy'], nw_ctrl_policy_name=module. params['network_control_policy'], pin_to_group_name=module.params['pin_group'], stats_policy_name=module.params['stats_policy'], ) if module.params.get('vlans_list'): for vlan in module.params['vlans_list']: if vlan['state'] == 'absent': child_dn = dn + '/if-' + str(vlan['name']) mo_1 = ucs.login_handle.query_dn(child_dn) ucs.login_handle.remove_mo(mo_1) else: mo_1 = VnicEtherIf( parent_mo_or_dn=mo, name=str(vlan['name']), default_net=vlan['native'], ) 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(): argument_spec = ucs_argument_spec argument_spec.update( org_dn=dict(type='str', default='org-root'), name=dict(type='str'), descr=dict(type='str'), fabric=dict(type='str', default='A', choices=['A', 'B']), redundancy_type=dict(type='str', default='none', choices=['none', 'primary', 'secondary']), vsan=dict(type='str', default='default'), template_type=dict(type='str', default='initial-template', choices=['initial-template', 'updating-template']), max_data=dict(type='str', default='2048'), wwpn_pool=dict(type='str', default='default'), qos_policy=dict(type='str'), pin_group=dict(type='str'), stats_policy=dict(type='str', default='default'), state=dict(type='str', default='present', choices=['present', 'absent']), vhba_template_list=dict(type='list'), ) # Note that use of vhba_template_list is an experimental feature which allows multiple resource updates with a single UCSM connection. # Support for vhba_template_list may change or be removed once persistent UCS connections are supported. # Either vhba_template_list or name is required (user can specify either a list of single resource). module = AnsibleModule( argument_spec, supports_check_mode=True, required_one_of=[ ['vhba_template_list', 'name'] ], mutually_exclusive=[ ['vhba_template_list', 'name'] ], ) ucs = UCSModule(module) err = False from ucsmsdk.mometa.vnic.VnicSanConnTempl import VnicSanConnTempl from ucsmsdk.mometa.vnic.VnicFcIf import VnicFcIf changed = False try: # Only documented use is a single resource, but to also support experimental # feature allowing multiple updates all params are converted to a vhba_template_list below. if module.params['vhba_template_list']: # directly use the list (single resource and list are mutually exclusive vhba_template_list = module.params['vhba_template_list'] else: # single resource specified, create list from the current params vhba_template_list = [module.params] for vhba_template in vhba_template_list: mo_exists = False props_match = False # set default params. Done here to set values for lists which can't be done in the argument_spec if not vhba_template.get('descr'): vhba_template['descr'] = '' if not vhba_template.get('fabric'): vhba_template['fabric'] = 'A' if not vhba_template.get('redundancy_type'): vhba_template['redundancy_type'] = 'none' if not vhba_template.get('vsan'): vhba_template['vsan'] = 'default' if not vhba_template.get('template_type'): vhba_template['template_type'] = 'initial-template' if not vhba_template.get('max_data'): vhba_template['max_data'] = '2048' if not vhba_template.get('wwpn_pool'): vhba_template['wwpn_pool'] = 'default' if not vhba_template.get('qos_policy'): vhba_template['qos_policy'] = '' if not vhba_template.get('pin_group'): vhba_template['pin_group'] = '' if not vhba_template.get('stats_policy'): vhba_template['stats_policy'] = 'default' # dn is <org_dn>/san-conn-templ-<name> dn = module.params['org_dn'] + '/san-conn-templ-' + vhba_template['name'] mo = ucs.login_handle.query_dn(dn) if mo: mo_exists = True # check top-level mo props kwargs = dict(descr=vhba_template['descr']) kwargs['switch_id'] = vhba_template['fabric'] kwargs['redundancy_pair_type'] = vhba_template['redundancy_type'] kwargs['templ_type'] = vhba_template['template_type'] kwargs['max_data_field_size'] = vhba_template['max_data'] kwargs['ident_pool_name'] = vhba_template['wwpn_pool'] kwargs['qos_policy_name'] = vhba_template['qos_policy'] kwargs['pin_to_group_name'] = vhba_template['pin_group'] kwargs['stats_policy_name'] = vhba_template['stats_policy'] if (mo.check_prop_match(**kwargs)): # top-level props match, check next level mo/props child_dn = dn + '/if-default' mo_1 = ucs.login_handle.query_dn(child_dn) if mo_1: kwargs = dict(name=vhba_template['vsan']) if (mo_1.check_prop_match(**kwargs)): props_match = 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: if not props_match: if not module.check_mode: # create if mo does not already exist mo = VnicSanConnTempl( parent_mo_or_dn=module.params['org_dn'], name=vhba_template['name'], descr=vhba_template['descr'], switch_id=vhba_template['fabric'], redundancy_pair_type=vhba_template['redundancy_type'], templ_type=vhba_template['template_type'], max_data_field_size=vhba_template['max_data'], ident_pool_name=vhba_template['wwpn_pool'], qos_policy_name=vhba_template['qos_policy'], pin_to_group_name=vhba_template['pin_group'], stats_policy_name=vhba_template['stats_policy'], ) mo_1 = VnicFcIf( parent_mo_or_dn=mo, name=vhba_template['vsan'], ) 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 run_module(): argument_spec = ucs_argument_spec argument_spec.update( timezone=dict(type='str'), description=dict(type='str', aliases=['descr'], default=''), admin_state=dict(type='str', default='enabled', choices=['disabled', 'enabled']), state=dict(type='str', default='present', choices=['present', 'absent']), ) module = AnsibleModule( argument_spec, supports_check_mode=True, required_if=[ ['state', 'present', ['timezone']], ], ) ucs = UCSModule(module) err = False changed = False try: mo_exists = False props_match = False dn = 'sys/svc-ext/datetime-svc' 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: mo.timezone = "" mo.descr = "" ucs.login_handle.add_mo(mo, modify_present=True) ucs.login_handle.commit() changed = True else: if mo_exists: # check top-level mo props kwargs = dict(descr=module.params['description']) kwargs['timezone'] = module.params['timezone'] kwargs['admin_state'] = module.params['admin_state'] if mo.check_prop_match(**kwargs): props_match = True if not props_match: if not module.check_mode: # update mo, timezone mo always exists mo.timezone = module.params['timezone'] mo.descr = module.params['description'] mo.admin_state = module.params['admin_state'] ucs.login_handle.add_mo(mo, modify_present=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(): argument_spec = ucs_argument_spec argument_spec.update( org_dn=dict(type='str', default='org-root'), name=dict(type='str'), descr=dict(type='str'), wwnn_pool=dict(type='str', default='default'), vhba_list=dict(type='list'), state=dict(type='str', default='present', choices=['present', 'absent']), san_connectivity_list=dict(type='list'), ) # Note that use of san_connectivity_list is an experimental feature which allows multiple resource updates with a single UCSM connection. # Support for san_connectivity_list may change or be removed once persistent UCS connections are supported. # Either san_connectivity_list or name is required (user can specify either a list or single resource). module = AnsibleModule( argument_spec, supports_check_mode=True, required_one_of=[ ['san_connectivity_list', 'name'], ], mutually_exclusive=[ ['san_connectivity_list', 'name'], ], ) ucs = UCSModule(module) err = False from ucsmsdk.mometa.vnic.VnicSanConnPolicy import VnicSanConnPolicy from ucsmsdk.mometa.vnic.VnicFcNode import VnicFcNode from ucsmsdk.mometa.vnic.VnicFc import VnicFc from ucsmsdk.mometa.vnic.VnicFcIf import VnicFcIf changed = False try: # Only documented use is a single resource, but to also support experimental # feature allowing multiple updates all params are converted to a san_connectivity_list below. if module.params['san_connectivity_list']: # directly use the list (single resource and list are mutually exclusive san_connectivity_list = module.params['san_connectivity_list'] else: # single resource specified, create list from the current params san_connectivity_list = [module.params] for san_connectivity in san_connectivity_list: mo_exists = False props_match = False # set default params. Done here to set values for lists which can't be done in the argument_spec if not san_connectivity.get('descr'): san_connectivity['descr'] = '' if not san_connectivity.get('wwnn_pool'): san_connectivity['wwnn_pool'] = 'default' if san_connectivity.get('vhba_list'): for vhba in san_connectivity['vhba_list']: if not vhba.get('adapter_policy'): vhba['adapter_policy'] = '' if not vhba.get('order'): vhba['order'] = 'unspecified' # dn is <org_dn>/san-conn-pol-<name> dn = module.params['org_dn'] + '/san-conn-pol-' + san_connectivity['name'] mo = ucs.login_handle.query_dn(dn) if mo: mo_exists = True # check top-level mo props kwargs = dict(descr=san_connectivity['descr']) if (mo.check_prop_match(**kwargs)): # top-level props match, check next level mo/props # vnicFcNode object child_dn = dn + '/fc-node' mo_1 = ucs.login_handle.query_dn(child_dn) if mo_1: kwargs = dict(ident_pool_name=san_connectivity['wwnn_pool']) if (mo_1.check_prop_match(**kwargs)): if not san_connectivity.get('vhba_list'): props_match = True else: # check vnicFc props for vhba in san_connectivity['vhba_list']: child_dn = dn + '/fc-' + vhba['name'] mo_2 = ucs.login_handle.query_dn(child_dn) kwargs = {} kwargs['adaptor_profile_name'] = vhba['adapter_policy'] kwargs['order'] = vhba['order'] kwargs['nw_templ_name'] = vhba['vhba_template'] if (mo_2.check_prop_match(**kwargs)): props_match = 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: if not props_match: if not module.check_mode: # create if mo does not already exist mo = VnicSanConnPolicy( parent_mo_or_dn=module.params['org_dn'], name=san_connectivity['name'], descr=san_connectivity['descr'], ) mo_1 = VnicFcNode( parent_mo_or_dn=mo, ident_pool_name=san_connectivity['wwnn_pool'], addr='pool-derived', ) if san_connectivity.get('vhba_list'): for vhba in san_connectivity['vhba_list']: mo_2 = VnicFc( parent_mo_or_dn=mo, name=vhba['name'], adaptor_profile_name=vhba['adapter_policy'], nw_templ_name=vhba['vhba_template'], order=vhba['order'], ) mo_2_1 = VnicFcIf( parent_mo_or_dn=mo_2, name='default', ) 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 run_module(): argument_spec = ucs_argument_spec argument_spec.update( ntp_server=dict(type='str', aliases=['name']), description=dict(type='str', aliases=['descr'], default=''), state=dict(type='str', default='present', choices=['present', 'absent']), ) module = AnsibleModule( argument_spec, supports_check_mode=True, required_if=[ ['state', 'present', ['ntp_server']], ], ) # UCSModule verifies ucsmsdk is present and exits on failure. Imports are below ucs object creation. ucs = UCSModule(module) err = False from ucsmsdk.mometa.comm.CommNtpProvider import CommNtpProvider changed = False try: mo_exists = False props_match = False dn = 'sys/svc-ext/datetime-svc/ntp-' + module.params['ntp_server'] 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(descr=module.params['description']) if mo.check_prop_match(**kwargs): props_match = True if not props_match: if not module.check_mode: # update/add mo mo = CommNtpProvider(parent_mo_or_dn='sys/svc-ext/datetime-svc', name=module.params['ntp_server'], descr=module.params['description']) ucs.login_handle.add_mo(mo, modify_present=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(): argument_spec = ucs_argument_spec argument_spec.update( priority=dict(required=True, type='str', choices=[ "best-effort", "bronze", "fc", "gold", "platinum", "silver" ]), cos=dict(required=True, type='str'), weight=dict(required=True, type='str'), admin_state=dict(required=False, type='str', default='enabled', choices=['disabled', 'enabled']), drop=dict(required=False, type='str', default='drop', choices=['drop', 'no-drop']), mtu=dict(required=False, type='str', default='normal'), multicast_optimize=dict(required=False, type='str', default='no', choices=['false', 'no', 'true', 'yes']), ) module = AnsibleModule( argument_spec, supports_check_mode=True, ) ucs = UCSModule(module) err = False changed = False try: dn = "fabric/lan/classes/class-" + module.params['priority'] mo = ucs.login_handle.query_dn(dn) # check top-level mo props if module.params['priority'] == 'best-effort': kwargs = dict(weight=module.params['weight']) kwargs['mtu'] = module.params['mtu'] kwargs['multicast_optimize'] = module.params['multicast_optimize'] if not mo.check_prop_match(**kwargs): if not module.check_mode: mo.weight = module.params['weight'] mo.mtu = module.params['mtu'] mo.multicast_optimize = module.params['multicast_optimize'] ucs.login_handle.add_mo(mo, True) ucs.login_handle.commit() changed = True elif module.params['priority'] == 'fc': kwargs = dict(weight=module.params['weight']) kwargs['cos'] = module.params['cos'] if not mo.check_prop_match(**kwargs): if not module.check_mode: mo.weight = module.params['weight'] mo.cos = module.params['cos'] ucs.login_handle.add_mo(mo, True) ucs.login_handle.commit() changed = True else: kwargs = dict(weight=module.params['weight']) kwargs['priority'] = module.params['priority'] kwargs['mtu'] = module.params['mtu'] kwargs['cos'] = module.params['cos'] kwargs['drop'] = module.params['drop'] kwargs['admin_state'] = module.params['admin_state'] kwargs['multicast_optimize'] = module.params['multicast_optimize'] if not mo.check_prop_match(**kwargs): if not module.check_mode: mo.weight = module.params['weight'] mo.mtu = module.params['mtu'] mo.cos = module.params['cos'] mo.drop = module.params['drop'] mo.admin_state = module.params['admin_state'] mo.multicast_optimize = module.params['multicast_optimize'] 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(): local_lun = dict( name=dict(type='str', required=True), state=dict(type='str', default='present', choices=['present', 'absent']), size=dict(type='str', default='1'), auto_deploy=dict(type='str', default='auto-deploy', choices=['auto-deploy', 'no-auto-deploy']), expand_to_avail=dict(type='str', default='no', choices=['no', 'yes']), fractional_size=dict(type='str', default='0'), disk_policy_name=dict(type='str', default=''), ) 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=''), local_luns=dict(type='list', elements='dict', options=local_lun), 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.lstorage.LstorageProfile import LstorageProfile from ucsmsdk.mometa.lstorage.LstorageDasScsiLun import LstorageDasScsiLun ucs.result['changed'] = False try: mo_exists = False props_match = False # dn is <org_dn>/profile-<name> dn = module.params['org_dn'] + '/profile-' + 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() ucs.result['changed'] = True else: 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('local_luns'): props_match = True else: # check local lun props for lun in module.params['local_luns']: child_dn = dn + '/das-scsi-lun-' + lun['name'] mo_1 = ucs.login_handle.query_dn(child_dn) if lun['state'] == 'absent': if mo_1: props_match = False break else: if mo_1: kwargs = dict(size=str(lun['size'])) kwargs['auto_deploy'] = lun['auto_deploy'] kwargs['expand_to_avail'] = lun['expand_to_avail'] kwargs['fractional_size'] = str(lun['fractional_size']) kwargs['local_disk_policy_name'] = lun['disk_policy_name'] if mo_1.check_prop_match(**kwargs): props_match = True else: props_match = False break if not props_match: if not module.check_mode: # create if mo does not already exist mo = LstorageProfile( parent_mo_or_dn=module.params['org_dn'], name=module.params['name'], descr=module.params['description'], ) if module.params.get('local_luns'): for lun in module.params['local_luns']: if lun['state'] == 'absent': child_dn = dn + '/das-scsi-lun-' + lun['name'] mo_1 = ucs.login_handle.query_dn(child_dn) ucs.login_handle.remove_mo(mo_1) else: mo_1 = LstorageDasScsiLun( parent_mo_or_dn=mo, name=lun['name'], size=str(lun['size']), auto_deploy=lun['auto_deploy'], expand_to_avail=lun['expand_to_avail'], fractional_size=str(lun['fractional_size']), local_disk_policy_name=lun['disk_policy_name'], ) ucs.login_handle.add_mo(mo, True) ucs.login_handle.commit() ucs.result['changed'] = True except Exception as e: err = True ucs.result['msg'] = "setup error: %s " % str(e) if err: module.fail_json(**ucs.result) module.exit_json(**ucs.result)
def main(): argument_spec = ucs_argument_spec argument_spec.update( org_dn=dict(type='str', default='org-root'), name=dict(type='str'), purpose=dict(type='str', choices=['node', 'port']), descr=dict(type='str'), order=dict(type='str', default='default', choices=['default', 'sequential']), first_addr=dict(type='str'), last_addr=dict(type='str'), state=dict(type='str', default='present', choices=['present', 'absent']), wwn_list=dict(type='list'), ) # Note that use of wwn_list is an experimental feature which allows multiple resource updates with a single UCSM connection. # Support for wwn_list may change or be removed once persistent UCS connections are supported. # Either wwn_list or name is required (user can specify either a list or single resource). module = AnsibleModule( argument_spec, supports_check_mode=True, required_one_of=[['wwn_list', 'name']], mutually_exclusive=[['wwn_list', 'name']], ) ucs = UCSModule(module) err = False from ucsmsdk.mometa.fcpool.FcpoolInitiators import FcpoolInitiators from ucsmsdk.mometa.fcpool.FcpoolBlock import FcpoolBlock changed = False try: # Only documented use is a single resource, but to also support experimental # feature allowing multiple updates all params are converted to a wwn_list below. if module.params['wwn_list']: # directly use the list (single resource and list are mutually exclusive wwn_list = module.params['wwn_list'] else: # single resource specified, create list from the current params wwn_list = [module.params] for wwn in wwn_list: mo_exists = False props_match = False # set default params. Done here to set values for lists which can't be done in the argument_spec if not wwn.get('descr'): wwn['descr'] = '' if not wwn.get('order'): wwn['order'] = 'default' # dn is <org_dn>/wwn-pool-<name> for WWNN or WWPN dn = module.params['org_dn'] + '/wwn-pool-' + wwn['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: # append purpose param with suffix used by UCSM purpose_param = wwn['purpose'] + '-wwn-assignment' if mo_exists: # check top-level mo props kwargs = dict(assignment_order=wwn['order']) kwargs['descr'] = wwn['descr'] kwargs['purpose'] = purpose_param if (mo.check_prop_match(**kwargs)): # top-level props match, check next level mo/props if 'last_addr' in wwn and 'first_addr' in wwn: block_dn = dn + '/block-' + wwn['first_addr'].upper( ) + '-' + wwn['last_addr'].upper() mo_1 = ucs.login_handle.query_dn(block_dn) if mo_1: props_match = True else: props_match = True if not props_match: if not module.check_mode: # create if mo does not already exist mo = FcpoolInitiators( parent_mo_or_dn=module.params['org_dn'], name=wwn['name'], descr=wwn['descr'], assignment_order=wwn['order'], purpose=purpose_param, ) if 'last_addr' in wwn and 'first_addr' in wwn: mo_1 = FcpoolBlock( parent_mo_or_dn=mo, to=wwn['last_addr'], r_from=wwn['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)
def main(): vnic_spec = dict( name=dict(type='str', required=True), admin_vcon=dict(type='str', choices=['1', '2', '3', '4', 'any']), order=dict(type='str'), transport=dict(type='str', required=True, choices=['ethernet', 'fc']), state=dict(type='str', default='present', choices=['present', 'absent']), ) argument_spec = ucs_argument_spec argument_spec.update( sp_name=dict(required=True, type='str'), vnics=dict(required=True, type='list', elements='dict', options=vnic_spec), org_dn=dict(required=False, type='str', default='org-root'), ) module = AnsibleModule(argument_spec, supports_check_mode=True) ucs = UCSModule(module) err = False changed = False try: sp_dn = dn = module.params['org_dn'] + "/ls-" + module.params['sp_name'] sp = ucs.login_handle.query_dn(dn) if not sp: raise ValueError("SP '%s' does not exist" % sp_dn) for vnic in module.params['vnics']: vnic_mo = get_vnic( ucs, (get_vnic_dn(sp_dn, vnic['transport'], vnic['name']))) if vnic['state'] != 'absent' and not vnic_mo: raise ValueError( "vNIC '%s' is not assigned to service profile '%s'" % (vnic['name'], sp_dn)) if vnic_mo: if not matches_existing_vnic_order(vnic, vnic_mo): changed = True break if changed and not module.check_mode: for vnic in module.params['vnics']: vnic_mo = get_vnic( ucs, (get_vnic_dn(sp_dn, vnic['transport'], vnic['name']))) if vnic['state'] == 'absent' and vnic_mo: remove_vnic_assignment_order(ucs, vnic, sp) elif not vnic_mo: update_vnic_assignment_order(ucs, vnic, sp) elif not matches_existing_vnic_order(vnic, vnic_mo): update_vnic_assignment_order(ucs, vnic, sp) 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(): argument_spec = ucs_argument_spec argument_spec.update( org_dn=dict(type='str', default='org-root'), name=dict(required=True, type='str'), descr=dict(type='str'), description=dict(type='str', aliases=['descr']), graphics_card_mode=dict( type='str', choices=['any-configuration', 'compute', 'graphics']), state=dict(type='str', default='present', choices=['present', 'absent']), ) module = AnsibleModule( argument_spec, supports_check_mode=True, required_if=[ ['state', 'present', ['name']], ], ) # UCSModule verifies ucsmsdk is present and exits on failure. # Imports are below for UCS object creation. ucs = UCSModule(module) from importlib import import_module from ucsmsdk.ucscoreutils import get_meta_info # The Class(es) this module is managing module_file = 'ucsmsdk.mometa.compute.ComputeGraphicsCardPolicy' module_class = 'ComputeGraphicsCardPolicy' mo_module = import_module(module_file) mo_class = getattr(mo_module, module_class) META = get_meta_info(class_id=module_class) err = False changed = False requested_state = module.params['state'] kwargs = dict() # Manage Aliased Attributes for attribute in ['descr:description']: attribute_alias = attribute.split(':') if module.params[attribute_alias[1]] is not None: kwargs[attribute_alias[0]] = module.params[attribute_alias[1]] # Manage Attributes for attribute in ['graphics_card_mode', 'descr']: if module.params[attribute] is not None: kwargs[attribute] = module.params[attribute] try: dn = (module.params['org_dn'] + '/' + META.rn[0:META.rn.rindex('-') + 1] + module.params['name']) mo = ucs.login_handle.query_dn(dn) # Determine state change if mo: # Object exists, if it should exist has anything changed? if requested_state == 'present': # Do some or all Object properties not match, that is a change if not mo.check_prop_match(**kwargs): changed = True # Object does not exist but should, that is a change else: if requested_state == 'present': changed = True # Object exists but should not, that is a change if mo and requested_state == 'absent': changed = True # Apply state if not check_mode if changed and not module.check_mode: if requested_state == 'absent': ucs.login_handle.remove_mo(mo) else: kwargs['parent_mo_or_dn'] = module.params['org_dn'] kwargs['name'] = module.params['name'] mo = mo_class(**kwargs) ucs.login_handle.add_mo(mo, modify_present=True) ucs.login_handle.commit() 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(): argument_spec = ucs_argument_spec argument_spec.update( name=dict(type='str', required=True), description=dict(type='str', default=''), trigger_config=dict(type='str', default='', choices=['on-next-boot']), uptime_disr=dict(type='str', required=True, choices=['immediate', 'timer-automatic', 'user-ack']), 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, so additional imports are done below. from ucsmsdk.mometa.lsmaint.LsmaintMaintPolicy import LsmaintMaintPolicy changed = False try: mo_exists = False props_match = False dn_base = 'org-root' dn = dn_base + '/maint-' + 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: if mo_exists: # check top-level mo props kwargs = dict(name=module.params['name']) kwargs['descr'] = module.params['description'] kwargs['trigger_config'] = module.params['trigger_config'] kwargs['uptime_disr'] = module.params['uptime_disr'] if mo.check_prop_match(**kwargs): props_match = True if not props_match: if not module.check_mode: # create if mo does not already exist mo = LsmaintMaintPolicy( parent_mo_or_dn=dn_base, name=module.params['name'], descr=module.params['description'], trigger_config=module.params['trigger_config'], uptime_disr=module.params['uptime_disr'], ) 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(): argument_spec = ucs_argument_spec argument_spec.update( org_dn=dict(type='str', default='org-root'), name=dict(type='str', required=True), source_template=dict(type='str', required=True), user_label=dict(type='str', default=''), power_state=dict(type='str', choices=['up', 'down']), 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.ls.LsServer import LsServer from ucsmsdk.mometa.ls.LsPower import LsPower changed = False try: mo_exists = False props_match = False dn = module.params['org_dn'] + '/ls-' + 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: if mo_exists: # check top-level mo props kwargs = dict(src_templ_name=module.params['source_template']) kwargs['usr_lbl'] = module.params['user_label'] # service profiles are of type 'instance' kwargs['type'] = 'instance' if mo.check_prop_match(**kwargs): # top-level props match if module.params.get('power_state'): child_dn = dn + '/power' mo_1 = ucs.login_handle.query_dn(child_dn) if mo_1: kwargs = dict(state=module.params['power_state']) if mo_1.check_prop_match(**kwargs): props_match = True else: # no power state provided, use existing state as match props_match = True if not props_match: if not module.check_mode: # create if mo does not already exist mo = LsServer( parent_mo_or_dn=module.params['org_dn'], name=module.params['name'], src_templ_name=module.params['source_template'], type='instance', usr_lbl=module.params['user_label'], ) if module.params.get('power_state'): admin_state = 'admin-' + module.params['power_state'] mo_1 = LsPower( parent_mo_or_dn=mo, state=admin_state, ) 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(): manual_disk = dict( slot_num=dict(type='str', required=True), role=dict(type='str', default='normal', choices=['normal', 'ded-hot-spare', 'glob-hot-spare']), span_id=dict(type='str', default='unspecified'), 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), description=dict(type='str', aliases=['descr'], default=''), raid_level=dict( type='str', default='stripe', choices=[ 'stripe', 'mirror', 'mirror-stripe', 'stripe-parity', 'stripe-dual-parity', 'stripe-parity-stripe', 'stripe-dual-parity-stripe', ], ), num_drives=dict(type='str', default='1'), configuration_mode=dict(type='str', default='automatic', choices=['automatic', 'manual']), num_ded_hot_spares=dict(type='str', default='unspecified'), num_glob_hot_spares=dict(type='str', default='unspecified'), drive_type=dict(type='str', default='unspecified', choices=['unspecified', 'HDD', 'SSD']), use_remaining_disks=dict(type='str', default='no', choices=['yes', 'no']), min_drive_size=dict(type='str', default='unspecified'), manual_disks=dict(type='list', elements='dict', options=manual_disk), state=dict(type='str', default='present', choices=['present', 'absent']), virtual_drive=dict(type='dict', options=_virtual_drive_argument_spec()), ) module = AnsibleModule( argument_spec, supports_check_mode=True, ) ucs = UCSModule(module) # UCSModule creation above verifies ucsmsdk is present and exits on failure. # Additional imports are done below or in called functions. ucs.result['changed'] = False props_match = False # dn is <org_dn>/disk-group-config-<name> dn = module.params['org_dn'] + '/disk-group-config-' + module.params['name'] mo = ucs.login_handle.query_dn(dn) if mo: if module.params['state'] == 'absent': # mo must exist but all properties do not have to match if not module.check_mode: ucs.login_handle.remove_mo(mo) ucs.login_handle.commit() ucs.result['changed'] = True else: # state == 'present' props_match = check_disk_policy_props(ucs, module, mo, dn) if module.params['state'] == 'present' and not props_match: configure_disk_policy(ucs, module, dn) module.exit_json(**ucs.result)