#!/usr/bin/python3 import os import json import argparse def parse_cli_args(): ''' Command line argument processing ''' parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter, ) parser.add_argument('--list', action='store_true', help='list hosts') parser.add_argument('--host', help='get host variables') args = parser.parse_args() return args def list_hosts(): host = os.getenv('ANSIBLE_HOST','127.0.0.1') port = os.getenv('ANSIBLE_PORT', '22') python_interpreter= os.getenv('ANSIBLE_PYTHON_INTERPRETER', '/usr/bin/python3') inventory = { 'all': { 'hosts': [host], }, '_meta': { 'hostvars': { host: { 'ansible_port': port, 'ansible_python_interpreter': python_interpreter, }, }, }, } # myhost = {'ansible_port': '5555', 'ansible_host': '192.0.2.50'} # inventory['hosts'].append({'myhost': myhost}) print(json.dumps(inventory)) def get_host_vars(host): print(json.dumps({})) def main(): args = parse_cli_args() if args.list: list_hosts() elif args.host: get_host_vars(args.host) # if __name__ == '__main__': # main() main()