Created
November 5, 2016 13:51
-
-
Save trondhindenes/1be24f70b584900c514dd373ffb0d9ab to your computer and use it in GitHub Desktop.
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 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