Skip to content

Commit 08566e4

Browse files
committed
LDAP Server Configuration (#14)
Merge branch 'feature/ldap' into master
2 parents f5bbc5d + 2ce5d70 commit 08566e4

11 files changed

+99
-17
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ install:
2323
script:
2424
- ansible-playbook tests/deploy.yml -i tests/inventory --syntax-check
2525
- ansible-playbook tests/deploy.yml -i tests/inventory
26-
- unbuffer ansible-playbook -vv tests/deploy.yml -i tests/inventory >/tmp/idempotency.log 2>&1
26+
- ANSIBLE_STDOUT_CALLBACK=debug unbuffer ansible-playbook -vv tests/deploy.yml -i tests/inventory >/tmp/idempotency.log 2>&1
2727
- 'grep -A1 "PLAY RECAP" /tmp/idempotency.log | grep -qP "changed=0.*failed=0" &&
2828
(echo "Idempotence: PASS"; exit 0) || (echo "Idempotence: FAIL"; cat /tmp/idempotency.log;
2929
exit 1)'

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ By default, NetBox will be configured to output to `/srv/netbox/shared/applicati
5757
and `/srv/netbox/shared/requests.log`. You can override these with a valid
5858
uWSGI logger by setting `netbox_uwsgi_logger` and `netbox_uwsgi_req_logger`.
5959

60+
Toggle `netbox_ldap_enabled` to `true` to configure LDAP authentication for
61+
NetBox. By default, Ansible will look for `netbox_ldap_config.py.j2` in your
62+
playbook's `templates/` directory - which you can find an example of in this
63+
role's `templates/` directory. You can set `netbox_ldap_config_template` to a
64+
different location if you have your template located somewhere else.
6065

6166
Example Playbook
6267
----------------

defaults/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,6 @@ netbox_uwsgi_logger: "file:{{ netbox_shared_path }}/application.log"
4444
netbox_uwsgi_req_logger: "file:{{ netbox_shared_path }}/requests.log"
4545

4646
netbox_load_initial_data: false
47+
48+
netbox_ldap_enabled: false
49+
netbox_ldap_config_template: netbox_ldap_config.py.j2

tasks/deploy_netbox.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,35 @@
2727
notify:
2828
- reload netbox
2929

30+
- block:
31+
- name: Install django-auth-ldap if LDAP is enabled
32+
pip:
33+
name: django-auth-ldap
34+
virtualenv: "{{ netbox_virtualenv_path }}"
35+
36+
- name: Generate LDAP configuration for NetBox if enabled
37+
template:
38+
src: "{{ netbox_ldap_config_template }}"
39+
dest: "{{ netbox_shared_path }}/ldap_config.py"
40+
mode: 0600
41+
notify:
42+
- reload netbox
43+
when:
44+
- netbox_ldap_enabled
45+
3046
- name: Symlink NetBox configuration file into the active NetBox release
3147
file:
3248
src: "{{ netbox_shared_path }}/configuration.py"
3349
dest: "{{ netbox_config_path }}/configuration.py"
3450
state: link
3551

52+
- name: Symlink/Remove NetBox LDAP configuration file into/from the active NetBox release
53+
file:
54+
src: "{{ netbox_shared_path }}/ldap_config.py"
55+
dest: "{{ netbox_config_path }}/ldap_config.py"
56+
force: yes
57+
state: "{{ 'absent' if netbox_ldap_enabled else 'link' }}"
58+
3659
- name: Run database migrations for NetBox
3760
django_manage:
3861
command: migrate

tasks/install_packages_apt.yml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
- name: Install NetBox dependencies and selected Python version
2+
- name: Install required packages for selected NetBox configuration
33
apt:
44
name: "{{ item }}"
55
state: latest
@@ -8,10 +8,5 @@
88
with_items:
99
- "{{ netbox_python3_packages if (netbox_python == 3) else netbox_python2_packages }}"
1010
- "{{ netbox_packages }}"
11-
12-
- name: Ensure git is installed
13-
apt:
14-
name: git
15-
state: installed
16-
when:
17-
- netbox_git
11+
- "{{ netbox_ldap_packages if netbox_ldap_enabled else [] }}"
12+
- "{{ 'git' if netbox_git else [] }}"

tasks/install_packages_yum.yml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,13 @@
66
when:
77
- netbox_python == 3
88

9-
- name: Install NetBox dependencies and selected Python version
9+
- name: Install required packages for selected NetBox configuration
1010
yum:
1111
name: "{{ item }}"
1212
state: latest
1313
update_cache: yes
1414
with_items:
1515
- "{{ netbox_python3_packages if (netbox_python == 3) else netbox_python2_packages }}"
1616
- "{{ netbox_packages }}"
17-
18-
- name: Ensure git is installed
19-
yum:
20-
name: git
21-
state: installed
22-
when:
23-
- netbox_git
17+
- "{{ netbox_ldap_packages if netbox_ldap_enabled else [] }}"
18+
- "{{ 'git' if netbox_git else [] }}"

templates/configuration.py.j2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ BASE_PATH = '{{ netbox_base_path }}'
1717
PAGINATE_COUNT = '{{ netbox_paginate_count }}'
1818
TIME_ZONE = '{{ netbox_timezone }}'
1919
PREFER_IPV4 = '{{ "True" if netbox_prefer_ipv4 else "False" }}'
20+
21+
# vim: ft=python
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# {{ ansible_managed }}
2+
import ldap
3+
from django_auth_ldap.config import LDAPSearch, GroupOfNamesType
4+
5+
"""
6+
Read the NetBox LDAP configuration documentation if you need assistance:
7+
http://netbox.readthedocs.io/en/latest/installation/ldap/
8+
9+
This is just an example. Modify it to your liking and place it in your
10+
playbook's templates/ directory (or anywhere, but make sure
11+
"netbox_ldap_config_template" is configured to whatever location you place the
12+
template in should it not be in templates/.
13+
"""
14+
15+
# Use variables like the below if you prefer:
16+
AUTH_LDAP_SERVER_URI = "{{ ldap_server_uri }}"
17+
18+
# Or just store all your values in this file:
19+
AUTH_LDAP_BIND_DN = "CN=NETBOXSA, OU=Service Accounts,DC=example,DC=com"
20+
# I would however recommend putting passwords in vaulted variables.
21+
AUTH_LDAP_BIND_PASSWORD = "demo"
22+
23+
AUTH_LDAP_CONNECTION_OPTIONS = {
24+
ldap.OPT_REFERRALS: 0
25+
}
26+
27+
LDAP_IGNORE_CERT_ERRORS = False
28+
29+
AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=Users,dc=example,dc=com",
30+
ldap.SCOPE_SUBTREE,
31+
"(sAMAccountName=%(user)s)")
32+
AUTH_LDAP_USER_DN_TEMPLATE = "uid=%(user)s,ou=users,dc=example,dc=com"
33+
AUTH_LDAP_USER_ATTR_MAP = {
34+
"first_name": "givenName",
35+
"last_name": "sn"
36+
}
37+
38+
AUTH_LDAP_GROUP_SEARCH = LDAPSearch("dc=example,dc=com", ldap.SCOPE_SUBTREE,
39+
"(objectClass=group)")
40+
AUTH_LDAP_GROUP_TYPE = GroupOfNamesType()
41+
AUTH_LDAP_REQUIRE_GROUP = "CN=NETBOX_USERS,DC=example,DC=com"
42+
AUTH_LDAP_USER_FLAGS_BY_GROUP = {
43+
"is_active": "cn=active,ou=groups,dc=example,dc=com",
44+
"is_staff": "cn=staff,ou=groups,dc=example,dc=com",
45+
"is_superuser": "cn=superuser,ou=groups,dc=example,dc=com"
46+
}
47+
AUTH_LDAP_FIND_GROUP_PERMS = True
48+
AUTH_LDAP_CACHE_GROUPS = True
49+
AUTH_LDAP_GROUP_CACHE_TIMEOUT = 3600
50+
51+
# vim: ft=python

vars/centos-7.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ netbox_python3_packages:
2020
- python34-pip
2121
netbox_python3_binary: /usr/bin/python3.4
2222
netbox_pip3_binary: /usr/bin/pip3
23+
netbox_ldap_packages:
24+
- openldap-devel

vars/debian-8.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ netbox_python3_packages:
1818
- python3-pip
1919
netbox_python3_binary: /usr/bin/python3.4
2020
netbox_pip3_binary: /usr/bin/pip3
21+
netbox_ldap_packages:
22+
- libldap2-dev
23+
- libssl-dev

vars/ubuntu-16.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ netbox_python3_packages:
1818
- python3-pip
1919
netbox_python3_binary: /usr/bin/python3.5
2020
netbox_pip3_binary: /usr/bin/pip3
21+
netbox_ldap_packages:
22+
- libldap2-dev
23+
- libssl-dev

0 commit comments

Comments
 (0)