homelab-nomad/nomad_vars.py

52 lines
1.3 KiB
Python
Raw Permalink Normal View History

#! /usr/bin/env python3
import sys
import yaml
from nomad import Nomad
2023-03-24 16:57:37 +00:00
def write_nomad():
nomad = Nomad()
with open("./ansible_playbooks/vars/nomad_vars.yml") as f:
vars = yaml.load(f, yaml.CLoader)
for path, items in vars.items():
# ignore special path there for terraform
if path == "nomad/oidc":
continue
print("path", path, "items", items)
2023-03-24 16:57:37 +00:00
if items == "DELETE":
print(nomad.variable.delete_variable(path))
2023-03-24 16:57:37 +00:00
else:
print(
nomad.variable.create_variable(
path,
{"Path": path, "Items": {k: str(v) for k, v in items.items()}},
)
)
def print_sample():
clean_vars = {}
with open("./ansible_playbooks/vars/nomad_vars.yml") as f:
vars = yaml.load(f, yaml.CLoader)
for path, items in vars.items():
if items == "DELETE":
continue
else:
clean_vars[path] = {k: "VALUE" for k in items}
print(yaml.dump(clean_vars))
def main():
if len(sys.argv) > 1 and sys.argv[1] == "print":
print_sample()
else:
write_nomad()
if __name__ == "__main__":
main()