Created
July 3, 2024 07:12
-
-
Save jimi008/06207c1976a2519d2112c2001a8ee5f9 to your computer and use it in GitHub Desktop.
Ansible Custom Dynamic Inventory in Python
This file contains hidden or 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
#!/usr/bin/python | |
import json | |
import argparse | |
def get_inventory_data(): | |
data = { | |
"database": { | |
"hosts": ["sqlserver1"], | |
"vars" : { | |
"ansible_ssh_pass": "password", | |
"ansible_ssh_host": "192.168.25.15" | |
} | |
} | |
} | |
return data | |
if __name__ == "__main__": | |
data = get_inventory_data() | |
#print(json.dumps(data)) | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--list', action='store_true') | |
parser.add_argument('--host', action='store') | |
arg = parser.parse_args() | |
if arg and arg.list: | |
print(json.dumps(data)) | |
elif arg.host: | |
print(json.dumps({'_meta': {'hostvars': {}}})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment