From e1b9a007b2abaf6d6ec85986c0262a2abb86a889 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Sat, 18 Jun 2022 03:15:19 +0200 Subject: Add some python scripts. --- import_yaml.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100755 import_yaml.py (limited to 'import_yaml.py') diff --git a/import_yaml.py b/import_yaml.py new file mode 100755 index 0000000..35558c8 --- /dev/null +++ b/import_yaml.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 + +"""Import extisting nodes.yaml into database""" + +import json +import yaml + +import pyenc +from pyenc.db import db +import pyenc.model as model + +app = pyenc.create_app() +app.app_context().push() + + +with open('/usr/local/puppet/nodes.yaml') as f: + data = yaml.full_load(f) + + +for fqdn, val in data.items(): + h = model.Host.query.where(model.Host.fqdn == fqdn).first() + if not h: + h = model.Host(fqdn=fqdn) + h.environment = val.get('environment') + print(h) + + classes = val['classes'] + if type(classes) == dict: + classes = classes.keys() + cls = model.PuppetClass.query \ + .where(model.PuppetClass.class_name.in_(classes)).all() + print(cls) + + h.classes.extend(cls) + + +db.session.commit() -- cgit v1.2.3