Skip to content
Open
24 changes: 23 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -399,14 +399,36 @@ If this is undesirable, consider toggling `netbox_superuser_enabled`.
# netbox_local_settings_file: "{{ playbook_dir }}/files/netbox/local_settings.py"
----

If you need to override any settings or extend the functionality in NetBox' `settings.py`
If you need to override any settings or extend the functionality in NetBox' `settings.py`
in a way that is not supported by the `configuration.py` (i.e. the `netbox_config` role variable),
you can set `netbox_local_settings_file` to a local file path in your playbook to deploy a `local_settings.py` file within NetBox.
This feature was https://github.com/netbox-community/netbox/issues/16127[introduced in NetBox v4.0.2].
You may need to use this file for deploying certain NetBox plugins.

NOTE: Commenting or removing this role variable from your playbook will remove `local_settings.py` from your NetBox deployment.

[source,yaml]
----
netbox_custom_validators_enabled: true
netbox_custom_validators_file:
- file: netbox/custom_validators.py
action: deploy
netbox_custom_pipelines_enabled: true
netbox_custom_pipelines_files:
- file: netbox/custom_pipeline.py
action: deploy
----
Actions are: +
`deploy` – copy file from Ansible files directory to shared and symlink to NetBox install (default) +
`copy` – copy file from Ansible files directory to shared and remove symlinks to NetBox install +
`remove` – remove file from shared directory and remove symlinks to NetBox install


Toggle `netbox_custom_validators_enabled`|`netbox_custom_pipelines_enabled` to `true` to enable management of custom validator|pipeline files for NetBox.
`netbox_custom_validators_file`|`netbox_custom_pipelines_files` should be the path to your custom validator|pipeline file - by default, Ansible will search your playbook's `files/` directory for this.

NOTE: The `file` property supports nesting of directories inside `files/`, the copied file uses the basename: eg `files/netbox/custom_validators.py` -> `shared/custom_validators.py`.

[source,yaml]
----
netbox_napalm_enabled: false
Expand Down
12 changes: 12 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ netbox_ldap_config_template: netbox_ldap_config.py.j2

# netbox_local_settings_file: "{{ playbook_dir }}/files/netbox/local_settings.py"

netbox_custom_validators_enabled: false
netbox_custom_validators_files:
- file: "custom_validators.py"
# Valid states are remove, copy, deploy (default)
action: remove

netbox_custom_pipelines_enabled: false
netbox_custom_pipelines_files:
- file: "custom_pipeline.py"
# Valid states are remove, copy, deploy (default)
action: remove

netbox_napalm_enabled: false
netbox_napalm_packages:
- napalm
Expand Down
84 changes: 84 additions & 0 deletions tasks/deploy_netbox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,90 @@
group: "{{ netbox_group }}"
mode: 0640
state: "{{ 'link' if netbox_local_settings_file is defined else 'absent' }}"

# custom_validators
- name: Copy NetBox custom_validators files to shared
ansible.builtin.copy:
src: "{{ item.file }}"
dest: "{{ netbox_shared_path ~ '/' ~ (item.file | basename) }}"
owner: "{{ netbox_user }}"
group: "{{ netbox_group }}"
mode: 0640
loop: "{{ netbox_custom_validators_files }}"
when:
- netbox_custom_validators_enabled
- netbox_custom_validators_files is defined
- item.file is defined
- item.file | length > 0
- (item.action | default('deploy')) != 'remove'

- name: Remove NetBox custom_validators files from shared
ansible.builtin.file:
path: "{{ netbox_shared_path ~ '/' ~ (item.file | basename) }}"
state: absent
loop: "{{ netbox_custom_validators_files }}"
when:
- netbox_custom_validators_enabled
- netbox_custom_validators_files is defined
- item.file is defined
- item.file | length > 0
- (item.action | default('deploy')) == 'remove'

- name: Add/Remove NetBox custom_validators symlinks into the active NetBox release
ansible.builtin.file:
src: "{{ netbox_shared_path ~ '/' ~ (item.file | basename) if (item.action | default('deploy')) == 'deploy' and netbox_custom_validators_enabled else omit }}"
dest: "{{ netbox_current_path }} ~ '/netbox/' ~ (item.file | basename) }}"
owner: "{{ netbox_user }}"
group: "{{ netbox_group }}"
mode: 0640
state: "{{ 'link' if (item.action | default('deploy')) == 'deploy' else 'absent' }}"
loop: "{{ netbox_custom_validators_files }}"
when:
- netbox_custom_validators_files is defined
- item.file is defined
- item.file | length > 0

# custom_pipelines for python social auth
- name: Copy NetBox custom_pipelines files to shared
ansible.builtin.copy:
src: "{{ item.file }}"
dest: "{{ netbox_shared_path ~ '/' ~ (item.file | basename) }}"
owner: "{{ netbox_user }}"
group: "{{ netbox_group }}"
mode: 0640
loop: "{{ netbox_custom_pipelines_files }}"
when:
- netbox_custom_pipelines_enabled
- netbox_custom_pipelines_files is defined
- item.file is defined
- item.file | length > 0
- (item.action | default('deploy')) != 'remove'

- name: Remove NetBox custom_pipelines files from shared
ansible.builtin.file:
path: "{{ netbox_shared_path ~ '/' ~ (item.file | basename) }}"
state: absent
loop: "{{ netbox_custom_pipelines_files }}"
when:
- netbox_custom_pipelines_enabled
- netbox_custom_pipelines_files is defined
- item.file is defined
- item.file | length > 0
- (item.action | default('deploy')) == 'remove'

- name: Add/Remove NetBox custom_pipelines symlinks into the active NetBox release
ansible.builtin.file:
src: "{{ netbox_shared_path ~ '/' ~ (item.file | basename) if (item.action | default('deploy')) == 'deploy' and netbox_custom_pipelines_enabled else omit }}"
dest: "{{ netbox_config_path ~ '/' ~ (item.file | basename) }}"
owner: "{{ netbox_user }}"
group: "{{ netbox_group }}"
mode: 0640
state: "{{ 'link' if (item.action | default('deploy')) == 'deploy' else 'absent' }}"
loop: "{{ netbox_custom_pipelines_files }}"
when:
- netbox_custom_pipelines_files is defined
- item.file is defined
- item.file | length > 0
notify:
- reload netbox.service

Expand Down
2 changes: 2 additions & 0 deletions templates/configuration.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ METRICS_ENABLED = True
{% for setting, value in _netbox_config.items() %}
{% if value in [True, False] %}
{{ setting }} = {{ 'True' if value else 'False' }}
{% elif setting == 'CUSTOM_VALIDATORS' %}
{{ setting }} = {{ value | safe }}
{% elif value is string or value is number %}
{{ setting }} = {{ value | to_nice_json }}
{% else %}
Expand Down