netbox-docker/startup_scripts/010_groups.py

26 lines
645 B
Python
Raw Normal View History

import sys
2021-04-20 23:47:49 +02:00
from startup_script_utils import load_yaml
from users.models import AdminGroup, AdminUser
groups = load_yaml("/opt/netbox/initializers/groups.yml")
if groups is None:
sys.exit()
2021-04-20 23:47:49 +02:00
for params in groups:
2021-04-21 00:05:47 +02:00
groupname = params["name"]
2021-04-20 23:47:49 +02:00
2021-04-21 00:05:47 +02:00
group, created = AdminGroup.objects.get_or_create(name=groupname)
if created:
print("👥 Created group", groupname)
2021-04-20 23:47:49 +02:00
for username in params.get("users", []):
user = AdminUser.objects.get(username=username)
if user:
2021-04-20 23:47:49 +02:00
group.user_set.add(user)
print(" 👤 Assigned user %s to group %s" % (username, AdminGroup.name))
2021-04-20 23:47:49 +02:00
group.save()