We have been using yaml for puppet config for quite a while now.
With configs getting bigger and more complex, we decided to move all the puppet hiera config to be stored in a db rather than yaml files.
An issue emerged is that the yaml config is too complex to be escaped by standard shell script/awk tools, it can be done but would be too complex and might not be fool proof.
import yaml
import sys
import psycopg2
try:
conn = psycopg2.connect("dbname='hiera' user='hiera' host='localhost' password='hiera'")
except:
print "I am unable to connect to the database"
cur = conn.cursor()
with open(sys.argv[1] , 'r') as stream:
try:
doc=yaml.load(stream)
for yamlkey, yamlvalue in doc.iteritems():
if str(yamlvalue).find("[")<>-1 :
txt=str(yamlvalue).replace("\"","\\\"").replace("'","\"")
else:
txt="\""+str(yamlvalue)+"\""
cur.execute("""INSERT INTO keyval(key,val) VALUES ( %s , %s )""", (yamlkey,txt ) )
conn.commit()
except yaml.YAMLError as exc:
print(exc)
With configs getting bigger and more complex, we decided to move all the puppet hiera config to be stored in a db rather than yaml files.
An issue emerged is that the yaml config is too complex to be escaped by standard shell script/awk tools, it can be done but would be too complex and might not be fool proof.
Instead, I used python to do the job.
python should be able to parse
the yaml file and actually insert the data for us into a table.
Here is a script below that does
the key and value inserts:
[root@Vardamir ~]# cat
pyparser.py
import yaml
import sys
import psycopg2
try:
conn = psycopg2.connect("dbname='hiera' user='hiera' host='localhost' password='hiera'")
except:
print "I am unable to connect to the database"
cur = conn.cursor()
with open(sys.argv[1] , 'r') as stream:
try:
doc=yaml.load(stream)
for yamlkey, yamlvalue in doc.iteritems():
if str(yamlvalue).find("[")<>-1 :
txt=str(yamlvalue).replace("\"","\\\"").replace("'","\"")
else:
txt="\""+str(yamlvalue)+"\""
cur.execute("""INSERT INTO keyval(key,val) VALUES ( %s , %s )""", (yamlkey,txt ) )
conn.commit()
except yaml.YAMLError as exc:
print(exc)
[root@Vardamir ~]#
To run this just; pass the yaml
file as a parameter:
#python pyparser.py act.yaml
For this to work we need to
install some packages for python:
yum install PyYAML.x86_64
yum install python-psycopg2
No comments:
Post a Comment