Skip to content

Commit

Permalink
Check superuser account state before creating it
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenneth Joss committed Apr 10, 2024
1 parent 3d163a8 commit e8442c9
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion ansible/roles/netbox/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,24 @@
not netbox__primary|bool)
changed_when: false

- name: Check if Django superuser account exists
# In case of an upgrade (with DB migrations), the superuser will
# be existent but the createsuperuser command will run anyway (and fails).
# So we check if it exist beforehand and only run the create command if needed.
environment:
DJANGO_SUPERUSER_PASSWORD: '{{ netbox__superuser_password }}'
django_manage:
command: shell -c 'import sys; from django.contrib.auth.models import User; print(User.objects.filter(username="{{ netbox__superuser_name }}").count())'
app_path: '{{ netbox__git_checkout + "/netbox" }}'
virtualenv: '{{ netbox__virtualenv }}'
become: True
become_user: '{{ netbox__user }}'
when: netbox__primary|bool
no_log: '{{ debops__no_log | d(True) }}'
register: netbox__check_superuser
ignore_errors: true
changed_when: false

- name: Create Django superuser account
environment:
DJANGO_SUPERUSER_PASSWORD: '{{ netbox__superuser_password }}'
Expand All @@ -279,7 +297,7 @@
become_user: '{{ netbox__user }}'
when: (netbox__primary|bool and
not netbox__register_installed.stat.exists|bool and
not netbox__register_migration.stdout is search('No migrations to apply.'))
netbox__check_superuser.out | trim == "0")
no_log: '{{ debops__no_log | d(True) }}'

- name: Generate systemd service unit
Expand Down

0 comments on commit e8442c9

Please sign in to comment.