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

Support for installing/configuring CUSTOM_VALIDATORS #194

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
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
Copy link
Owner

Choose a reason for hiding this comment

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

This appears to refer to an example but I don't see any changes in this PR to introduce that example.

`netbox_config.CUSTOM_VALIDATORS` to
Copy link
Owner

Choose a reason for hiding this comment

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

This could use some more clarity. I don't think this is the exact value that users should be using, right? It depends on what validators they define in their validators file.


```
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
Copy link
Owner

Choose a reason for hiding this comment

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

Not relevant, I think this was accidentally copied and left in?

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:
Copy link
Owner

Choose a reason for hiding this comment

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

I'd suggest setting a non-empty default here, custom_validators.py is fine (and the user can override it with something more specific if they prefer).

Copy link
Owner

Choose a reason for hiding this comment

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

Actually it looks like a default was specified in the README. This should be consistent with that. I'm fine with either filename.


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
Copy link
Owner

Choose a reason for hiding this comment

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

Errors shouldn't be ignored here.

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


Copy link
Owner

Choose a reason for hiding this comment

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

Remove extra newline.

- 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