mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
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.
This commit is contained in:
parent
a12086f1bf
commit
a05804bf8d
3 changed files with 35 additions and 33 deletions
|
@ -54,3 +54,16 @@ def openstack_argument_spec():
|
||||||
else:
|
else:
|
||||||
spec['login_tenant_name'] = dict(required=True)
|
spec['login_tenant_name'] = dict(required=True)
|
||||||
return spec
|
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
|
||||||
|
|
|
@ -382,19 +382,6 @@ def _add_floating_ip(module, nova, server):
|
||||||
return 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):
|
def _get_image_id(module, nova):
|
||||||
if module.params['image_name']:
|
if module.params['image_name']:
|
||||||
for image in nova.images.list():
|
for image in nova.images.list():
|
||||||
|
@ -446,8 +433,8 @@ def _create_server(module, nova):
|
||||||
if server.status == 'ACTIVE':
|
if server.status == 'ACTIVE':
|
||||||
server = _add_floating_ip(module, nova, server)
|
server = _add_floating_ip(module, nova, server)
|
||||||
|
|
||||||
private = _get_ips(getattr(server, 'addresses'), 'fixed', 'private')
|
private = openstack_find_nova_addresses(getattr(server, 'addresses'), 'fixed', 'private')
|
||||||
public = _get_ips(getattr(server, 'addresses'), 'floating', 'public')
|
public = openstack_find_nova_addresses(getattr(server, 'addresses'), 'floating', 'public')
|
||||||
|
|
||||||
# now exit with info
|
# 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)
|
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")
|
module.fail_json(msg = "Timeout waiting for the server to come up.. Please check manually")
|
||||||
if server.status == 'ERROR':
|
if server.status == 'ERROR':
|
||||||
module.fail_json(msg = "Error in creating the server.. Please check manually")
|
module.fail_json(msg = "Error in creating the server.. Please check manually")
|
||||||
private = _get_ips(getattr(server, 'addresses'), 'fixed', 'private')
|
private = openstack_find_nova_addresses(getattr(server, 'addresses'), 'fixed', 'private')
|
||||||
public = _get_ips(getattr(server, 'addresses'), 'floating', 'public')
|
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)
|
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):
|
def _check_floating_ips(module, nova, server):
|
||||||
changed = False
|
changed = False
|
||||||
if module.params['floating_ip_pools'] or module.params['floating_ips'] or module.params['auto_floating_ip']:
|
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 not ips:
|
||||||
# If we're configured to have a floating but we don't have one,
|
# If we're configured to have a floating but we don't have one,
|
||||||
# let's add one
|
# let's add one
|
||||||
|
@ -516,8 +503,8 @@ def _get_server_state(module, nova):
|
||||||
if server.status != 'ACTIVE':
|
if server.status != 'ACTIVE':
|
||||||
module.fail_json( msg="The VM is available but not Active. state:" + server.status)
|
module.fail_json( msg="The VM is available but not Active. state:" + server.status)
|
||||||
(ip_changed, server) = _check_floating_ips(module, nova, server)
|
(ip_changed, server) = _check_floating_ips(module, nova, server)
|
||||||
private = _get_ips(getattr(server, 'addresses'), 'fixed', 'private')
|
private = openstack_find_nova_addresses(getattr(server, 'addresses'), 'fixed', 'private')
|
||||||
public = _get_ips(getattr(server, 'addresses'), 'floating', 'public')
|
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)
|
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':
|
if server and module.params['state'] == 'absent':
|
||||||
return True
|
return True
|
||||||
|
|
|
@ -28,6 +28,8 @@ try:
|
||||||
except:
|
except:
|
||||||
import simplejson as json
|
import simplejson as json
|
||||||
|
|
||||||
|
from ansible.module_utils.openstack import *
|
||||||
|
|
||||||
###################################################
|
###################################################
|
||||||
# executed with no parameters, return the list of
|
# executed with no parameters, return the list of
|
||||||
# all groups and hosts
|
# all groups and hosts
|
||||||
|
@ -57,12 +59,12 @@ if not config:
|
||||||
sys.exit('Unable to find configfile in %s' % ', '.join(NOVA_CONFIG_FILES))
|
sys.exit('Unable to find configfile in %s' % ', '.join(NOVA_CONFIG_FILES))
|
||||||
|
|
||||||
client = nova_client.Client(
|
client = nova_client.Client(
|
||||||
version = config.get('openstack', 'version'),
|
config.get('openstack', 'version'),
|
||||||
username = config.get('openstack', 'username'),
|
config.get('openstack', 'username'),
|
||||||
api_key = config.get('openstack', 'api_key'),
|
config.get('openstack', 'api_key'),
|
||||||
auth_url = config.get('openstack', 'auth_url'),
|
config.get('openstack', 'project_id'),
|
||||||
|
config.get('openstack', 'auth_url'),
|
||||||
region_name = config.get('openstack', 'region_name'),
|
region_name = config.get('openstack', 'region_name'),
|
||||||
project_id = config.get('openstack', 'project_id'),
|
|
||||||
auth_system = config.get('openstack', 'auth_system')
|
auth_system = config.get('openstack', 'auth_system')
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -70,20 +72,20 @@ if len(sys.argv) == 2 and (sys.argv[1] == '--list'):
|
||||||
groups = {}
|
groups = {}
|
||||||
|
|
||||||
# Cycle on servers
|
# Cycle on servers
|
||||||
for f in client.servers.list():
|
for server in client.servers.list():
|
||||||
private = [ x['addr'] for x in getattr(f, 'addresses').itervalues().next() if x['OS-EXT-IPS:type'] == 'fixed']
|
private = openstack_find_nova_addresses(getattr(server, 'addresses'), 'fixed', 'private')
|
||||||
public = [ x['addr'] for x in getattr(f, 'addresses').itervalues().next() if x['OS-EXT-IPS:type'] == 'floating']
|
public = openstack_find_nova_addresses(getattr(server, 'addresses'), 'floating', 'public')
|
||||||
|
|
||||||
# Define group (or set to empty string)
|
# 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
|
# Create group if not exist
|
||||||
if group not in groups:
|
if group not in groups:
|
||||||
groups[group] = []
|
groups[group] = []
|
||||||
|
|
||||||
# Append group to list
|
# Append group to list
|
||||||
if f.accessIPv4:
|
if server.accessIPv4:
|
||||||
groups[group].append(f.accessIPv4)
|
groups[group].append(server.accessIPv4)
|
||||||
continue
|
continue
|
||||||
if public:
|
if public:
|
||||||
groups[group].append(''.join(public))
|
groups[group].append(''.join(public))
|
||||||
|
@ -104,8 +106,8 @@ elif len(sys.argv) == 3 and (sys.argv[1] == '--host'):
|
||||||
results = {}
|
results = {}
|
||||||
ips = []
|
ips = []
|
||||||
for instance in client.servers.list():
|
for instance in client.servers.list():
|
||||||
private = [ x['addr'] for x in getattr(instance, 'addresses').itervalues().next() if x['OS-EXT-IPS:type'] == 'fixed']
|
private = openstack_find_nova_addresses(getattr(instance, 'addresses'), 'fixed', 'private')
|
||||||
public = [ x['addr'] for x in getattr(instance, 'addresses').itervalues().next() if x['OS-EXT-IPS:type'] == 'floating']
|
public = openstack_find_nova_addresses(getattr(instance, 'addresses'), 'floating', 'public')
|
||||||
ips.append( instance.accessIPv4)
|
ips.append( instance.accessIPv4)
|
||||||
ips.append(''.join(private))
|
ips.append(''.join(private))
|
||||||
ips.append(''.join(public))
|
ips.append(''.join(public))
|
||||||
|
|
Loading…
Add table
Reference in a new issue