From 191b934dbd36f46169d2ad843550dbbdc07e8d7d Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Mon, 15 Jan 2018 13:57:30 +0530 Subject: [PATCH] freeipa: support for host vars (#34535) Adds method to return host vars related to FreeIPA hostname. Signed-off-by: Abhijeet Kasurde --- contrib/inventory/freeipa.py | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/contrib/inventory/freeipa.py b/contrib/inventory/freeipa.py index 20f5626aed7..7efc05e3d83 100755 --- a/contrib/inventory/freeipa.py +++ b/contrib/inventory/freeipa.py @@ -3,8 +3,9 @@ # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) import argparse -from ipalib import api import json +from ipalib import api, errors +from six import u def initialize(): @@ -72,25 +73,29 @@ def parse_args(): return parser.parse_args() -def print_host(host): - ''' - This function is really a stub, it could return variables to be used in - a playbook. However, at this point there are no variables stored in - FreeIPA/IPA. - +def get_host_attributes(api, host): + """ This function expects one string, this hostname to lookup variables for. - ''' + Args: + api: FreeIPA API Object + host: Name of Hostname - print(json.dumps({})) - - return None + 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 {} if __name__ == '__main__': args = parse_args() + api = initialize() if args.host: - print_host(args.host) + print(get_host_attributes(api, args.host)) elif args.list: - api = initialize() list_groups(api)