2013-03-16 05:29:16 +00:00
|
|
|
#!/usr/bin/env python
|
2012-08-28 19:56:16 +00:00
|
|
|
|
|
|
|
# (c) 2012, Marco Vito Moscaritolo <marco@agavee.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible,
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
import os
|
|
|
|
import ConfigParser
|
|
|
|
from novaclient import client as nova_client
|
|
|
|
|
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except:
|
|
|
|
import simplejson as json
|
|
|
|
|
2014-08-03 05:02:29 +00:00
|
|
|
from ansible.module_utils.openstack import *
|
|
|
|
|
2012-08-28 19:56:16 +00:00
|
|
|
###################################################
|
|
|
|
# executed with no parameters, return the list of
|
|
|
|
# all groups and hosts
|
|
|
|
|
2014-06-05 18:05:02 +00:00
|
|
|
NOVA_CONFIG_FILES = [os.getcwd() + "/nova.ini",
|
|
|
|
os.path.expanduser(os.environ.get('ANSIBLE_CONFIG', "~/nova.ini")),
|
|
|
|
"/etc/ansible/nova.ini"]
|
|
|
|
|
2014-07-01 16:41:55 +00:00
|
|
|
NOVA_DEFAULTS = {
|
|
|
|
'auth_system': None,
|
|
|
|
'region_name': None,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-10 16:31:40 +00:00
|
|
|
def nova_load_config_file():
|
2014-07-01 16:41:55 +00:00
|
|
|
p = ConfigParser.SafeConfigParser(NOVA_DEFAULTS)
|
2012-09-10 16:31:40 +00:00
|
|
|
|
2014-06-05 18:05:02 +00:00
|
|
|
for path in NOVA_CONFIG_FILES:
|
|
|
|
if os.path.exists(path):
|
|
|
|
p.read(path)
|
|
|
|
return p
|
|
|
|
|
|
|
|
return None
|
2012-09-10 16:31:40 +00:00
|
|
|
|
|
|
|
config = nova_load_config_file()
|
2014-06-05 18:05:02 +00:00
|
|
|
if not config:
|
|
|
|
sys.exit('Unable to find configfile in %s' % ', '.join(NOVA_CONFIG_FILES))
|
2012-08-28 19:56:16 +00:00
|
|
|
|
|
|
|
client = nova_client.Client(
|
2014-08-03 05:02:29 +00:00
|
|
|
config.get('openstack', 'version'),
|
|
|
|
config.get('openstack', 'username'),
|
|
|
|
config.get('openstack', 'api_key'),
|
|
|
|
config.get('openstack', 'project_id'),
|
|
|
|
config.get('openstack', 'auth_url'),
|
2012-10-09 19:43:58 +00:00
|
|
|
region_name = config.get('openstack', 'region_name'),
|
|
|
|
auth_system = config.get('openstack', 'auth_system')
|
2012-08-28 19:56:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if len(sys.argv) == 2 and (sys.argv[1] == '--list'):
|
|
|
|
groups = {}
|
|
|
|
|
|
|
|
# Cycle on servers
|
2014-08-03 05:02:29 +00:00
|
|
|
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')
|
2013-05-23 02:25:05 +00:00
|
|
|
|
|
|
|
# Define group (or set to empty string)
|
2014-08-03 05:02:29 +00:00
|
|
|
group = server.metadata['group'] if server.metadata.has_key('group') else 'undefined'
|
2012-08-28 19:56:16 +00:00
|
|
|
|
|
|
|
# Create group if not exist
|
|
|
|
if group not in groups:
|
2012-09-05 14:26:01 +00:00
|
|
|
groups[group] = []
|
2012-08-28 19:56:16 +00:00
|
|
|
|
|
|
|
# Append group to list
|
2014-08-03 05:02:29 +00:00
|
|
|
if server.accessIPv4:
|
|
|
|
groups[group].append(server.accessIPv4)
|
2013-05-23 02:25:05 +00:00
|
|
|
continue
|
|
|
|
if public:
|
|
|
|
groups[group].append(''.join(public))
|
|
|
|
continue
|
|
|
|
if private:
|
|
|
|
groups[group].append(''.join(private))
|
|
|
|
continue
|
2012-08-28 19:56:16 +00:00
|
|
|
|
|
|
|
# Return server list
|
2014-07-01 04:40:11 +00:00
|
|
|
print(json.dumps(groups, sort_keys=True, indent=2))
|
2012-08-28 19:56:16 +00:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
#####################################################
|
|
|
|
# executed with a hostname as a parameter, return the
|
|
|
|
# variables for that host
|
|
|
|
|
|
|
|
elif len(sys.argv) == 3 and (sys.argv[1] == '--host'):
|
|
|
|
results = {}
|
2013-05-23 02:25:05 +00:00
|
|
|
ips = []
|
2012-08-28 19:56:16 +00:00
|
|
|
for instance in client.servers.list():
|
2014-08-03 05:02:29 +00:00
|
|
|
private = openstack_find_nova_addresses(getattr(instance, 'addresses'), 'fixed', 'private')
|
|
|
|
public = openstack_find_nova_addresses(getattr(instance, 'addresses'), 'floating', 'public')
|
2013-05-23 02:25:05 +00:00
|
|
|
ips.append( instance.accessIPv4)
|
|
|
|
ips.append(''.join(private))
|
|
|
|
ips.append(''.join(public))
|
|
|
|
if sys.argv[2] in ips:
|
2012-08-28 19:56:16 +00:00
|
|
|
for key in vars(instance):
|
|
|
|
# Extract value
|
|
|
|
value = getattr(instance, key)
|
|
|
|
|
|
|
|
# Generate sanitized key
|
|
|
|
key = 'os_' + re.sub("[^A-Za-z0-9\-]", "_", key).lower()
|
|
|
|
|
|
|
|
# Att value to instance result (exclude manager class)
|
|
|
|
#TODO: maybe use value.__class__ or similar inside of key_name
|
|
|
|
if key != 'os_manager':
|
|
|
|
results[key] = value
|
|
|
|
|
2014-07-01 04:40:11 +00:00
|
|
|
print(json.dumps(results, sort_keys=True, indent=2))
|
2012-08-28 19:56:16 +00:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
else:
|
|
|
|
print "usage: --list ..OR.. --host <hostname>"
|
|
|
|
sys.exit(1)
|