Skip to content

Instantly share code, notes, and snippets.

@Faheetah
Created August 17, 2017 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Faheetah/1f42f05e8e65f46e48dc19c9f35eed37 to your computer and use it in GitHub Desktop.
Save Faheetah/1f42f05e8e65f46e48dc19c9f35eed37 to your computer and use it in GitHub Desktop.
Ansible RethinkDB dynamic inventory module
#!/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