- name: Configure Keycloak through verified HTTPS no_log: true module_defaults: ansible.builtin.uri: validate_certs: true ca_path: "{{ system_ca_bundle }}" use_proxy: false follow_redirects: none unredirected_headers: [Authorization] timeout: 30 block: - name: Validate optional logout redirect before API changes ansible.builtin.assert: that: pve_logout_redirect_uri in pve_redirect_uris when: pve_logout_enabled | default(false) | bool - name: Obtain short-lived admin token ansible.builtin.uri: url: "{{ keycloak_base_url }}/realms/{{ keycloak_admin_realm }}/protocol/openid-connect/token" method: POST body_format: form-urlencoded body: grant_type: password client_id: "{{ keycloak_admin_client_id }}" username: "{{ keycloak_admin_username }}" password: "{{ keycloak_admin_password }}" status_code: 200 register: kc_token until: kc_token.status | default(0) == 200 retries: 20 delay: 3 changed_when: false - name: Define admin headers and realm settings ansible.builtin.set_fact: kc_headers: Authorization: "Bearer {{ kc_token.json.access_token }}" kc_api: "{{ keycloak_base_url }}/admin/realms/{{ keycloak_realm }}" kc_realm_desired: realm: "{{ keycloak_realm }}" enabled: true ssoSessionIdleTimeout: "{{ keycloak_sso_idle | int }}" ssoSessionMaxLifespan: "{{ keycloak_sso_max | int }}" clientSessionIdleTimeout: "{{ keycloak_client_idle | int }}" clientSessionMaxLifespan: "{{ keycloak_client_max | int }}" - name: Read realm ansible.builtin.uri: url: "{{ kc_api }}" headers: "{{ kc_headers }}" status_code: [200, 404] register: kc_realm_current - name: Create realm if missing ansible.builtin.uri: url: "{{ keycloak_base_url }}/admin/realms" headers: "{{ kc_headers }}" method: POST body_format: json body: "{{ kc_realm_desired }}" status_code: 201 when: kc_realm_current.status == 404 changed_when: true - name: Determine realm drift ansible.builtin.set_fact: kc_realm_drift: "{{ (kc_realm_current.json | default({}) | combine(kc_realm_desired)) != (kc_realm_current.json | default({})) }}" - name: Update only managed realm fields ansible.builtin.uri: url: "{{ kc_api }}" headers: "{{ kc_headers }}" method: PUT body_format: json body: "{{ kc_realm_desired }}" status_code: 204 when: kc_realm_current.status == 200 and kc_realm_drift changed_when: true - name: Ensure each top-level group exists ansible.builtin.include_tasks: keycloak_group.yml loop: "{{ pve_roles }}" loop_control: loop_var: kc_group_name - name: Find client by exact ID ansible.builtin.uri: url: "{{ kc_api }}/clients?clientId={{ keycloak_client_id | urlencode }}" headers: "{{ kc_headers }}" register: kc_clients - name: Select exact match ansible.builtin.set_fact: kc_matches: "{{ kc_clients.json | selectattr('clientId', 'equalto', keycloak_client_id) | list }}" - name: Require unique client ansible.builtin.assert: that: kc_matches | length <= 1 - name: Define client settings ansible.builtin.set_fact: kc_client_desired: clientId: "{{ keycloak_client_id }}" protocol: openid-connect enabled: true publicClient: false bearerOnly: false clientAuthenticatorType: client-secret standardFlowEnabled: true implicitFlowEnabled: false directAccessGrantsEnabled: false serviceAccountsEnabled: false redirectUris: "{{ pve_redirect_uris }}" webOrigins: [] kc_session_attributes: client.session.idle.timeout: "{{ keycloak_client_idle | string }}" client.session.max.lifespan: "{{ keycloak_client_max | string }}" - name: Prepare verified logout redirect chain ansible.builtin.include_tasks: logout_plan.yml when: pve_logout_enabled | default(false) | bool - name: Register explicit post-logout return address ansible.builtin.set_fact: kc_session_attributes: "{{ kc_session_attributes | combine({'post.logout.redirect.uris': logout_plan.allowed_redirects | join('##')}) }}" when: pve_logout_enabled | default(false) | bool - name: Create confidential PVE client ansible.builtin.uri: url: "{{ kc_api }}/clients" headers: "{{ kc_headers }}" method: POST body_format: json body: "{{ kc_client_desired | combine({'secret': keycloak_client_secret, 'attributes': kc_session_attributes, 'defaultClientScopes': ['profile', 'email']}) }}" status_code: 201 when: kc_matches | length == 0 changed_when: true - name: Read client after creation ansible.builtin.uri: url: "{{ kc_api }}/clients?clientId={{ keycloak_client_id | urlencode }}" headers: "{{ kc_headers }}" register: kc_clients - name: Store client representation ansible.builtin.set_fact: kc_client_current: "{{ kc_clients.json | selectattr('clientId', 'equalto', keycloak_client_id) | first }}" - name: Read actual client secret (not masked representation) ansible.builtin.uri: url: "{{ kc_api }}/clients/{{ kc_client_current.id }}/client-secret" headers: "{{ kc_headers }}" register: kc_secret - name: Merge managed attributes preserving other client settings ansible.builtin.set_fact: kc_client_update: "{{ kc_client_desired | combine({'attributes': (kc_client_current.attributes | default({}) | combine(kc_session_attributes))}) }}" - name: Update client only on drift or secret rotation ansible.builtin.uri: url: "{{ kc_api }}/clients/{{ kc_client_current.id }}" headers: "{{ kc_headers }}" method: PUT body_format: json body: "{{ kc_client_update | combine({'secret': keycloak_client_secret}) }}" status_code: 204 when: (kc_client_current | combine(kc_client_update)) != kc_client_current or kc_secret.json.value != keycloak_client_secret changed_when: true - name: Ensure username claim independent of inherited client scopes ansible.builtin.include_tasks: keycloak_mapper.yml vars: kc_mapper_desired: name: pve-preferred-username protocol: openid-connect protocolMapper: oidc-usermodel-property-mapper consentRequired: false config: user.attribute: username claim.name: preferred_username jsonType.label: String id.token.claim: 'true' access.token.claim: 'true' userinfo.token.claim: 'true' - name: Ensure flat group membership mapper ansible.builtin.include_tasks: keycloak_mapper.yml vars: kc_mapper_desired: name: groups protocol: openid-connect protocolMapper: oidc-group-membership-mapper consentRequired: false config: claim.name: groups full.path: 'false' id.token.claim: 'true' access.token.claim: 'true' userinfo.token.claim: 'true' always: - name: Clear authentication facts ansible.builtin.set_fact: kc_headers: {} kc_token: {} kc_secret: {} kc_client_current: {} kc_clients: {} kc_matches: []