Created
August 17, 2017 21:16
-
-
Save Faheetah/1f42f05e8e65f46e48dc19c9f35eed37 to your computer and use it in GitHub Desktop.
Ansible RethinkDB dynamic inventory module
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
#!/usr/bin/env python | |
import json | |
import rethinkdb as r | |
from os import environ as env | |
HOST = env.get('RETHINK_HOST', '127.0.0.1') | |
PORT = env.get('RETHINK_PORT', 28015) | |
DB = env.get('RETHINK_DB', 'inventory') | |
GROUP = env.get('RETHINK_GROUP', 'dev') | |
r.connect(HOST, PORT, DB).repl() | |
groups = {'_meta': {'hostvars': {}}} | |
def get_groups(group): | |
group = r.table('groups').get(group).run() | |
if not groups.get(group['id']): | |
groups[group['id']] = group | |
for g in group.get('children', []): | |
get_groups(g) | |
if 'hosts' in group: | |
hosts = r.table('hosts').get_all(*group['hosts']).run() | |
for host in hosts: | |
if 'vars' in host: | |
groups['_meta']['hostvars'][host['id']] = host['vars'] | |
# Would probably want to implement --list and stuff here, | |
# but since _meta is included Ansible should never call --host | |
get_groups(GROUP) | |
print(json.dumps(groups)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment