Files
syno-scripts/PVE-IDM/tasks/pve.yml
T
2026-09-08 17:05:36 +02:00

71 lines
3.1 KiB
YAML

- name: Check PVE version
ansible.builtin.command: pveversion
register: pve_version
changed_when: false
- name: Require PVE 9
ansible.builtin.assert:
that: "'pve-manager/9.' in pve_version.stdout"
- name: Verify OIDC discovery using PVE system CA trust
ansible.builtin.uri:
url: "{{ pve_oidc_issuer }}/.well-known/openid-configuration"
validate_certs: true
ca_path: "{{ system_ca_bundle }}"
use_proxy: false
follow_redirects: none
register: oidc_discovery
- name: Require exact issuer match
ansible.builtin.assert:
that: oidc_discovery.json.issuer == pve_oidc_issuer
- name: Read PVE realms
ansible.builtin.command: pveum realm list --output-format json
register: pve_realms
changed_when: false
- name: Select existing realm
ansible.builtin.set_fact:
pve_realm_matches: "{{ pve_realms.stdout | from_json | selectattr('realm', 'equalto', pve_realm) | list }}"
- name: Prevent changing another realm type
ansible.builtin.assert:
that: pve_realm_matches | length == 0 or pve_realm_matches[0].type == 'openid'
- name: Read full existing realm configuration
ansible.builtin.command:
argv: [pvesh, get, "/access/domains/{{ pve_realm }}", --output-format, json]
register: pve_realm_read
when: pve_realm_matches | length > 0
changed_when: false
no_log: true
- name: Define managed PVE realm settings
ansible.builtin.set_fact:
pve_desired:
issuer-url: "{{ pve_oidc_issuer }}"
client-id: "{{ keycloak_client_id }}"
autocreate: 1
groups-claim: groups
groups-autocreate: 1
groups-overwrite: 1
query-userinfo: 1
username-claim: preferred_username
pve_existing: "{{ (pve_realm_read.stdout | default('{}')) | from_json }}"
pve_drift: false
no_log: true
- name: Compare managed PVE fields
ansible.builtin.set_fact:
pve_drift: true
when: pve_existing.get(item.key, '') | string != item.value | string
loop: "{{ pve_desired | dict2items }}"
no_log: true
- name: Require compatible immutable username claim
ansible.builtin.assert:
that: pve_realm_matches | length == 0 or pve_existing.get('username-claim', '') == 'preferred_username'
fail_msg: 'Existing realm has a different immutable username-claim. See README migration guidance; automatic realm deletion is disabled.'
- name: Create or update PVE realm when configuration or key differs
ansible.builtin.command:
argv: "{{ ['pveum', 'realm', ('modify' if pve_realm_matches | length > 0 else 'add'), pve_realm] + ([] if pve_realm_matches | length > 0 else ['--type', 'openid', '--username-claim', 'preferred_username']) + ['--issuer-url', pve_oidc_issuer, '--client-id', keycloak_client_id, '--client-key', keycloak_client_secret, '--autocreate', '1', '--groups-claim', 'groups', '--groups-autocreate', '1', '--groups-overwrite', '1', '--query-userinfo', '1'] }}"
when: pve_realm_matches | length == 0 or pve_drift or pve_existing.get('client-key', '') != keycloak_client_secret
no_log: true
changed_when: true
- name: Clear in-memory key result
ansible.builtin.set_fact:
pve_existing: {}
pve_realm_read: {}
no_log: true