Skip to content

Instantly share code, notes, and snippets.

@metajiji
Created August 24, 2019 14:19
Show Gist options
  • Save metajiji/f69817b6d6411f0cda315ed053b2d8fe to your computer and use it in GitHub Desktop.
Save metajiji/f69817b6d6411f0cda315ed053b2d8fe to your computer and use it in GitHub Desktop.
Powerdns database to ansible vars
import sqlite3
import yaml
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
def zones_to_yml(cursor):
zones = {'pdns_records_powerdns_zones': []}
cursor.execute('SELECT name, type FROM domains;')
for r in cursor.fetchall():
d = {
'name': r['name'],
'kind': r['type'].lower(),
'nameservers': [
'ns1.%s' % r['name'],
'ns2.%s' % r['name'],
]
}
zones['pdns_records_powerdns_zones'].append(d)
with open('zones.yml', 'w') as fd:
yaml.dump(zones, fd, width=float('inf'))
def records_to_yml(cursor):
records = {'pdns_records_powerdns_records': []}
cursor.execute('''
SELECT
domains.name AS zone,
records.name AS name,
records.type AS type,
records.content AS content,
records.ttl AS ttl
FROM records,domains
WHERE domains.id = records.domain_id;''')
for r in cursor.fetchall():
records['pdns_records_powerdns_records'].append(r)
with open('records.yml', 'w') as fd:
yaml.dump(records, fd, width=float('inf'))
if __name__ == '__main__':
conn = sqlite3.connect('db.sqlite')
conn.row_factory = dict_factory
cur = conn.cursor()
zones_to_yml(cur)
records_to_yml(cur)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment