From a05804bf8d384cb5ff0916f7f4994679e137acca Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Sat, 2 Aug 2014 22:02:29 -0700 Subject: [PATCH] Update the nova inventory plugin with new ip code The provisioning module knows more about how nova deals with IP addresses now. Ensure that the inventory module is similarly as smart by separating out the logic into the openstack/module_utils. --- lib/ansible/module_utils/openstack.py | 13 +++++++++++++ library/cloud/nova_compute | 27 +++++++------------------- plugins/inventory/nova.py | 28 ++++++++++++++------------- 3 files changed, 35 insertions(+), 33 deletions(-) diff --git a/lib/ansible/module_utils/openstack.py b/lib/ansible/module_utils/openstack.py index c70eb9fbfa..7f955a112d 100644 --- a/lib/ansible/module_utils/openstack.py +++ b/lib/ansible/module_utils/openstack.py @@ -54,3 +54,16 @@ def openstack_argument_spec(): else: spec['login_tenant_name'] = dict(required=True) return spec + + +def openstack_find_nova_addresses(addresses, ext_tag, key_name=None): + + ret = [] + for (k, v) in addresses.iteritems(): + if key_name and k == key_name: + ret.extend([addrs['addr'] for addrs in v]) + else: + for interface_spec in v: + if 'OS-EXT-IPS:type' in interface_spec and interface_spec['OS-EXT-IPS:type'] == ext_tag: + ret.append(interface_spec['addr']) + return ret diff --git a/library/cloud/nova_compute b/library/cloud/nova_compute index 933d571493..2358d7cb63 100644 --- a/library/cloud/nova_compute +++ b/library/cloud/nova_compute @@ -382,19 +382,6 @@ def _add_floating_ip(module, nova, server): return server -def _get_ips(addresses, ext_tag, key_name=None): - - ret = [] - for (k, v) in addresses.iteritems(): - if key_name and k == key_name: - ret.extend([addrs['addr'] for addrs in v]) - else: - for interface_spec in v: - if 'OS-EXT-IPS:type' in interface_spec and interface_spec['OS-EXT-IPS:type'] == ext_tag: - ret.append(interface_spec['addr']) - return ret - - def _get_image_id(module, nova): if module.params['image_name']: for image in nova.images.list(): @@ -446,8 +433,8 @@ def _create_server(module, nova): if server.status == 'ACTIVE': server = _add_floating_ip(module, nova, server) - private = _get_ips(getattr(server, 'addresses'), 'fixed', 'private') - public = _get_ips(getattr(server, 'addresses'), 'floating', 'public') + private = openstack_find_nova_addresses(getattr(server, 'addresses'), 'fixed', 'private') + public = openstack_find_nova_addresses(getattr(server, 'addresses'), 'floating', 'public') # now exit with info module.exit_json(changed = True, id = server.id, private_ip=''.join(private), public_ip=''.join(public), status = server.status, info = server._info) @@ -459,8 +446,8 @@ def _create_server(module, nova): module.fail_json(msg = "Timeout waiting for the server to come up.. Please check manually") if server.status == 'ERROR': module.fail_json(msg = "Error in creating the server.. Please check manually") - private = _get_ips(getattr(server, 'addresses'), 'fixed', 'private') - public = _get_ips(getattr(server, 'addresses'), 'floating', 'public') + private = openstack_find_nova_addresses(getattr(server, 'addresses'), 'fixed', 'private') + public = openstack_find_nova_addresses(getattr(server, 'addresses'), 'floating', 'public') module.exit_json(changed = True, id = info['id'], private_ip=''.join(private), public_ip=''.join(public), status = server.status, info = server._info) @@ -473,7 +460,7 @@ def _delete_floating_ip_list(module, nova, server, extra_ips): def _check_floating_ips(module, nova, server): changed = False if module.params['floating_ip_pools'] or module.params['floating_ips'] or module.params['auto_floating_ip']: - ips = _get_ips(server.addresses, 'floating') + ips = openstack_find_nova_addresses(server.addresses, 'floating') if not ips: # If we're configured to have a floating but we don't have one, # let's add one @@ -516,8 +503,8 @@ def _get_server_state(module, nova): if server.status != 'ACTIVE': module.fail_json( msg="The VM is available but not Active. state:" + server.status) (ip_changed, server) = _check_floating_ips(module, nova, server) - private = _get_ips(getattr(server, 'addresses'), 'fixed', 'private') - public = _get_ips(getattr(server, 'addresses'), 'floating', 'public') + private = openstack_find_nova_addresses(getattr(server, 'addresses'), 'fixed', 'private') + public = openstack_find_nova_addresses(getattr(server, 'addresses'), 'floating', 'public') module.exit_json(changed = ip_changed, id = server.id, public_ip = ''.join(public), private_ip = ''.join(private), info = server._info) if server and module.params['state'] == 'absent': return True diff --git a/plugins/inventory/nova.py b/plugins/inventory/nova.py index 3ae19d0d17..585e26732e 100755 --- a/plugins/inventory/nova.py +++ b/plugins/inventory/nova.py @@ -28,6 +28,8 @@ try: except: import simplejson as json +from ansible.module_utils.openstack import * + ################################################### # executed with no parameters, return the list of # all groups and hosts @@ -57,12 +59,12 @@ if not config: sys.exit('Unable to find configfile in %s' % ', '.join(NOVA_CONFIG_FILES)) client = nova_client.Client( - version = config.get('openstack', 'version'), - username = config.get('openstack', 'username'), - api_key = config.get('openstack', 'api_key'), - auth_url = config.get('openstack', 'auth_url'), + config.get('openstack', 'version'), + config.get('openstack', 'username'), + config.get('openstack', 'api_key'), + config.get('openstack', 'project_id'), + config.get('openstack', 'auth_url'), region_name = config.get('openstack', 'region_name'), - project_id = config.get('openstack', 'project_id'), auth_system = config.get('openstack', 'auth_system') ) @@ -70,20 +72,20 @@ if len(sys.argv) == 2 and (sys.argv[1] == '--list'): groups = {} # Cycle on servers - for f in client.servers.list(): - private = [ x['addr'] for x in getattr(f, 'addresses').itervalues().next() if x['OS-EXT-IPS:type'] == 'fixed'] - public = [ x['addr'] for x in getattr(f, 'addresses').itervalues().next() if x['OS-EXT-IPS:type'] == 'floating'] + for server in client.servers.list(): + private = openstack_find_nova_addresses(getattr(server, 'addresses'), 'fixed', 'private') + public = openstack_find_nova_addresses(getattr(server, 'addresses'), 'floating', 'public') # Define group (or set to empty string) - group = f.metadata['group'] if f.metadata.has_key('group') else 'undefined' + group = server.metadata['group'] if server.metadata.has_key('group') else 'undefined' # Create group if not exist if group not in groups: groups[group] = [] # Append group to list - if f.accessIPv4: - groups[group].append(f.accessIPv4) + if server.accessIPv4: + groups[group].append(server.accessIPv4) continue if public: groups[group].append(''.join(public)) @@ -104,8 +106,8 @@ elif len(sys.argv) == 3 and (sys.argv[1] == '--host'): results = {} ips = [] for instance in client.servers.list(): - private = [ x['addr'] for x in getattr(instance, 'addresses').itervalues().next() if x['OS-EXT-IPS:type'] == 'fixed'] - public = [ x['addr'] for x in getattr(instance, 'addresses').itervalues().next() if x['OS-EXT-IPS:type'] == 'floating'] + private = openstack_find_nova_addresses(getattr(instance, 'addresses'), 'fixed', 'private') + public = openstack_find_nova_addresses(getattr(instance, 'addresses'), 'floating', 'public') ips.append( instance.accessIPv4) ips.append(''.join(private)) ips.append(''.join(public))