Skip to content

Instantly share code, notes, and snippets.

@trondhindenes
Created November 5, 2016 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trondhindenes/1be24f70b584900c514dd373ffb0d9ab to your computer and use it in GitHub Desktop.
Save trondhindenes/1be24f70b584900c514dd373ffb0d9ab to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os
import rethinkdb
import json
import rethinkdb as r
from os import listdir
from os.path import isfile, join
tempfolder = '/tmp/ansiblecmdb/'
onlyfiles = [f for f in listdir(tempfolder) if isfile(join(tempfolder, f))]
for file in onlyfiles:
print(file)
host_name = str(file).split(".")[0]
with open(os.path.join(tempfolder, file)) as data_file:
data = json.load(data_file)
r.connect("localhost", 28015).repl()
try:
r.db_create('ansible_facts').run()
except:
pass
try:
r.db("ansible_facts").table_create("ansible_facts").run()
except:
pass
#check if we have data
cursor = r.db("ansible_facts").table("ansible_facts").filter(r.row['inventory_hostname'] == host_name).run()
if cursor.items.__len__() != 0:
r.db("ansible_facts").table("ansible_facts").filter(r.row['inventory_hostname'] == host_name).delete().run()
r.db("ansible_facts").table("ansible_facts").insert(data).run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment