Skip to content
Open
27 changes: 27 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,33 @@ 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: netbox_custom_validators.py
----

Toggle `netbox_custom_validators_enabled` to `true` to create a custom validator file for
NetBox. `netbox_custom_validators_file` should be the path to your custom validator file - by
default, Ansible will search your playbook's `files/` directory for this.
You can find an example in `examples/`. You will also need to set
`netbox_config.CUSTOM_VALIDATORS` to

```
CUSTOM_VALIDATORS: |
{
"dcim.device": (
'custom_validators.DeviceValidator',
),
"virtualization.virtualmachine": (
'custom_validators.VirtualMachineValidator',
)
}
```

TIP: By default, a local (non-LDAP) superuser will still be created by this
role. If this is undesirable, consider toggling `netbox_superuser_enabled`.

[source,yaml]
----
netbox_napalm_enabled: false
Expand Down
3 changes: 3 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ 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_file:

netbox_napalm_enabled: false
netbox_napalm_packages:
- napalm
Expand Down
24 changes: 24 additions & 0 deletions tasks/deploy_netbox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,30 @@
notify:
- reload netbox.service

# custom validators
- name: Copy NetBox custom validators into shared
ansible.builtin.copy:
src: "{{ netbox_custom_validators_file }}"
dest: "{{ netbox_shared_path }}/custom_validators.py"
owner: "{{ netbox_user }}"
group: "{{ netbox_group }}"
ignore_errors: yes
when:
- netbox_custom_validators_file is defined and netbox_custom_validators_enabled
notify:
- reload netbox.service

- name: Symlink/Remove NetBox local_settings.py file into/from the active NetBox release
Copy link
Owner

@lae lae Dec 4, 2024

Choose a reason for hiding this comment

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

Compared to local_settings.py, as far as I can tell custom_validators.py is not an expected file that gets imported in the upstream NetBox codebase. Having not tested this I'm assuming dropping it in the netbox folder makes it reachable from NetBox as custom_validators.XYZ and is why it's being symlinked here?

This isn't really an issue for stable NetBox deployments, but if I recall correctly it prevents updates for git-based deployments because the NetBox directory ends up having uncommitted changes. Which means we have to use a different method of making this file importable, maybe via installing the file into the virtualenv's site-packages directory directly?

file:
src: "{{ netbox_shared_path + '/custom_validators.py' if netbox_custom_validators_enabled else omit }}"
dest: "{{ netbox_current_path }}/netbox/custom_validators.py"
owner: "{{ netbox_user }}"
group: "{{ netbox_group }}"
state: "{{ 'link' if netbox_custom_validators_enabled else 'absent' }}"
notify:
- reload netbox.service


- name: Copy NetBox scripts into SCRIPTS_ROOT
copy:
src: "{{ item.src }}"
Expand Down
2 changes: 2 additions & 0 deletions templates/configuration.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,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
Loading