2015-01-07 00:29:19 +00:00
|
|
|
#!/usr/bin/env python
|
2017-08-29 15:38:33 +00:00
|
|
|
# Copyright (c) 2017 Ansible Project
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
2014-10-11 01:26:17 +00:00
|
|
|
|
2015-01-20 22:45:41 +00:00
|
|
|
import argparse
|
FreeIPA inventory (ipalib 4.6.2): avoid exception (#34356)
Workaround for https://pagure.io/freeipa/issue/7345.
mentions:
- IPA_CONFDIR environment variable when directory doesn't exist
- domain, xmlrpc_uri or jsonrpc_uri parameters when one is missing
Exception was:
$ IPA_CONFDIR=/path/to/empty/dir ./ansible/contrib/inventory/freeipa.py --list
Traceback (most recent call last):
File "./ansible/contrib/inventory/freeipa.py", line 95, in <module>
api = initialize()
File "./ansible/contrib/inventory/freeipa.py", line 18, in initialize
api.finalize()
File "local/lib/python2.7/site-packages/ipalib/plugable.py", line 738, in finalize
self.__do_if_not_done('load_plugins')
File "local/lib/python2.7/site-packages/ipalib/plugable.py", line 425, in __do_if_not_done
getattr(self, name)()
File "local/lib/python2.7/site-packages/ipalib/plugable.py", line 618, in load_plugins
for package in self.packages:
File "local/lib/python2.7/site-packages/ipalib/__init__.py", line 949, in packages
ipaclient.remote_plugins.get_package(self),
File "local/lib/python2.7/site-packages/ipaclient/remote_plugins/__init__.py", line 120, in get_package
server_info = ServerInfo(api)
File "local/lib/python2.7/site-packages/ipaclient/remote_plugins/__init__.py", line 26, in __init__
hostname = DNSName(api.env.server).ToASCII()
AttributeError: 'Env' object has no attribute 'server'
2018-02-26 10:07:51 +00:00
|
|
|
from distutils.version import LooseVersion
|
2015-01-20 22:45:41 +00:00
|
|
|
import json
|
FreeIPA inventory (ipalib 4.6.2): avoid exception (#34356)
Workaround for https://pagure.io/freeipa/issue/7345.
mentions:
- IPA_CONFDIR environment variable when directory doesn't exist
- domain, xmlrpc_uri or jsonrpc_uri parameters when one is missing
Exception was:
$ IPA_CONFDIR=/path/to/empty/dir ./ansible/contrib/inventory/freeipa.py --list
Traceback (most recent call last):
File "./ansible/contrib/inventory/freeipa.py", line 95, in <module>
api = initialize()
File "./ansible/contrib/inventory/freeipa.py", line 18, in initialize
api.finalize()
File "local/lib/python2.7/site-packages/ipalib/plugable.py", line 738, in finalize
self.__do_if_not_done('load_plugins')
File "local/lib/python2.7/site-packages/ipalib/plugable.py", line 425, in __do_if_not_done
getattr(self, name)()
File "local/lib/python2.7/site-packages/ipalib/plugable.py", line 618, in load_plugins
for package in self.packages:
File "local/lib/python2.7/site-packages/ipalib/__init__.py", line 949, in packages
ipaclient.remote_plugins.get_package(self),
File "local/lib/python2.7/site-packages/ipaclient/remote_plugins/__init__.py", line 120, in get_package
server_info = ServerInfo(api)
File "local/lib/python2.7/site-packages/ipaclient/remote_plugins/__init__.py", line 26, in __init__
hostname = DNSName(api.env.server).ToASCII()
AttributeError: 'Env' object has no attribute 'server'
2018-02-26 10:07:51 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from ipalib import api, errors, __version__ as IPA_VERSION
|
2018-01-15 08:27:30 +00:00
|
|
|
from six import u
|
2015-01-20 22:45:41 +00:00
|
|
|
|
2017-05-30 20:08:09 +00:00
|
|
|
|
2015-01-20 22:45:41 +00:00
|
|
|
def initialize():
|
|
|
|
'''
|
|
|
|
This function initializes the FreeIPA/IPA API. This function requires
|
2017-01-27 23:20:31 +00:00
|
|
|
no arguments. A kerberos key must be present in the users keyring in
|
FreeIPA inventory (ipalib 4.6.2): avoid exception (#34356)
Workaround for https://pagure.io/freeipa/issue/7345.
mentions:
- IPA_CONFDIR environment variable when directory doesn't exist
- domain, xmlrpc_uri or jsonrpc_uri parameters when one is missing
Exception was:
$ IPA_CONFDIR=/path/to/empty/dir ./ansible/contrib/inventory/freeipa.py --list
Traceback (most recent call last):
File "./ansible/contrib/inventory/freeipa.py", line 95, in <module>
api = initialize()
File "./ansible/contrib/inventory/freeipa.py", line 18, in initialize
api.finalize()
File "local/lib/python2.7/site-packages/ipalib/plugable.py", line 738, in finalize
self.__do_if_not_done('load_plugins')
File "local/lib/python2.7/site-packages/ipalib/plugable.py", line 425, in __do_if_not_done
getattr(self, name)()
File "local/lib/python2.7/site-packages/ipalib/plugable.py", line 618, in load_plugins
for package in self.packages:
File "local/lib/python2.7/site-packages/ipalib/__init__.py", line 949, in packages
ipaclient.remote_plugins.get_package(self),
File "local/lib/python2.7/site-packages/ipaclient/remote_plugins/__init__.py", line 120, in get_package
server_info = ServerInfo(api)
File "local/lib/python2.7/site-packages/ipaclient/remote_plugins/__init__.py", line 26, in __init__
hostname = DNSName(api.env.server).ToASCII()
AttributeError: 'Env' object has no attribute 'server'
2018-02-26 10:07:51 +00:00
|
|
|
order for this to work. IPA default configuration directory is /etc/ipa,
|
|
|
|
this path could be overridden with IPA_CONFDIR environment variable.
|
2015-01-20 22:45:41 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
api.bootstrap(context='cli')
|
FreeIPA inventory (ipalib 4.6.2): avoid exception (#34356)
Workaround for https://pagure.io/freeipa/issue/7345.
mentions:
- IPA_CONFDIR environment variable when directory doesn't exist
- domain, xmlrpc_uri or jsonrpc_uri parameters when one is missing
Exception was:
$ IPA_CONFDIR=/path/to/empty/dir ./ansible/contrib/inventory/freeipa.py --list
Traceback (most recent call last):
File "./ansible/contrib/inventory/freeipa.py", line 95, in <module>
api = initialize()
File "./ansible/contrib/inventory/freeipa.py", line 18, in initialize
api.finalize()
File "local/lib/python2.7/site-packages/ipalib/plugable.py", line 738, in finalize
self.__do_if_not_done('load_plugins')
File "local/lib/python2.7/site-packages/ipalib/plugable.py", line 425, in __do_if_not_done
getattr(self, name)()
File "local/lib/python2.7/site-packages/ipalib/plugable.py", line 618, in load_plugins
for package in self.packages:
File "local/lib/python2.7/site-packages/ipalib/__init__.py", line 949, in packages
ipaclient.remote_plugins.get_package(self),
File "local/lib/python2.7/site-packages/ipaclient/remote_plugins/__init__.py", line 120, in get_package
server_info = ServerInfo(api)
File "local/lib/python2.7/site-packages/ipaclient/remote_plugins/__init__.py", line 26, in __init__
hostname = DNSName(api.env.server).ToASCII()
AttributeError: 'Env' object has no attribute 'server'
2018-02-26 10:07:51 +00:00
|
|
|
|
|
|
|
if not os.path.isdir(api.env.confdir):
|
|
|
|
print("WARNING: IPA configuration directory (%s) is missing. "
|
|
|
|
"Environment variable IPA_CONFDIR could be used to override "
|
|
|
|
"default path." % api.env.confdir)
|
|
|
|
|
|
|
|
if LooseVersion(IPA_VERSION) >= LooseVersion('4.6.2'):
|
|
|
|
# With ipalib < 4.6.0 'server' and 'domain' have default values
|
|
|
|
# ('localhost:8888', 'example.com'), newer versions don't and
|
|
|
|
# DNS autodiscovery is broken, then one of jsonrpc_uri / xmlrpc_uri is
|
|
|
|
# required.
|
|
|
|
# ipalib 4.6.0 is unusable (https://pagure.io/freeipa/issue/7132)
|
|
|
|
# that's why 4.6.2 is explicitely tested.
|
|
|
|
if 'server' not in api.env or 'domain' not in api.env:
|
|
|
|
sys.exit("ERROR: ('jsonrpc_uri' or 'xmlrpc_uri') or 'domain' are not "
|
|
|
|
"defined in '[global]' section of '%s' nor in '%s'." %
|
|
|
|
(api.env.conf, api.env.conf_default))
|
|
|
|
|
2015-01-20 22:45:41 +00:00
|
|
|
api.finalize()
|
2015-04-09 02:33:38 +00:00
|
|
|
try:
|
|
|
|
api.Backend.rpcclient.connect()
|
|
|
|
except AttributeError:
|
2017-05-30 20:08:09 +00:00
|
|
|
# FreeIPA < 4.0 compatibility
|
2015-04-09 02:33:38 +00:00
|
|
|
api.Backend.xmlclient.connect()
|
2017-01-27 23:45:23 +00:00
|
|
|
|
2015-01-20 22:45:41 +00:00
|
|
|
return api
|
|
|
|
|
2017-05-30 20:08:09 +00:00
|
|
|
|
2015-01-20 22:45:41 +00:00
|
|
|
def list_groups(api):
|
|
|
|
'''
|
2016-11-18 13:41:38 +00:00
|
|
|
This function prints a list of all host groups. This function requires
|
2015-01-20 22:45:41 +00:00
|
|
|
one argument, the FreeIPA/IPA API object.
|
|
|
|
'''
|
|
|
|
|
|
|
|
inventory = {}
|
2017-05-30 20:08:09 +00:00
|
|
|
hostvars = {}
|
2015-01-20 22:45:41 +00:00
|
|
|
|
2017-12-18 17:28:19 +00:00
|
|
|
result = api.Command.hostgroup_find(all=True)['result']
|
2015-01-20 22:45:41 +00:00
|
|
|
|
|
|
|
for hostgroup in result:
|
2016-11-18 13:41:38 +00:00
|
|
|
# Get direct and indirect members (nested hostgroups) of hostgroup
|
|
|
|
members = []
|
2017-08-18 19:46:43 +00:00
|
|
|
|
2016-11-18 13:41:38 +00:00
|
|
|
if 'member_host' in hostgroup:
|
|
|
|
members = [host for host in hostgroup['member_host']]
|
|
|
|
if 'memberindirect_host' in hostgroup:
|
2017-01-27 23:45:23 +00:00
|
|
|
members += (host for host in hostgroup['memberindirect_host'])
|
2016-11-18 13:41:38 +00:00
|
|
|
inventory[hostgroup['cn'][0]] = {'hosts': [host for host in members]}
|
|
|
|
|
|
|
|
for member in members:
|
|
|
|
hostvars[member] = {}
|
2015-01-20 22:45:41 +00:00
|
|
|
|
|
|
|
inventory['_meta'] = {'hostvars': hostvars}
|
|
|
|
inv_string = json.dumps(inventory, indent=1, sort_keys=True)
|
2015-08-28 06:18:13 +00:00
|
|
|
print(inv_string)
|
2017-01-27 23:45:23 +00:00
|
|
|
|
2015-01-20 22:45:41 +00:00
|
|
|
return None
|
|
|
|
|
2017-05-30 20:08:09 +00:00
|
|
|
|
2015-01-20 22:45:41 +00:00
|
|
|
def parse_args():
|
|
|
|
'''
|
|
|
|
This function parses the arguments that were passed in via the command line.
|
|
|
|
This function expects no arguments.
|
|
|
|
'''
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='Ansible FreeIPA/IPA '
|
|
|
|
'inventory module')
|
|
|
|
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 specified host')
|
|
|
|
|
|
|
|
return parser.parse_args()
|
|
|
|
|
2017-05-30 20:08:09 +00:00
|
|
|
|
2018-01-15 08:27:30 +00:00
|
|
|
def get_host_attributes(api, host):
|
|
|
|
"""
|
2015-01-20 22:45:41 +00:00
|
|
|
This function expects one string, this hostname to lookup variables for.
|
2018-01-15 08:27:30 +00:00
|
|
|
Args:
|
|
|
|
api: FreeIPA API Object
|
|
|
|
host: Name of Hostname
|
2015-01-20 22:45:41 +00:00
|
|
|
|
2018-01-15 08:27:30 +00:00
|
|
|
Returns: Dict of Host vars if found else None
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
result = api.Command.host_show(u(host))['result']
|
|
|
|
if 'usercertificate' in result:
|
|
|
|
del result['usercertificate']
|
|
|
|
return json.dumps(result, indent=1)
|
|
|
|
except errors.NotFound as e:
|
|
|
|
return {}
|
2015-01-20 22:45:41 +00:00
|
|
|
|
2017-05-30 20:08:09 +00:00
|
|
|
|
2015-01-20 22:45:41 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
args = parse_args()
|
2018-01-15 08:27:30 +00:00
|
|
|
api = initialize()
|
2014-10-11 01:26:17 +00:00
|
|
|
|
2015-01-20 22:45:41 +00:00
|
|
|
if args.host:
|
2018-01-15 08:27:30 +00:00
|
|
|
print(get_host_attributes(api, args.host))
|
2015-01-20 22:45:41 +00:00
|
|
|
elif args.list:
|
|
|
|
list_groups(api)
|