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 3f3c201 commit e5dc5e5
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 @@ -220,6 +220,24 @@
register: netbox__register_migration
changed_when: netbox__register_migration.changed | bool

- 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
community.general.django_manage:
command: 'createsuperuser --noinput --username={{ netbox__superuser_name }} --email={{ netbox__superuser_email }}'
Expand All @@ -230,7 +248,7 @@
become: True
become_user: '{{ netbox__user }}'
when: (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 e5dc5e5

Please sign in to comment.