-
-
Save aeciopires/11f6a5e3b5fad919ca0b46524b87e360 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