2015-02-12 01:11:42 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
# Copyright (c) 2012, Marco Vito Moscaritolo <marco@agavee.com>
|
|
|
|
# Copyright (c) 2013, Jesse Keating <jesse.keating@rackspace.com>
|
2015-03-01 17:56:10 +01:00
|
|
|
# Copyright (c) 2015, Hewlett-Packard Development Company, L.P.
|
2016-02-29 05:21:42 +01:00
|
|
|
# Copyright (c) 2016, Rackspace Australia
|
2015-02-12 01:11:42 +01:00
|
|
|
#
|
|
|
|
# This module 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.
|
|
|
|
#
|
|
|
|
# This software 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 this software. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2015-04-28 15:36:42 +02:00
|
|
|
# The OpenStack Inventory module uses os-client-config for configuration.
|
2016-11-08 16:41:38 +01:00
|
|
|
# https://github.com/openstack/os-client-config
|
2015-02-26 17:59:00 +01:00
|
|
|
# This means it will either:
|
|
|
|
# - Respect normal OS_* environment variables like other OpenStack tools
|
|
|
|
# - Read values from a clouds.yaml file.
|
|
|
|
# If you want to configure via clouds.yaml, you can put the file in:
|
|
|
|
# - Current directory
|
|
|
|
# - ~/.config/openstack/clouds.yaml
|
|
|
|
# - /etc/openstack/clouds.yaml
|
|
|
|
# - /etc/ansible/openstack.yml
|
|
|
|
# The clouds.yaml file can contain entries for multiple clouds and multiple
|
2017-03-07 10:10:40 +01:00
|
|
|
# regions of those clouds. If it does, this inventory module will by default
|
|
|
|
# connect to all of them and present them as one contiguous inventory. You
|
|
|
|
# can limit to one cloud by passing the `--cloud` parameter, or use the
|
|
|
|
# OS_CLOUD environment variable. If caching is enabled, and a cloud is
|
|
|
|
# selected, then per-cloud cache folders will be used.
|
2015-02-26 17:59:00 +01:00
|
|
|
#
|
|
|
|
# See the adjacent openstack.yml file for an example config file
|
2015-12-03 16:04:24 +01:00
|
|
|
# There are two ansible inventory specific options that can be set in
|
|
|
|
# the inventory section.
|
|
|
|
# expand_hostvars controls whether or not the inventory will make extra API
|
|
|
|
# calls to fill out additional information about each server
|
|
|
|
# use_hostnames changes the behavior from registering every host with its UUID
|
|
|
|
# and making a group of its hostname to only doing this if the
|
|
|
|
# hostname in question has more than one server
|
2016-02-29 05:21:42 +01:00
|
|
|
# fail_on_errors causes the inventory to fail and return no hosts if one cloud
|
|
|
|
# has failed (for example, bad credentials or being offline).
|
|
|
|
# When set to False, the inventory will return hosts from
|
|
|
|
# whichever other clouds it can contact. (Default: True)
|
2017-08-11 18:00:59 +02:00
|
|
|
#
|
|
|
|
# Also it is possible to pass the correct user by setting an ansible_user: $myuser
|
|
|
|
# metadata attribute.
|
2015-02-26 17:59:00 +01:00
|
|
|
|
2015-02-12 01:11:42 +01:00
|
|
|
import argparse
|
|
|
|
import collections
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import time
|
2016-02-29 05:21:42 +01:00
|
|
|
from distutils.version import StrictVersion
|
2015-02-12 01:11:42 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except:
|
|
|
|
import simplejson as json
|
|
|
|
|
2018-05-26 03:40:39 +02:00
|
|
|
import openstack as sdk
|
|
|
|
from openstack.cloud import inventory as sdk_inventory
|
|
|
|
from openstack.config import loader as cloud_config
|
2015-02-12 01:11:42 +01:00
|
|
|
|
2016-04-01 00:26:12 +02:00
|
|
|
CONFIG_FILES = ['/etc/ansible/openstack.yaml', '/etc/ansible/openstack.yml']
|
2015-02-12 01:11:42 +01:00
|
|
|
|
|
|
|
|
2015-12-03 16:04:24 +01:00
|
|
|
def get_groups_from_server(server_vars, namegroup=True):
|
2015-03-01 17:56:10 +01:00
|
|
|
groups = []
|
2015-02-12 01:11:42 +01:00
|
|
|
|
2015-03-01 17:56:10 +01:00
|
|
|
region = server_vars['region']
|
|
|
|
cloud = server_vars['cloud']
|
|
|
|
metadata = server_vars.get('metadata', {})
|
2015-02-12 01:11:42 +01:00
|
|
|
|
2015-03-01 17:56:10 +01:00
|
|
|
# Create a group for the cloud
|
|
|
|
groups.append(cloud)
|
2015-02-12 01:11:42 +01:00
|
|
|
|
2015-03-01 17:56:10 +01:00
|
|
|
# Create a group on region
|
|
|
|
groups.append(region)
|
2015-02-12 01:11:42 +01:00
|
|
|
|
2015-03-01 17:56:10 +01:00
|
|
|
# And one by cloud_region
|
|
|
|
groups.append("%s_%s" % (cloud, region))
|
2015-02-12 01:11:42 +01:00
|
|
|
|
2015-03-01 17:56:10 +01:00
|
|
|
# Check if group metadata key in servers' metadata
|
|
|
|
if 'group' in metadata:
|
|
|
|
groups.append(metadata['group'])
|
2015-02-12 01:11:42 +01:00
|
|
|
|
2015-03-01 17:56:10 +01:00
|
|
|
for extra_group in metadata.get('groups', '').split(','):
|
|
|
|
if extra_group:
|
2016-08-04 18:45:15 +02:00
|
|
|
groups.append(extra_group.strip())
|
2015-02-12 01:11:42 +01:00
|
|
|
|
2015-03-01 17:56:10 +01:00
|
|
|
groups.append('instance-%s' % server_vars['id'])
|
2015-12-03 16:04:24 +01:00
|
|
|
if namegroup:
|
|
|
|
groups.append(server_vars['name'])
|
2015-02-12 01:11:42 +01:00
|
|
|
|
2015-03-01 17:56:10 +01:00
|
|
|
for key in ('flavor', 'image'):
|
|
|
|
if 'name' in server_vars[key]:
|
|
|
|
groups.append('%s-%s' % (key, server_vars[key]['name']))
|
2015-02-12 01:11:42 +01:00
|
|
|
|
2015-03-01 17:56:10 +01:00
|
|
|
for key, value in iter(metadata.items()):
|
|
|
|
groups.append('meta-%s_%s' % (key, value))
|
2015-02-12 01:11:42 +01:00
|
|
|
|
2015-03-01 17:56:10 +01:00
|
|
|
az = server_vars.get('az', None)
|
|
|
|
if az:
|
|
|
|
# Make groups for az, region_az and cloud_region_az
|
|
|
|
groups.append(az)
|
|
|
|
groups.append('%s_%s' % (region, az))
|
|
|
|
groups.append('%s_%s_%s' % (cloud, region, az))
|
|
|
|
return groups
|
2015-02-12 01:11:42 +01:00
|
|
|
|
|
|
|
|
2017-03-07 10:10:40 +01:00
|
|
|
def get_host_groups(inventory, refresh=False, cloud=None):
|
|
|
|
(cache_file, cache_expiration_time) = get_cache_settings(cloud)
|
2015-12-03 16:07:13 +01:00
|
|
|
if is_cache_stale(cache_file, cache_expiration_time, refresh=refresh):
|
2015-03-01 17:56:10 +01:00
|
|
|
groups = to_json(get_host_groups_from_cloud(inventory))
|
2018-01-02 17:03:44 +01:00
|
|
|
with open(cache_file, 'w') as f:
|
|
|
|
f.write(groups)
|
2015-03-01 17:56:10 +01:00
|
|
|
else:
|
2018-01-02 17:03:44 +01:00
|
|
|
with open(cache_file, 'r') as f:
|
|
|
|
groups = f.read()
|
2015-03-01 17:56:10 +01:00
|
|
|
return groups
|
2015-02-12 01:11:42 +01:00
|
|
|
|
|
|
|
|
2016-02-24 19:36:50 +01:00
|
|
|
def append_hostvars(hostvars, groups, key, server, namegroup=False):
|
|
|
|
hostvars[key] = dict(
|
|
|
|
ansible_ssh_host=server['interface_ip'],
|
2017-04-03 22:02:54 +02:00
|
|
|
ansible_host=server['interface_ip'],
|
2016-02-24 19:36:50 +01:00
|
|
|
openstack=server)
|
2017-08-11 18:00:59 +02:00
|
|
|
|
|
|
|
metadata = server.get('metadata', {})
|
|
|
|
if 'ansible_user' in metadata:
|
|
|
|
hostvars[key]['ansible_user'] = metadata['ansible_user']
|
|
|
|
|
2016-02-24 19:36:50 +01:00
|
|
|
for group in get_groups_from_server(server, namegroup=namegroup):
|
|
|
|
groups[group].append(key)
|
|
|
|
|
|
|
|
|
2015-03-01 17:56:10 +01:00
|
|
|
def get_host_groups_from_cloud(inventory):
|
|
|
|
groups = collections.defaultdict(list)
|
2015-12-03 16:04:24 +01:00
|
|
|
firstpass = collections.defaultdict(list)
|
2015-03-01 17:56:10 +01:00
|
|
|
hostvars = {}
|
2015-12-03 16:04:24 +01:00
|
|
|
list_args = {}
|
|
|
|
if hasattr(inventory, 'extra_config'):
|
|
|
|
use_hostnames = inventory.extra_config['use_hostnames']
|
|
|
|
list_args['expand'] = inventory.extra_config['expand_hostvars']
|
2018-05-26 03:40:39 +02:00
|
|
|
if StrictVersion(sdk.version.__version__) >= StrictVersion("0.13.0"):
|
2016-02-29 05:21:42 +01:00
|
|
|
list_args['fail_on_cloud_config'] = \
|
|
|
|
inventory.extra_config['fail_on_errors']
|
2015-12-03 16:04:24 +01:00
|
|
|
else:
|
|
|
|
use_hostnames = False
|
|
|
|
|
|
|
|
for server in inventory.list_hosts(**list_args):
|
2015-02-12 01:11:42 +01:00
|
|
|
|
2015-03-01 17:56:10 +01:00
|
|
|
if 'interface_ip' not in server:
|
|
|
|
continue
|
2015-12-03 16:04:24 +01:00
|
|
|
firstpass[server['name']].append(server)
|
|
|
|
for name, servers in firstpass.items():
|
|
|
|
if len(servers) == 1 and use_hostnames:
|
2016-02-24 19:36:50 +01:00
|
|
|
append_hostvars(hostvars, groups, name, servers[0])
|
2015-12-03 16:04:24 +01:00
|
|
|
else:
|
2016-02-24 19:36:50 +01:00
|
|
|
server_ids = set()
|
|
|
|
# Trap for duplicate results
|
2015-12-03 16:04:24 +01:00
|
|
|
for server in servers:
|
2016-02-24 19:36:50 +01:00
|
|
|
server_ids.add(server['id'])
|
|
|
|
if len(server_ids) == 1 and use_hostnames:
|
|
|
|
append_hostvars(hostvars, groups, name, servers[0])
|
|
|
|
else:
|
|
|
|
for server in servers:
|
|
|
|
append_hostvars(
|
2016-03-16 00:42:06 +01:00
|
|
|
hostvars, groups, server['id'], server,
|
2016-02-24 19:36:50 +01:00
|
|
|
namegroup=True)
|
2015-03-01 17:56:10 +01:00
|
|
|
groups['_meta'] = {'hostvars': hostvars}
|
|
|
|
return groups
|
2015-02-12 01:11:42 +01:00
|
|
|
|
2015-03-01 17:56:10 +01:00
|
|
|
|
2015-12-03 16:07:13 +01:00
|
|
|
def is_cache_stale(cache_file, cache_expiration_time, refresh=False):
|
2015-03-01 17:56:10 +01:00
|
|
|
''' Determines if cache file has expired, or if it is still valid '''
|
2015-12-03 16:07:13 +01:00
|
|
|
if refresh:
|
|
|
|
return True
|
2016-03-09 18:54:30 +01:00
|
|
|
if os.path.isfile(cache_file) and os.path.getsize(cache_file) > 0:
|
2015-03-01 17:56:10 +01:00
|
|
|
mod_time = os.path.getmtime(cache_file)
|
|
|
|
current_time = time.time()
|
|
|
|
if (mod_time + cache_expiration_time) > current_time:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2017-03-07 10:10:40 +01:00
|
|
|
def get_cache_settings(cloud=None):
|
2018-05-30 21:11:53 +02:00
|
|
|
config_files = cloud_config.CONFIG_FILES + CONFIG_FILES
|
2018-06-18 22:41:15 +02:00
|
|
|
if cloud:
|
|
|
|
config = cloud_config.OpenStackConfig(
|
|
|
|
config_files=config_files).get_one(cloud=cloud)
|
|
|
|
else:
|
|
|
|
config = cloud_config.OpenStackConfig(
|
|
|
|
config_files=config_files).get_all()[0]
|
2015-03-01 17:56:10 +01:00
|
|
|
# For inventory-wide caching
|
|
|
|
cache_expiration_time = config.get_cache_expiration_time()
|
|
|
|
cache_path = config.get_cache_path()
|
2017-03-07 10:10:40 +01:00
|
|
|
if cloud:
|
|
|
|
cache_path = '{0}_{1}'.format(cache_path, cloud)
|
2015-03-01 17:56:10 +01:00
|
|
|
if not os.path.exists(cache_path):
|
|
|
|
os.makedirs(cache_path)
|
|
|
|
cache_file = os.path.join(cache_path, 'ansible-inventory.cache')
|
|
|
|
return (cache_file, cache_expiration_time)
|
|
|
|
|
|
|
|
|
|
|
|
def to_json(in_dict):
|
|
|
|
return json.dumps(in_dict, sort_keys=True, indent=2)
|
2015-02-12 01:11:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
def parse_args():
|
|
|
|
parser = argparse.ArgumentParser(description='OpenStack Inventory Module')
|
2017-03-07 10:10:40 +01:00
|
|
|
parser.add_argument('--cloud', default=os.environ.get('OS_CLOUD'),
|
|
|
|
help='Cloud name (default: None')
|
2015-02-12 01:11:42 +01:00
|
|
|
parser.add_argument('--private',
|
|
|
|
action='store_true',
|
|
|
|
help='Use private address for ansible host')
|
|
|
|
parser.add_argument('--refresh', action='store_true',
|
|
|
|
help='Refresh cached information')
|
2015-03-01 17:56:10 +01:00
|
|
|
parser.add_argument('--debug', action='store_true', default=False,
|
|
|
|
help='Enable debug output')
|
2015-02-12 01:11:42 +01:00
|
|
|
group = parser.add_mutually_exclusive_group(required=True)
|
|
|
|
group.add_argument('--list', action='store_true',
|
|
|
|
help='List active servers')
|
|
|
|
group.add_argument('--host', help='List details about the specific host')
|
2015-03-01 17:56:10 +01:00
|
|
|
|
2015-02-12 01:11:42 +01:00
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
args = parse_args()
|
|
|
|
try:
|
2018-05-26 03:40:39 +02:00
|
|
|
config_files = cloud_config.CONFIG_FILES + CONFIG_FILES
|
|
|
|
sdk.enable_logging(debug=args.debug)
|
2015-12-03 16:04:24 +01:00
|
|
|
inventory_args = dict(
|
2015-03-01 17:56:10 +01:00
|
|
|
refresh=args.refresh,
|
|
|
|
config_files=config_files,
|
|
|
|
private=args.private,
|
2017-03-07 10:10:40 +01:00
|
|
|
cloud=args.cloud,
|
2015-03-01 17:56:10 +01:00
|
|
|
)
|
2018-05-26 03:40:39 +02:00
|
|
|
if hasattr(sdk_inventory.OpenStackInventory, 'extra_config'):
|
2015-12-03 16:04:24 +01:00
|
|
|
inventory_args.update(dict(
|
|
|
|
config_key='ansible',
|
|
|
|
config_defaults={
|
|
|
|
'use_hostnames': False,
|
|
|
|
'expand_hostvars': True,
|
2016-02-29 05:21:42 +01:00
|
|
|
'fail_on_errors': True,
|
2015-12-03 16:04:24 +01:00
|
|
|
}
|
|
|
|
))
|
|
|
|
|
2018-05-26 03:40:39 +02:00
|
|
|
inventory = sdk_inventory.OpenStackInventory(**inventory_args)
|
2015-03-01 17:56:10 +01:00
|
|
|
|
2015-02-12 01:11:42 +01:00
|
|
|
if args.list:
|
2017-03-07 10:10:40 +01:00
|
|
|
output = get_host_groups(inventory, refresh=args.refresh, cloud=args.cloud)
|
2015-02-12 01:11:42 +01:00
|
|
|
elif args.host:
|
2015-03-01 17:56:10 +01:00
|
|
|
output = to_json(inventory.get_host(args.host))
|
|
|
|
print(output)
|
2018-05-26 03:40:39 +02:00
|
|
|
except sdk.exceptions.OpenStackCloudException as e:
|
2015-08-10 21:35:30 +02:00
|
|
|
sys.stderr.write('%s\n' % e.message)
|
2015-02-12 01:11:42 +01:00
|
|
|
sys.exit(1)
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|