Skip to content

Commit 3fbc496

Browse files
committed
feat: Add NetBox superuser creation task
This task is not idempotent, therefore is can run only once after the database creation task. Flushing the handler during play execution will not execute any handler after the remaining tasks. Therefore the flush needs to be called after all deployment tasks have been executed. The flush is only needed for the createsuperuser task, because the database must be already configured. Hence, the createsuperuser task needs to be at the end.
1 parent c01ece7 commit 3fbc496

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

roles/nb_install/defaults/main.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ nb_install_netbox_secret_key: "{{ lookup('ansible.builtin.password',
1919
'/dev/null', chars=['ascii_letters', 'digits', '!@#$%^&*(-_=+)'],
2020
length=50, seed=inventory_hostname) }}"
2121

22+
# NetBox (Django) superuser creation enabled
23+
nb_install_netbox_superuser_creation_enabled: true
24+
25+
# NetBox superuser username
26+
nb_install_netbox_superuser_username: admin
27+
28+
# NetBox superuser user email address
29+
nb_install_netbox_superuser_email: "[email protected]"
30+
31+
# NetBox superuser password
32+
# nb_install_netbox_superuser_password: "ADD A SUPERUSER PASSWORD"
33+
2234
# NetBox system user
2335
nb_install_netbox_system_user: netbox
2436

roles/nb_install/tasks/database.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
name: "{{ nb_install_database_name }}"
3333
owner: "{{ nb_install_database_user_name }}"
3434
state: present
35+
register: __nb_install_database_create_result
3536
notify: netbox-upgrade-script
3637

3738
- name: database | Verify database user can connect to database

roles/nb_install/tasks/main.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
---
22
# main tasks file for Ansible role pheus.netbox.nb_install
33

4+
- name: Ensure superuser (admin) password is provided
5+
ansible.builtin.assert:
6+
that:
7+
- nb_install_netbox_superuser_password is defined
8+
- nb_install_netbox_superuser_password | length > 0
9+
fail_msg: |
10+
The initial superuser (admin) account for NetBox requires a password.
11+
12+
Please set an password via "nb_isntall_netbox_superuser_password"
13+
or disable the creation of the superuser via
14+
"nb_install_netbox_superuser_creation_enabled".
15+
when: nb_install_netbox_superuser_creation_enabled | bool
16+
417
- name: Include PostgreSQL database installation and preparation tasks
518
ansible.builtin.include_tasks:
619
file: database.yml
@@ -13,4 +26,9 @@
1326
ansible.builtin.include_tasks:
1427
file: netbox.yml
1528

29+
- name: Include NetBox superuser installation tasks
30+
ansible.builtin.include_tasks:
31+
file: superuser.yml
32+
when: nb_install_netbox_superuser_creation_enabled | bool
33+
1634
...

roles/nb_install/tasks/superuser.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
# tasks file for NetBox superuser creation
3+
4+
# Flush the Ansible handlers to enforce the execution of the upgrade script
5+
# before the creation of the superuser.
6+
- name: superuser | Ensure handlers are executed
7+
ansible.builtin.meta:
8+
flush_handlers
9+
10+
- name: superuser | Create an initial NetBox admin (superuser)
11+
become: true
12+
community.general.django_manage:
13+
command: >-
14+
createsuperuser --noinput
15+
--username={{ nb_install_netbox_superuser_username }}
16+
--email={{ nb_install_netbox_superuser_email }}
17+
project_path: "{{ nb_install_netbox_install_path }}/netbox"
18+
virtualenv: "{{ nb_install_netbox_install_path }}/venv"
19+
environment:
20+
DJANGO_SUPERUSER_PASSWORD: "{{ nb_install_netbox_superuser_password }}"
21+
when: >
22+
nb_install_netbox_superuser_creation_enabled | bool
23+
and __nb_install_database_create_result is defined
24+
and __nb_install_database_create_result.changed | bool
25+
and 'CREATE DATABASE' in
26+
__nb_install_database_create_result.executed_commands[0]
27+
28+
...

0 commit comments

Comments
 (0)