From dd54add12027f5cd996bc968da2b7a1fd1f6e590 Mon Sep 17 00:00:00 2001 From: Matthew Smith Date: Fri, 29 Nov 2024 12:32:04 +1100 Subject: [PATCH 1/3] inital commit, needs testing --- README.adoc | 27 +++++++++++++++++++++++++++ defaults/main.yml | 3 +++ tasks/deploy_netbox.yml | 24 ++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/README.adoc b/README.adoc index 78a4cb1..f63757a 100644 --- a/README.adoc +++ b/README.adoc @@ -412,6 +412,33 @@ relative path. NOTE: The destination file will always be `local_settings.py`, the source file name can be unique to allow for different deployments from the one ansible repository. +[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 diff --git a/defaults/main.yml b/defaults/main.yml index 2878a04..c344982 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -76,6 +76,9 @@ netbox_ldap_config_template: netbox_ldap_config.py.j2 netbox_local_settings_file: +netbox_custom_validators_enabled: false +netbox_custom_validators_file: + netbox_napalm_enabled: false netbox_napalm_packages: - napalm diff --git a/tasks/deploy_netbox.yml b/tasks/deploy_netbox.yml index e0afea0..58cb07e 100644 --- a/tasks/deploy_netbox.yml +++ b/tasks/deploy_netbox.yml @@ -156,6 +156,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 + file: + src: "{{ netbox_shared_path + '/custom_validators.py' if netbox_custom_validators_enabled else omit }}" + dest: "{{ netbox_config_path }}/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 }}" From bdc808b1782920f4db49b1035fb78d21a0913cc2 Mon Sep 17 00:00:00 2001 From: Matthew Smith Date: Mon, 2 Dec 2024 10:41:04 +1100 Subject: [PATCH 2/3] specific handling for custom_validators --- templates/configuration.py.j2 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/templates/configuration.py.j2 b/templates/configuration.py.j2 index a248be2..9bafab1 100644 --- a/templates/configuration.py.j2 +++ b/templates/configuration.py.j2 @@ -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 %} From b83128d578cc329e6b0c6a317fda59fc07ef05dd Mon Sep 17 00:00:00 2001 From: Matthew Smith Date: Tue, 3 Dec 2024 14:47:02 +1100 Subject: [PATCH 3/3] put validators in the correct directory --- tasks/deploy_netbox.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks/deploy_netbox.yml b/tasks/deploy_netbox.yml index bb00bf2..41245bd 100644 --- a/tasks/deploy_netbox.yml +++ b/tasks/deploy_netbox.yml @@ -173,7 +173,7 @@ - name: Symlink/Remove NetBox local_settings.py file into/from the active NetBox release file: src: "{{ netbox_shared_path + '/custom_validators.py' if netbox_custom_validators_enabled else omit }}" - dest: "{{ netbox_config_path }}/custom_validators.py" + dest: "{{ netbox_current_path }}/netbox/custom_validators.py" owner: "{{ netbox_user }}" group: "{{ netbox_group }}" state: "{{ 'link' if netbox_custom_validators_enabled else 'absent' }}"