Skip to content

Commit

Permalink
Add support for NetBox plugin installation and configuration (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
jvoss authored Apr 29, 2022
1 parent 360173a commit f964be6
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 7 deletions.
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,35 @@ information about available external authentication methods.

## Plugins

Coming soon.
Netbox plugins that are pip modules can be installed and configured by setting
the `netbox_plugins` list variable. Below is an example for the Netbox BGP
plugin.

```yaml
netbox_plugins:
- name: netbox_bgp # Plugin name
pip: netbox-bgp # Pip module name
config: # Plugin configuration
device_ext_page: left
asdot: True
```

### Removing Plugins
To remove a plugin, an `absent` state can be assigned to the `netbox_plugins`
entry:

```yaml
netbox_plugins:
- name: netbox_bgp # Plugin name
pip: netbox-bgp # Pip module name
state: absent
```

**Note that it may be necessary to remove database tables that were installed
as part of a plugin.** This role does not manage database tables that may have
been created as part of a plugin. Please
[see the documentation](https://docs.netbox.dev/en/stable/plugins/#drop-database-tables)
for more information on table management.

## Version locking

Expand Down
2 changes: 1 addition & 1 deletion handlers/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
register: config_check
failed_when: "config_check.stdout != 'System check identified no issues (0 silenced).'"

- name: handler | restart Netbox services
- name: handlers | restart Netbox services
ansible.builtin.service:
name: "{{ item }}"
state: restarted
Expand Down
2 changes: 1 addition & 1 deletion tasks/configure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
owner: "{{ netbox_user }}"
mode: '0644'
create: yes
register: netbox_auth_ldap_requirements
register: netbox_local_requirements
when: netbox_auth_ldap is defined

- name: configure | set netbox_override_dynamic_config True for Netbox < 3.1.0
Expand Down
3 changes: 3 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
- name: Include Netbox configuration tasks
include_tasks: configure.yml

- name: Include Netbox plugin tasks
include_tasks: plugins.yml

- name: Include Netbox upgrade tasks
include_tasks: upgrade.yml

Expand Down
28 changes: 28 additions & 0 deletions tasks/plugins.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
- name: plugins | manage plugins listed in local_requirements.txt
ansible.builtin.lineinfile:
path: "{{ netbox_current_path }}/local_requirements.txt"
line: "{{ item.pip }}"
regexp: "^{{ item.pip }}"
owner: "{{ netbox_user }}"
mode: '0644'
create: yes
state: "{{ item.state | default('present') }}"
loop: "{{ netbox_plugins }}"
loop_control:
label: "{{ item.pip | default('') }}"
register: netbox_local_requirements
when: item.pip is defined

- name: plugins | uninstall pip plugins marked absent
ansible.builtin.pip:
name: "{{ item.pip }}"
virtualenv: "{{ netbox_current_path }}/venv"
state: absent
loop: "{{ netbox_plugins }}"
loop_control:
label: "{{ item.pip | default('') }}"
notify: restart_netbox
when:
- item.pip is defined
- item.state | default('present') == 'absent'
2 changes: 1 addition & 1 deletion tasks/upgrade.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
become_method: sudo
become_user: "{{ netbox_user }}"
notify: restart_netbox
when: netbox_current_symlink.changed or netbox_auth_ldap_requirements.changed or netbox_napalm_step.changed
when: netbox_current_symlink.changed or netbox_local_requirements.changed or netbox_napalm_step.changed
tags:
- skip_ansible_lint # task vs handler to trigger at specific point in run
16 changes: 13 additions & 3 deletions templates/configuration.py.j2
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# {{ ansible_managed }}

import json

#########################
# #
# Required settings #
Expand Down Expand Up @@ -208,13 +210,21 @@ NAPALM_ARGS = {{ netbox_napalm.args|to_json }}
PAGINATE_COUNT = {{ netbox_paginate_count }}
{% endif %}

PLUGINS = {{ netbox_plugins | map(attribute='name') | to_json }}
PLUGINS = [
{%- for plugin in netbox_plugins %}
{%- if plugin.state | default('present') == 'present' %}
'{{ plugin.name }}',
{%- endif %}
{%- endfor %}
]

{% set plugins_config = dict() %}
{% for plugin in netbox_plugins %}
{% set x = plugins_config.__setitem__(plugin.name, plugin.config) %}
{% if plugin.state | default('present') == 'present' %}
{% set x = plugins_config.__setitem__(plugin.name, (plugin.config | default({}))) %}
{% endif %}
{% endfor %}
PLUGINS_CONFIG = {{ plugins_config | to_json }}
PLUGINS_CONFIG = json.loads('{{ plugins_config | to_json }}')

{% if netbox_override_dynamic_config %}
PREFER_IPV4 = {{ netbox_prefer_ipv4 }}
Expand Down

0 comments on commit f964be6

Please sign in to comment.