def main(): """ Main function :returns: Server Anti-Affinity Group information """ module = AnsibleModule(argument_spec=dict( auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), network_domain=dict(required=True, type='str'), servers=dict(required=True, type='list', elements='str'), state=dict(default='present', required=False, choices=['present', 'absent'])), supports_check_mode=True) network_domain_name = module.params.get('network_domain') network_domain_id = aa_group = None server_ids = list() datacenter = module.params.get('datacenter') state = module.params.get('state') try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json( msg='Invalid region. Regions must be one of {0}'.format(regions)) if credentials is False: module.fail_json(msg='Could not load the user credentials') try: client = NTTMCPClient(credentials, module.params.get('region')) except NTTMCPAPIException as e: module.fail_json(msg=e.msg) # Get the CND try: network = client.get_network_domain_by_name(network_domain_name, datacenter) network_domain_id = network.get('id') except (KeyError, IndexError, AttributeError, NTTMCPAPIException): module.fail_json(msg='Could not find the Cloud Network Domain: {0}'. format(network_domain_name)) # Get the servers for server in module.params.get('servers'): try: server = client.get_server_by_name( datacenter=datacenter, network_domain_id=network_domain_id, name=server) if server: server_ids.append(server.get('id')) else: module.fail_json( msg='Could not find the server - {0} in {1}'.format( server, datacenter)) except (KeyError, IndexError, AttributeError): module.fail_json( msg='Could not find the server - {0} in {1}'.format( server, datacenter)) # Attempt to find any existing AntiAffinity Group for this server combination try: aa_group = client.get_anti_affinity_group_by_servers( server_ids[0], server_ids[1]) except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json( msg='Could not retrieve any Server Anti Affinity Groups - {0}'. format(e)) try: if state == 'present': if aa_group: module.exit_json( msg='The server anti-affinity group already exists', data=aa_group) if module.check_mode: module.exit_json( msg='A new server anti-affinity group will be created') client.create_anti_affinity_group(server_ids[0], server_ids[1]) aa_group = client.get_anti_affinity_group_by_servers( server_ids[0], server_ids[1]) module.exit_json(changed=True, data=aa_group) elif state == 'absent': if not aa_group: module.exit_json( msg= 'No server anti-affinity group exists for this server combination' ) if module.check_mode: module.exit_json( msg='The server anti-affinity group {0} will be removed'. format(aa_group.get('id'))) client.delete_anti_affinity_group(aa_group.get('id')) module.exit_json( changed=True, msg='The Anti-Affinity Group {0} was successfully removed'. format(aa_group.get('id'))) except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json(msg='{0}'.format(e).replace('"', '\''))
def main(): """ Main function :returns: Server Anti-Affinity Group information """ module = AnsibleModule( argument_spec=dict( auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), network_domain=dict(default=None, type='str'), servers=dict(default=list(), type='list', elements='str'), ), supports_check_mode=True ) network_domain_name = module.params.get('network_domain') network_domain_id = None server_ids = list() datacenter = module.params.get('datacenter') return_data = return_object('antiaffinity_group') try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json(msg='Invalid region. Regions must be one of {0}'.format(regions)) if credentials is False: module.fail_json(msg='Could not load the user credentials') try: client = NTTMCPClient(credentials, module.params.get('region')) except NTTMCPAPIException as e: module.fail_json(msg=e.msg) # Get the CND if network_domain_name: try: network = client.get_network_domain_by_name(network_domain_name, datacenter) network_domain_id = network.get('id') except (KeyError, IndexError, AttributeError, NTTMCPAPIException): module.fail_json(msg='Could not find the Cloud Network Domain: {0}'.format(network_domain_name)) # Get the servers for server in module.params.get('servers'): try: server = client.get_server_by_name(datacenter=datacenter, network_domain_id=network_domain_id, name=server) if server: server_ids.append(server.get('id')) else: module.warn(warning='Could not find the server - {0} in {1}'.format(server, datacenter)) except (KeyError, IndexError, AttributeError): module.warn(warning='Could not find the server - {0} in {1}'.format(server, datacenter)) try: if len(server_ids) == 0: return_data['antiaffinity_group'] = client.list_server_anti_affinity_groups(network_domain_id=network_domain_id) elif len(server_ids) == 1: return_data['antiaffinity_group'] = client.list_server_anti_affinity_groups(network_domain_id, server_ids[0]) # Currently a server can belong to only a single antiaffinity group, while it doesn't do much more then the # case above today, this next section will handle any future situations were a server can belong to multiple # antiaffinity groups elif len(server_ids) == 2: return_data['antiaffinity_group'] = [client.get_anti_affinity_group_by_servers(server_ids[0], server_ids[1])] return_data['count'] = len(return_data['antiaffinity_group']) module.exit_json(data=return_data) except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json(msg='Could not retrieve any Server Anti Affinity Groups - {0}'.format(e))