diff --git a/changelogs/fragments/11256-fix-keycloak-roles-mapping.yml b/changelogs/fragments/11256-fix-keycloak-roles-mapping.yml new file mode 100644 index 0000000000..be567ca833 --- /dev/null +++ b/changelogs/fragments/11256-fix-keycloak-roles-mapping.yml @@ -0,0 +1,4 @@ +--- +bugfixes: + - keycloak_user_rolemapping module - fixed crash when assigning roles to users + without an existing role (https://github.com/ansible-collections/community.general/issues/10960, https://github.com/ansible-collections/community.general/pull/11256). diff --git a/plugins/modules/keycloak_user_rolemapping.py b/plugins/modules/keycloak_user_rolemapping.py index 2e5609e08a..b2bac72106 100644 --- a/plugins/modules/keycloak_user_rolemapping.py +++ b/plugins/modules/keycloak_user_rolemapping.py @@ -117,7 +117,7 @@ EXAMPLES = r""" auth_username: USERNAME auth_password: PASSWORD state: present - user_id: user1Id + uid: user_uid roles: - name: role_name1 id: role_id1 @@ -135,7 +135,7 @@ EXAMPLES = r""" auth_password: PASSWORD state: present client_id: client1 - user_id: user1Id + uid: user_uid roles: - name: role_name1 id: role_id1 @@ -351,7 +351,9 @@ def main(): # Fetch missing role_name else: if cid is None: - role["name"] = kc.get_realm_user_rolemapping_by_id(uid=uid, rid=role.get("id"), realm=realm)["name"] + role_rep = kc.get_realm_user_rolemapping_by_id(uid=uid, rid=role.get("id"), realm=realm) + if role_rep is not None: + role["name"] = role_rep["name"] else: role["name"] = kc.get_client_user_rolemapping_by_id( uid=uid, cid=cid, rid=role.get("id"), realm=realm