Merge pull request #13544 from emonty/inventory-config

Add OpenStack Inventory configuration
This commit is contained in:
Brian Coca 2015-12-14 13:22:19 -05:00
commit 938c26d952
2 changed files with 56 additions and 14 deletions

View file

@ -32,6 +32,13 @@
# all of them and present them as one contiguous inventory. # all of them and present them as one contiguous inventory.
# #
# See the adjacent openstack.yml file for an example config file # See the adjacent openstack.yml file for an example config file
# 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
import argparse import argparse
import collections import collections
@ -51,7 +58,7 @@ import shade.inventory
CONFIG_FILES = ['/etc/ansible/openstack.yaml'] CONFIG_FILES = ['/etc/ansible/openstack.yaml']
def get_groups_from_server(server_vars): def get_groups_from_server(server_vars, namegroup=True):
groups = [] groups = []
region = server_vars['region'] region = server_vars['region']
@ -76,7 +83,8 @@ def get_groups_from_server(server_vars):
groups.append(extra_group) groups.append(extra_group)
groups.append('instance-%s' % server_vars['id']) groups.append('instance-%s' % server_vars['id'])
groups.append(server_vars['name']) if namegroup:
groups.append(server_vars['name'])
for key in ('flavor', 'image'): for key in ('flavor', 'image'):
if 'name' in server_vars[key]: if 'name' in server_vars[key]:
@ -94,9 +102,9 @@ def get_groups_from_server(server_vars):
return groups return groups
def get_host_groups(inventory): def get_host_groups(inventory, refresh=False):
(cache_file, cache_expiration_time) = get_cache_settings() (cache_file, cache_expiration_time) = get_cache_settings()
if is_cache_stale(cache_file, cache_expiration_time): if is_cache_stale(cache_file, cache_expiration_time, refresh=refresh):
groups = to_json(get_host_groups_from_cloud(inventory)) groups = to_json(get_host_groups_from_cloud(inventory))
open(cache_file, 'w').write(groups) open(cache_file, 'w').write(groups)
else: else:
@ -106,23 +114,44 @@ def get_host_groups(inventory):
def get_host_groups_from_cloud(inventory): def get_host_groups_from_cloud(inventory):
groups = collections.defaultdict(list) groups = collections.defaultdict(list)
firstpass = collections.defaultdict(list)
hostvars = {} hostvars = {}
for server in inventory.list_hosts(): list_args = {}
if hasattr(inventory, 'extra_config'):
use_hostnames = inventory.extra_config['use_hostnames']
list_args['expand'] = inventory.extra_config['expand_hostvars']
else:
use_hostnames = False
for server in inventory.list_hosts(**list_args):
if 'interface_ip' not in server: if 'interface_ip' not in server:
continue continue
for group in get_groups_from_server(server): firstpass[server['name']].append(server)
groups[group].append(server['id']) for name, servers in firstpass.items():
hostvars[server['id']] = dict( if len(servers) == 1 and use_hostnames:
ansible_ssh_host=server['interface_ip'], server = servers[0]
openstack=server, hostvars[name] = dict(
) ansible_ssh_host=server['interface_ip'],
openstack=server)
for group in get_groups_from_server(server, namegroup=False):
groups[group].append(server['name'])
else:
for server in servers:
server_id = server['id']
hostvars[server_id] = dict(
ansible_ssh_host=server['interface_ip'],
openstack=server)
for group in get_groups_from_server(server, namegroup=True):
groups[group].append(server_id)
groups['_meta'] = {'hostvars': hostvars} groups['_meta'] = {'hostvars': hostvars}
return groups return groups
def is_cache_stale(cache_file, cache_expiration_time): def is_cache_stale(cache_file, cache_expiration_time, refresh=False):
''' Determines if cache file has expired, or if it is still valid ''' ''' Determines if cache file has expired, or if it is still valid '''
if refresh:
return True
if os.path.isfile(cache_file): if os.path.isfile(cache_file):
mod_time = os.path.getmtime(cache_file) mod_time = os.path.getmtime(cache_file)
current_time = time.time() current_time = time.time()
@ -169,14 +198,24 @@ def main():
try: try:
config_files = os_client_config.config.CONFIG_FILES + CONFIG_FILES config_files = os_client_config.config.CONFIG_FILES + CONFIG_FILES
shade.simple_logging(debug=args.debug) shade.simple_logging(debug=args.debug)
inventory = shade.inventory.OpenStackInventory( inventory_args = dict(
refresh=args.refresh, refresh=args.refresh,
config_files=config_files, config_files=config_files,
private=args.private, private=args.private,
) )
if hasattr(shade.inventory.OpenStackInventory, 'extra_config'):
inventory_args.update(dict(
config_key='ansible',
config_defaults={
'use_hostnames': False,
'expand_hostvars': True,
}
))
inventory = shade.inventory.OpenStackInventory(**inventory_args)
if args.list: if args.list:
output = get_host_groups(inventory) output = get_host_groups(inventory, refresh=args.refresh)
elif args.host: elif args.host:
output = to_json(inventory.get_host(args.host)) output = to_json(inventory.get_host(args.host))
print(output) print(output)

View file

@ -26,3 +26,6 @@ clouds:
username: stack username: stack
password: stack password: stack
project_name: stack project_name: stack
ansible:
use_hostnames: True
expand_hostvars: False