Skip to content

Commit 13f2a80

Browse files
committed
Support syncing users and groups for ldap-based realms
1 parent 8412688 commit 13f2a80

File tree

3 files changed

+82
-5
lines changed

3 files changed

+82
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ you need to install the `ifupdown2` package. Note that this will remove
466466
You can set realms / domains as authentication sources in the `domains.cfg` configuration file.
467467
If this file is not present, only the `Linux PAM` and `Proxmox VE authentication server` realms
468468
are available. Supported types are `pam`, `pve`, `ad` and `ldap`.
469+
It’s possible to automatically sync users and groups for LDAP-based realms (LDAP & Microsoft Active Directory) with `sync: true`.
469470
One realm should have the `default: 1` property to mark it as the default:
470471

471472
```
@@ -489,6 +490,7 @@ pve_domains_cfg:
489490
server2: dc02.yourdomain.com
490491
- name: ldap
491492
type: ldap
493+
sync: true
492494
attributes:
493495
comment: LDAP authentication
494496
base_dn: CN=Users,dc=yourdomain,dc=com

tasks/main.yml

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,27 @@
241241
with_items: "{{ pve_users }}"
242242
when: "not pve_cluster_enabled | bool or (pve_cluster_enabled | bool and inventory_hostname == groups[pve_group][0])"
243243

244+
- import_tasks: realms_config.yml
245+
when:
246+
- not pve_cluster_enabled | bool or (pve_cluster_enabled and inventory_hostname == groups[pve_group][0])
247+
- pve_domains_cfg | length > 0
248+
249+
- name: Select ldap-based realms with sync
250+
set_fact:
251+
pve_ldap_realms_with_sync: |
252+
{{ pve_domains_cfg | selectattr('type', 'in', ['ad', 'ldap'])
253+
| selectattr('sync', 'defined') }}
254+
255+
- name: Sync ldap-based realms
256+
include_tasks: realms_sync.yml
257+
loop: "{{ pve_ldap_realms_with_sync | flatten(levels=1) }}"
258+
loop_control:
259+
loop_var: pve_ldap_realm
260+
when:
261+
- not pve_cluster_enabled | bool or (pve_cluster_enabled and inventory_hostname == groups[pve_group][0])
262+
- pve_domains_cfg | length > 0
263+
- pve_ldap_realm.sync | bool
264+
244265
- name: Configure Proxmox ACLs
245266
proxmox_acl:
246267
path: "{{ item.path }}"
@@ -322,11 +343,6 @@
322343
- "not pve_cluster_enabled | bool or (pve_cluster_enabled | bool and inventory_hostname == groups[pve_group][0])"
323344
- "pve_datacenter_cfg | length > 0"
324345

325-
- import_tasks: realms_config.yml
326-
when:
327-
- not pve_cluster_enabled | bool or (pve_cluster_enabled and inventory_hostname == groups[pve_group][0])
328-
- pve_domains_cfg | length > 0
329-
330346
- import_tasks: ssl_config.yml
331347
when:
332348
- "pve_ssl_private_key is defined"

tasks/realms_sync.yml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
# expects to be called with variable pve_ldap_realm set
3+
4+
- name: Get pre-sync state of groups
5+
ansible.builtin.shell: pveum group list --output-format json-pretty
6+
register: groups_before
7+
changed_when: false
8+
9+
- name: Get pre-sync state of users
10+
ansible.builtin.shell: pveum user list --output-format json-pretty
11+
register: users_before
12+
changed_when: false
13+
14+
- name: "Sync ldap-based realm {{ pve_ldap_realm.name }}"
15+
ansible.builtin.shell: |
16+
pveum realm sync {{ pve_ldap_realm.name }}
17+
changed_when: false
18+
19+
- name: Get post-sync state of groups
20+
ansible.builtin.shell: pveum group list --output-format json-pretty
21+
register: groups_after
22+
changed_when: false
23+
24+
- name: Get post-sync state of users
25+
ansible.builtin.shell: pveum user list --output-format json-pretty
26+
register: users_after
27+
changed_when: false
28+
29+
- name: Create temporary file for pre-post-sync comparation
30+
ansible.builtin.tempfile:
31+
state: file
32+
suffix: pve_realm_sync_pre
33+
register: pre_sync_content
34+
changed_when: false
35+
36+
- name: Save pre-sync state of groups and users
37+
ansible.builtin.copy:
38+
content: |
39+
{{ groups_before.stdout | from_json | sort(attribute='groupid') | to_yaml }}
40+
{{ users_before.stdout | from_json | sort(attribute='userid') | to_yaml }}
41+
dest: "{{ pre_sync_content.path }}"
42+
changed_when: false
43+
when: not ansible_check_mode
44+
45+
- name: "Compare to post-sync state of groups and users for realm {{ pve_ldap_realm.name }}"
46+
ansible.builtin.copy:
47+
content: |
48+
{{ groups_after.stdout | from_json | sort(attribute='groupid') | to_yaml }}
49+
{{ users_after.stdout | from_json | sort(attribute='userid') | to_yaml }}
50+
dest: "{{ pre_sync_content.path }}"
51+
when: not ansible_check_mode
52+
diff: true
53+
54+
- name: Remove the temporary file for pre-post-sync comparation
55+
ansible.builtin.file:
56+
path: "{{ pre_sync_content.path }}"
57+
state: absent
58+
when: not ansible_check_mode
59+
changed_when: false

0 commit comments

Comments
 (0)