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

[debops.NetBox] Add support for standby NetBox on read only PSQL database #13

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions ansible/roles/netbox/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ netbox__domain: '{{ ansible_domain }}'

# ]]]
# ]]]
# Primary/Standby configuration [[[
# -----------------------------

# .. envvar:: netbox__primary [[[
#
# Boolean to define if the instance of NetBox will be primary or not.
# True: This instance will become primary and needs read and write
# database access.
# False: This instance will become standby,
# "netbox__config_maintenance_mode" will be set to True and
# "netbox__config_session_file_path" will be populated.
netbox__primary: True

# ]]]
# ]]]

# APT packages [[[
# ----------------

Expand Down Expand Up @@ -660,7 +676,7 @@ netbox__config_base_path: ''
#
# Enable or disable maintenance mode banner.
# This overwrites potentially existing `dynamic configuration settings <https://netbox.readthedocs.io/en/stable/configuration/dynamic-settings/>`__.
netbox__config_maintenance_mode: False
netbox__config_maintenance_mode: '{{ not netbox__primary }}'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use the bool filter here to support all the boolean values commonly accepted by Ansible.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


# ]]]
# .. envvar:: netbox__config_napalm_username [[[
Expand Down Expand Up @@ -833,7 +849,7 @@ netbox__config_metrics_enabled: False
# can be useful for enabling authentication on a standby instance with
# read-only database access.) Note that the user as which NetBox runs must have
# read and write permissions to this path.
netbox__config_session_file_path: False
netbox__config_session_file_path: '{{ "" if netbox__primary else netbox__data + "/sessions" }}'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use the bool filter here to support all the boolean values commonly accepted by Ansible.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


# ]]]
# .. envvar:: netbox__config_media_root [[[
Expand Down
57 changes: 55 additions & 2 deletions ansible/roles/netbox/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,61 @@
PATH: '{{ netbox__virtualenv_env_path }}'
become: True
become_user: '{{ netbox__user }}'
when: netbox__register_checkout is changed
when: (netbox__register_checkout is changed and
netbox__primary|bool)
register: netbox__register_migration

- name: Generate static content
## Since we do not run the manage.py file on secondary sites
## we need to generate those files in an extra task.
shell: # noqa no-handler
cmd: |
set -o nounset -o pipefail -o errexit
./manage.py collectstatic --no-input
chdir: '{{ netbox__git_checkout + "/netbox" }}'
executable: 'bash'
environment:
VIRTUAL_ENV: '{{ netbox__virtualenv }}'
PATH: '{{ netbox__virtualenv_env_path }}'
become: True
become_user: '{{ netbox__user }}'
when: (netbox__register_checkout is changed and
not netbox__primary|bool)
register: netbox__register_collectstatic
changed_when: not netbox__register_collectstatic.stdout is search('0 static files copied')

- name: Create local session directory
file:
path: '{{ netbox__data + "/sessions" }}'
owner: '{{ netbox__user }}'
group: '{{ netbox__group }}'
mode: '0770'
access_time: preserve
modification_time: preserve
state: directory
become: True
become_user: '{{ netbox__user }}'
when: (not netbox__primary|bool)

- name: Cleanup stale contenttypes and sessions
## Since we do not run the manage.py file on secondary sites
## we need to run the cleanup in an extra task.
shell: # noqa no-handler
cmd: |
set -o nounset -o pipefail -o errexit
./manage.py remove_stale_contenttypes --no-input
./manage.py clearsessions
chdir: '{{ netbox__git_checkout + "/netbox" }}'
executable: 'bash'
environment:
VIRTUAL_ENV: '{{ netbox__virtualenv }}'
PATH: '{{ netbox__virtualenv_env_path }}'
become: True
become_user: '{{ netbox__user }}'
when: (netbox__register_checkout is changed and
not netbox__primary|bool)
changed_when: false

- name: Create Django superuser account
environment:
DJANGO_SUPERUSER_PASSWORD: '{{ netbox__superuser_password }}'
Expand All @@ -225,7 +277,8 @@
virtualenv: '{{ netbox__virtualenv }}'
become: True
become_user: '{{ netbox__user }}'
when: (not netbox__register_installed.stat.exists|bool and
when: (netbox__primary|bool and
not netbox__register_installed.stat.exists|bool and
not netbox__register_migration.stdout is search('No migrations to apply.'))
no_log: '{{ debops__no_log | d(True) }}'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ SCRIPTS_ROOT = '{{ netbox__config_scripts_root }}'
# By default, NetBox will store session data in the database. Alternatively, a file path can be specified here to use
# local file storage instead. (This can be useful for enabling authentication on a standby instance with read-only
# database access.) Note that the user as which NetBox runs must have read and write permissions to this path.
SESSION_FILE_PATH = {{ netbox__config_session_file_path }}
SESSION_FILE_PATH = '{{ netbox__config_session_file_path }}'

# Time zone (default: UTC)
TIME_ZONE = '{{ netbox__config_time_zone }}'
Expand Down