Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add task for creating an API token for the superuser #128

Merged
merged 13 commits into from
May 22, 2021
1 change: 1 addition & 0 deletions .cache/roles/Musee Ullah.netbox
6 changes: 5 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,16 @@ netbox_git_uri: "https://github.com/netbox-community/netbox.git"
netbox_superuser_username: admin
#netbox_superuser_password: changeme
netbox_superuser_email: admin@localhost
superuser_enabled: false
florianow marked this conversation as resolved.
Show resolved Hide resolved
netbox_superuser_token: false
florianow marked this conversation as resolved.
Show resolved Hide resolved
----

It is *required* to set the superuser password. This role will create a new
superuser if the user does not exist, or will modify an existing user if they're
not a superuser/have a different email or password. (Yes, you can use this to
reset your superuser password if you forget it.)
reset your superuser password if you forget it.) it is possible to set a random
generate API-token for the superuser. You can create a superuser and configure LDAP.


[source,yaml]
----
Expand Down
2 changes: 2 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ netbox_git_uri: "https://github.com/netbox-community/netbox.git"

netbox_install_epel: true

netbox_superuser_enabled: false
netbox_superuser_username: admin
# netbox_superuser_password: changeme
netbox_superuser_email: admin@localhost
netbox_superuser_create_token: false

netbox_database: netbox
netbox_database_user: netbox
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
hosts: all
become: true
vars:
redis_version: 6.2.1
netbox_stable_version: 2.10.8
netbox_stable: true
netbox_socket: "0.0.0.0:80"
netbox_superuser_password: netbox
netbox_superuser_create_token: true
netbox_config:
ALLOWED_HOSTS:
- "{{ inventory_hostname }}"
Expand Down
11 changes: 10 additions & 1 deletion tasks/deploy_netbox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,16 @@
register: _netbox_superuser_result
changed_when: "'changed' in _netbox_superuser_result.stdout"
when:
- not netbox_ldap_enabled
- (not netbox_ldap_enabled) or (netbox_superuser_enabled)
lae marked this conversation as resolved.
Show resolved Hide resolved

- name: Create a super user token for NetBox
shell: "printf '{{ netbox_superuser_token }}' |\
{{ netbox_virtualenv_path }}/bin/python {{ netbox_current_path }}/netbox/manage.py shell"
register: _netbox_superuser_token
changed_when: "'changed' in _netbox_superuser_token.stdout"
when:
- netbox_superuser_create_token
- (not netbox_ldap_enabled) or (netbox_superuser_enabled)
florianow marked this conversation as resolved.
Show resolved Hide resolved

- name: Generate static assets for NetBox
django_manage:
Expand Down
2 changes: 2 additions & 0 deletions tests/group_vars/netbox
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ netbox_reports:
- src: reports/nothing.py
name: nothing
netbox_napalm_enabled: true
netbox_superuser_enabled: false
florianow marked this conversation as resolved.
Show resolved Hide resolved
netbox_superuser_password: netbox
netbox_superuser_token: true
florianow marked this conversation as resolved.
Show resolved Hide resolved
netbox_database: "netbox_{{ inventory_hostname_short }}"
netbox_database_host: 10.0.3.1
netbox_database_password:
Expand Down
10 changes: 10 additions & 0 deletions vars/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,13 @@ netbox_superuser_script: |
user.set_password(password)
user.save()
print("changed")

netbox_superuser_token: |
from users.models import Token
from django.contrib.auth.models import User
user = User.objects.get(username="{{ netbox_superuser_username }}")
query = Token.objects.filter(user=user)
if not query.exists():
token = Token.objects.create(user=user)
print("api-token: " + token.key)
print("changed")