Skip to content

Commit 2dbf619

Browse files
committed
Support a more flexible configuration file. (#15)
Merge branch 'feature/flexible-config' into master
2 parents 08566e4 + ec8415f commit 2dbf619

File tree

8 files changed

+81
-34
lines changed

8 files changed

+81
-34
lines changed

README.md

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
lae.netbox
55
=========
66

7-
Installs and configures DigitalOcean's [NetBox](https://github.com/digitalocean/netbox).
7+
Installs and configures DigitalOcean's [NetBox]().
88

99
This role deploys NetBox inside of a virtualenv, uses uWSGI as its frontend
1010
(can be used standalone or behind a load balancer), works with both Python 2/3
@@ -38,11 +38,15 @@ order to use Python 2.
3838

3939
See the *Example Playbook* section for more information on configuring the DB.
4040

41-
`netbox_secret_key`, if left undefined, will be generated by this role and (by
42-
default) stored in `/srv/netbox/shared/generated_secret_key`. This will then be
43-
used by the role to configure NetBox unless `netbox_secret_key` is later defined.
41+
`netbox_config` should contain a dictionary of of settings to configure
42+
NetBox with. See [Mandatory Settings]() and [Optional Settings]() from the
43+
NetBox documentation for more information, as well as `examples/netbox_config.yml`
44+
in this repository.
4445

45-
`netbox_allowed_hosts` is a list defining `ALLOWED_HOSTS`.
46+
If `netbox_config.SECRET_KEY` is left undefined, this role will automatically
47+
generate one for you and store it in `/srv/netbox/shared/generated_secret_key`
48+
(by default). The SECRET_KEY will be read from this file on subsequent runs,
49+
unless you override it by defining `netbox_config.SECRET_KEY`.
4650

4751
To load the initial data shipped by NetBox, set `netbox_load_initial_data` to
4852
true. Otherwise, this role will deploy NetBox with an empty slate.
@@ -79,8 +83,9 @@ socket to authenticate with the Postgres server.
7983
netbox_stable: true
8084
netbox_database_socket: "{{ postgresql_unix_socket_directories[0] }}"
8185
netbox_uwsgi_socket: "0.0.0.0:80"
82-
netbox_allowed_hosts:
83-
- netbox.idolactiviti.es
86+
netbox_config:
87+
ALLOWED_HOSTS:
88+
- netbox.idolactiviti.es
8489
postgresql_users:
8590
- name: "{{ netbox_database_user }}"
8691
role_attr_flags: CREATEDB,NOSUPERUSER
@@ -96,11 +101,15 @@ installing NetBox on to authenticate with it over TCP:
96101
vars:
97102
netbox_stable: true
98103
netbox_uwsgi_socket: "0.0.0.0:80"
99-
netbox_allowed_hosts:
100-
- "{{ inventory_hostname }}"
104+
netbox_config:
105+
ALLOWED_HOSTS:
106+
- "{{ inventory_hostname }}"
101107
netbox_database_host: pg-netbox.idolactiviti.es
102108
netbox_database_port: 15432
103109
netbox_database_name: netbox_prod
104110
netbox_database_user: netbox_prod_user
105111
netbox_database_password: "very_secure_password_for_prod"
106112

113+
[NetBox]: https://github.com/digitalocean/netbox
114+
[Mandatory Settings]: http://netbox.readthedocs.io/en/stable/configuration/mandatory-settings/
115+
[Optional Settings]: http://netbox.readthedocs.io/en/stable/configuration/optional-settings/

defaults/main.yml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,11 @@ netbox_database_user: netbox
1717
netbox_database_port: 5432
1818
#netbox_database_socket: /var/run/postgresql
1919

20-
#netbox_secret_key:
21-
netbox_allowed_hosts:
22-
- localhost
23-
- 127.0.0.1
24-
netbox_login_required: false
25-
netbox_base_path:
26-
netbox_paginate_count: 50
27-
netbox_timezone: UTC
28-
netbox_prefer_ipv4: false
20+
netbox_config:
21+
#SECRET_KEY:
22+
ALLOWED_HOSTS:
23+
- localhost
24+
- 127.0.0.1
2925

3026
netbox_user: netbox
3127
netbox_group: netbox

examples/netbox_config.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
netbox_config:
3+
#SECRET_KEY: "hLk3ED%o#P0teb&UjKN)Quvr=SW$HcM^Z(Rzy_sBXFfpY+5TVC" # let this be generated by the role so you don't have to store it
4+
ALLOWED_HOSTS:
5+
- 127.0.0.1
6+
- localhost
7+
ADMINS:
8+
-
9+
- Hank Hill
10+
- hhill@example.com
11+
-
12+
- Dale Gribble
13+
- dgribble@example.com
14+
# note that the array is nested above
15+
BANNER_TOP: &BANNER_TOP 'Your banner text'
16+
BANNER_BOTTOM: *BANNER_TOP
17+
# you probably won't need this, but you can reference other variables like above
18+
BASE_PATH: netbox/
19+
CORS_ORIGIN_ALLOW_ALL: false
20+
DEBUG: yes
21+
# yes, no, false, true, False, True are all valid booleans in Ansible - they will be inserted correctly in configuration.py
22+
EMAIL:
23+
SERVER: localhost
24+
PORT: 25
25+
USERNAME: user@localhost
26+
PASSWORD: password
27+
TIMEOUT: 10
28+
FROM_EMAIL: notifications@localhost
29+
LOGIN_REQUIRED: yes
30+
MAINTENANCE_MODE: False
31+
MAX_PAGE_SIZE: 500
32+
PAGINATE_COUNT: 100
33+
PREFER_IPV4: False
34+
TIME_ZONE: UTC
35+
DATE_FORMAT: N j, Y
36+
# quotes aren't necessary as you can see - but in my opinion quotes make this more readable
37+
SHORT_DATE_FORMAT: 'Y-m-d'
38+
TIME_FORMAT: 'g:i a'
39+
SHORT_TIME_FORMAT: 'H:i:s'
40+
DATETIME_FORMAT: 'N j, Y g:i a'
41+
SHORT_DATETIME_FORMAT: 'Y-m-d H:i'

tasks/deploy_netbox.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
- include: generate_secret_key.yml
1313
when:
14-
- netbox_secret_key is not defined
14+
- netbox_config.SECRET_KEY is not defined
1515

1616
- name: Create NetBox virtualenv and install needed Python dependencies
1717
pip:

tasks/generate_secret_key.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
src: "{{ netbox_shared_path }}/generated_secret_key"
1010
register: __netbox_secret_key_file
1111

12-
- name: Set netbox_secret_key to generated SECRET_KEY
12+
- name: Set netbox_config.SECRET_KEY to generated SECRET_KEY
1313
set_fact:
14-
netbox_secret_key: "{{ __netbox_secret_key_file['content'] | b64decode }}"
14+
netbox_config: "{{ netbox_config | combine({'SECRET_KEY': __netbox_secret_key_file['content'] | b64decode}) }}"

templates/configuration.py.j2

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
# {{ ansible_managed }}
2-
ALLOWED_HOSTS = {{ netbox_allowed_hosts | to_json }}
32
DATABASE = {
43
'NAME': '{{ netbox_database }}',
54
'USER': '{{ netbox_database_user }}',
65
{% if netbox_database_host is defined %}
76
'PASSWORD': '{{ netbox_database_password }}',
87
'HOST': '{{ netbox_database_host }}',
98
'PORT': '{{ netbox_database_port }}',
10-
{% else %}
9+
{% else %}
1110
'HOST': '{{ netbox_database_socket }}',
1211
{% endif %}
1312
}
14-
SECRET_KEY = '{{ netbox_secret_key }}'
15-
LOGIN_REQUIRED = {{ "True" if netbox_login_required else "False" }}
16-
BASE_PATH = '{{ netbox_base_path }}'
17-
PAGINATE_COUNT = '{{ netbox_paginate_count }}'
18-
TIME_ZONE = '{{ netbox_timezone }}'
19-
PREFER_IPV4 = '{{ "True" if netbox_prefer_ipv4 else "False" }}'
13+
14+
{% for setting, value in netbox_config.iteritems() %}
15+
{% if value in [True, False] %}
16+
{{ setting }} = {{ 'True' if value else 'False' }}
17+
{% else %}
18+
{{ setting }} = {{ value | to_nice_json }}
19+
{% endif %}
20+
{% endfor %}
2021

2122
# vim: ft=python

tests/group_vars/netbox

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
---
22
ansible_ssh_user: root
33
netbox_uwsgi_socket: "0.0.0.0:8080"
4-
netbox_allowed_hosts:
5-
- "{{ inventory_hostname }}"
4+
netbox_config:
5+
ALLOWED_HOSTS:
6+
- "{{ inventory_hostname }}"
67
netbox_database_socket: "{{ postgresql_unix_socket_directories[0] }}"
78
postgresql_users:
89
- name: "{{ netbox_database_user }}"

tests/test.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414
url: "http://{{ inventory_hostname }}:8080"
1515
- name: uWSGI/NetBox service status
1616
shell: "systemctl status netbox.service"
17-
changed_when: false
1817
- name: uWSGI/NetBox service journal
1918
shell: "journalctl --no-pager -xu netbox.service"
20-
changed_when: false
2119
- name: NetBox application log
2220
shell: cat /srv/netbox/shared/application.log
23-
changed_when: false
21+
- name: NetBox configuration
22+
shell: cat /srv/netbox/shared/configuration.py
2423
ignore_errors: yes

0 commit comments

Comments
 (0)