From 06fc8e4e6c9a8cc39f90a138f12c6808ae9794a3 Mon Sep 17 00:00:00 2001 From: Matthew Smith Date: Wed, 27 Nov 2024 09:01:08 +1100 Subject: [PATCH 1/5] deploy local settings to shared and symlink --- tasks/deploy_netbox.yml | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tasks/deploy_netbox.yml b/tasks/deploy_netbox.yml index 39bf78d..bce9d86 100644 --- a/tasks/deploy_netbox.yml +++ b/tasks/deploy_netbox.yml @@ -133,6 +133,30 @@ notify: - reload netbox.service +# local_settings.py +- name: Copy NetBox local_settings.py into shared (ignore if it doesn't exist) + copy: + src: "{{ playbook_dir }}/files/netbox/local_settings.py" + dest: "{{ netbox_shared_path }}/local_settings.py" + owner: "{{ netbox_user }}" + group: "{{ netbox_group }}" + ignore_errors: yes + +- name: Check if local_settings.py file exists in shared + stat: + path: "{{ netbox_shared_path }}/local_settings.py" + register: netbox_local_settings_file_in_shared + +- name: Symlink/Remove NetBox local_settings.py file into/from the active NetBox release + file: + src: "{{ netbox_shared_path + '/local_settings.py' if netbox_local_settings_file_in_shared.stat.exists else omit }}" + dest: "{{ netbox_config_path }}/local_settings.py" + owner: "{{ netbox_user }}" + group: "{{ netbox_group }}" + state: "{{ 'link' if netbox_local_settings_file_in_shared.stat.exists else 'absent' }}" + notify: + - reload netbox.service + - name: Copy NetBox scripts into SCRIPTS_ROOT copy: src: "{{ item.src }}" From dcc474966e4a3097bfecf5ff9c2a2c3593a13c41 Mon Sep 17 00:00:00 2001 From: Matthew Smith Date: Thu, 28 Nov 2024 10:22:56 +1100 Subject: [PATCH 2/5] change the way it works so it is var driven, update readme and role defaults, needs testing --- README.adoc | 13 +++++++++++++ defaults/main.yml | 3 +++ tasks/deploy_netbox.yml | 17 ++++++++--------- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/README.adoc b/README.adoc index f738bec..4659682 100644 --- a/README.adoc +++ b/README.adoc @@ -399,6 +399,19 @@ You can find an example in `examples/`. You will also need to set 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_local_settings_enabled: false +netbox_local_settings_config_file: local_setting.py +---- + +Toggle `netbox_local_settings_enabled` to `true` to deploy local_settings.py for +NetBox. `netbox_local_settings_config_file` should be the path to your file - by +default, Ansible will search your playbook's `files/` directory for this. + +NOTE: The destination file will always be `local_settings.py`, the source file name +can be unique. + [source,yaml] ---- netbox_napalm_enabled: false diff --git a/defaults/main.yml b/defaults/main.yml index c281ae2..a642203 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -74,6 +74,9 @@ netbox_requests_log: "file:{{ netbox_shared_path }}/requests.log" netbox_ldap_enabled: false netbox_ldap_config_template: netbox_ldap_config.py.j2 +netbox_local_settings_enabled: false +netbox_local_settings_config_file: "netbox_local_settings.py" + netbox_napalm_enabled: false netbox_napalm_packages: - napalm diff --git a/tasks/deploy_netbox.yml b/tasks/deploy_netbox.yml index bce9d86..4c5d7ac 100644 --- a/tasks/deploy_netbox.yml +++ b/tasks/deploy_netbox.yml @@ -135,25 +135,24 @@ # local_settings.py - name: Copy NetBox local_settings.py into shared (ignore if it doesn't exist) - copy: - src: "{{ playbook_dir }}/files/netbox/local_settings.py" + ansible.builtin.copy: + src: "{{ netbox_local_settings_config_file }}" dest: "{{ netbox_shared_path }}/local_settings.py" owner: "{{ netbox_user }}" group: "{{ netbox_group }}" ignore_errors: yes - -- name: Check if local_settings.py file exists in shared - stat: - path: "{{ netbox_shared_path }}/local_settings.py" - register: netbox_local_settings_file_in_shared + when: + - netbox_local_settings_enabled + notify: + - reload netbox.service - name: Symlink/Remove NetBox local_settings.py file into/from the active NetBox release file: - src: "{{ netbox_shared_path + '/local_settings.py' if netbox_local_settings_file_in_shared.stat.exists else omit }}" + src: "{{ netbox_shared_path + '/local_settings.py' if netbox_local_settings_enabled else omit }}" dest: "{{ netbox_config_path }}/local_settings.py" owner: "{{ netbox_user }}" group: "{{ netbox_group }}" - state: "{{ 'link' if netbox_local_settings_file_in_shared.stat.exists else 'absent' }}" + state: "{{ 'link' if netbox_local_settings_enabled else 'absent' }}" notify: - reload netbox.service From 3e304d796217a3b9e22bcd61ca3c8c781c099fc9 Mon Sep 17 00:00:00 2001 From: Matthew Smith Date: Thu, 28 Nov 2024 16:01:04 +1100 Subject: [PATCH 3/5] switch to a single var --- README.adoc | 9 ++++----- defaults/main.yml | 3 +-- tasks/deploy_netbox.yml | 8 ++++---- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/README.adoc b/README.adoc index 4659682..22430f0 100644 --- a/README.adoc +++ b/README.adoc @@ -401,16 +401,15 @@ role. If this is undesirable, consider toggling `netbox_superuser_enabled`. [source,yaml] ---- -netbox_local_settings_enabled: false -netbox_local_settings_config_file: local_setting.py +netbox_local_settings_file: netbox_local_settings.py ---- -Toggle `netbox_local_settings_enabled` to `true` to deploy local_settings.py for -NetBox. `netbox_local_settings_config_file` should be the path to your file - by +Add the file to copy from ansible to `netbox_local_settings_file` to deploy local_settings.py to +NetBox. `netbox_local_settings_file` should be the path to your file - by default, Ansible will search your playbook's `files/` directory for this. NOTE: The destination file will always be `local_settings.py`, the source file name -can be unique. +can be unique to allow for different deployments from the one ansible repository. [source,yaml] ---- diff --git a/defaults/main.yml b/defaults/main.yml index a642203..2878a04 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -74,8 +74,7 @@ netbox_requests_log: "file:{{ netbox_shared_path }}/requests.log" netbox_ldap_enabled: false netbox_ldap_config_template: netbox_ldap_config.py.j2 -netbox_local_settings_enabled: false -netbox_local_settings_config_file: "netbox_local_settings.py" +netbox_local_settings_file: netbox_napalm_enabled: false netbox_napalm_packages: diff --git a/tasks/deploy_netbox.yml b/tasks/deploy_netbox.yml index 4c5d7ac..e0afea0 100644 --- a/tasks/deploy_netbox.yml +++ b/tasks/deploy_netbox.yml @@ -136,23 +136,23 @@ # local_settings.py - name: Copy NetBox local_settings.py into shared (ignore if it doesn't exist) ansible.builtin.copy: - src: "{{ netbox_local_settings_config_file }}" + src: "{{ netbox_local_settings_file }}" dest: "{{ netbox_shared_path }}/local_settings.py" owner: "{{ netbox_user }}" group: "{{ netbox_group }}" ignore_errors: yes when: - - netbox_local_settings_enabled + - netbox_local_settings_file is defined notify: - reload netbox.service - name: Symlink/Remove NetBox local_settings.py file into/from the active NetBox release file: - src: "{{ netbox_shared_path + '/local_settings.py' if netbox_local_settings_enabled else omit }}" + src: "{{ netbox_shared_path + '/local_settings.py' if netbox_local_settings_file is defined else omit }}" dest: "{{ netbox_config_path }}/local_settings.py" owner: "{{ netbox_user }}" group: "{{ netbox_group }}" - state: "{{ 'link' if netbox_local_settings_enabled else 'absent' }}" + state: "{{ 'link' if netbox_local_settings_file is defined else 'absent' }}" notify: - reload netbox.service From 282202a894e251eea9608a168b76d415a6afedf6 Mon Sep 17 00:00:00 2001 From: Matthew Smith Date: Fri, 29 Nov 2024 11:23:32 +1100 Subject: [PATCH 4/5] better readme --- README.adoc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.adoc b/README.adoc index 22430f0..78a4cb1 100644 --- a/README.adoc +++ b/README.adoc @@ -404,9 +404,10 @@ role. If this is undesirable, consider toggling `netbox_superuser_enabled`. netbox_local_settings_file: netbox_local_settings.py ---- -Add the file to copy from ansible to `netbox_local_settings_file` to deploy local_settings.py to -NetBox. `netbox_local_settings_file` should be the path to your file - by -default, Ansible will search your playbook's `files/` directory for this. +Add the `netbox_local_settings_file` var with the name of the local settings +file in your ansible repository to deploy local_settings.py to NetBox. +Ansible will search your playbook's `files/` directory for this when using a +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. From 3be94586c0ec729906ab3741947407768d0d90b0 Mon Sep 17 00:00:00 2001 From: Musee Ullah Date: Fri, 29 Nov 2024 11:27:57 +0900 Subject: [PATCH 5/5] Make netbox_local_settings_file optional and include explanation --- README.adoc | 14 +++++++------- defaults/main.yml | 2 +- tasks/deploy_netbox.yml | 5 ++--- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/README.adoc b/README.adoc index 78a4cb1..90e5986 100644 --- a/README.adoc +++ b/README.adoc @@ -401,16 +401,16 @@ role. If this is undesirable, consider toggling `netbox_superuser_enabled`. [source,yaml] ---- -netbox_local_settings_file: netbox_local_settings.py +# netbox_local_settings_file: "{{ playbook_dir }}/files/netbox/local_settings.py" ---- -Add the `netbox_local_settings_file` var with the name of the local settings -file in your ansible repository to deploy local_settings.py to NetBox. -Ansible will search your playbook's `files/` directory for this when using a -relative path. +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: 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. +NOTE: Commenting or removing this role variable from your playbook will remove `local_settings.py` from your NetBox deployment. [source,yaml] ---- diff --git a/defaults/main.yml b/defaults/main.yml index 2878a04..1406a97 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -74,7 +74,7 @@ netbox_requests_log: "file:{{ netbox_shared_path }}/requests.log" netbox_ldap_enabled: false netbox_ldap_config_template: netbox_ldap_config.py.j2 -netbox_local_settings_file: +# netbox_local_settings_file: "{{ playbook_dir }}/files/netbox/local_settings.py" netbox_napalm_enabled: false netbox_napalm_packages: diff --git a/tasks/deploy_netbox.yml b/tasks/deploy_netbox.yml index e0afea0..4854018 100644 --- a/tasks/deploy_netbox.yml +++ b/tasks/deploy_netbox.yml @@ -140,15 +140,14 @@ dest: "{{ netbox_shared_path }}/local_settings.py" owner: "{{ netbox_user }}" group: "{{ netbox_group }}" - ignore_errors: yes when: - netbox_local_settings_file is defined notify: - reload netbox.service - name: Symlink/Remove NetBox local_settings.py file into/from the active NetBox release - file: - src: "{{ netbox_shared_path + '/local_settings.py' if netbox_local_settings_file is defined else omit }}" + ansible.builtin.file: + src: "{{ netbox_shared_path }}/local_settings.py" dest: "{{ netbox_config_path }}/local_settings.py" owner: "{{ netbox_user }}" group: "{{ netbox_group }}"