Last active
November 18, 2021 10:46
-
-
Save g1soori/0dcc654d787792c298376443d97eedfe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
""" | |
This is template for ansible dynamic inventory script. | |
""" | |
import json | |
import argparse | |
host = "host_ip" | |
def get_inventory_data(): | |
return { | |
"web": { | |
"hosts": ["web"], | |
"vars": { | |
"ansible_python_interpreter": "/opt/tools/bin/python", | |
"ansible_ssh_host": host | |
} | |
} | |
} | |
def read_cli_args(): | |
global args | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--list', action='store_true') | |
parser.add_argument('--host', action='store') | |
args = parser.parse_args() | |
# Default main function | |
if __name__ == "__main__": | |
global args | |
read_cli_args() | |
inventory_data = get_inventory_data() | |
if args and args.list: | |
print(json.dumps(inventory_data)) | |
elif args.host: | |
print(json.dumps({'_meta': {'hostvars': {}}})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment