# keycloak_user_rolemapping – Allows administration of Keycloak user_rolemapping with the Keycloak API — Keycloak Ansible Collection documentation

# keycloak_user_rolemapping – Allows administration of Keycloak user_rolemapping with the Keycloak API

Note

This module is part of the middleware_automation.keycloak collection.

It is not included in `ansible-core`. To check whether it is installed, run `ansible-galaxy collection list`.

To install it, use: `ansible-galaxy collection install middleware_automation.keycloak`.

To use it in a playbook, specify: `middleware_automation.keycloak.keycloak_user_rolemapping`.

New in middleware_automation.keycloak 3.0.0

## Synopsis

- This module allows you to add, remove or modify Keycloak user_rolemapping with the Keycloak REST API. It requires access to the REST API using OpenID Connect; the user connecting and the client being used must have the requisite access rights. In a default Keycloak installation, admin-cli and an admin user would work, as would a separate client definition with the scope tailored to your needs and a user having the expected roles.

- The names of module options are snake_cased versions of the camelCase ones found in the Keycloak API and its documentation at https://www.keycloak.org/docs-api/latest/rest-api/index.html.

- Attributes are multi-valued in the Keycloak API. All attributes are lists of individual values and are returned that way by this module. You may pass single values for attributes when calling the module, and this is translated into a list suitable for the API.

- When updating a user_rolemapping, where possible provide the role ID to the module. This removes a lookup to the API to translate the name into the role ID.

## Parameters

Parameter | Comments
--- | ---
auth_client_id string | OpenID Connect client_id to authenticate to the API with. Default: `"admin-cli"`
auth_client_secret string | Client Secret to use in conjunction with auth_client_id (if required).
auth_keycloak_url aliases: url string / required | URL to the Keycloak instance.
auth_password aliases: password string | Password to authenticate for API access with.
auth_realm string | Keycloak realm name to authenticate to for API access.
auth_username aliases: username string | Username to authenticate for API access with.
cid string | ID of the client whose role is to be mapped. This parameter is not required for updating or deleting the rolemapping but providing it reduces the number of API calls required. If neither `cid` nor `client_id` is specified, a realm role is mapped instead.
client_id string | Name of the client (different than `cid`) whose role is to be mapped. This parameter is required if `cid` is not provided (can be replaced by `cid` to reduce the number of API calls that must be made). If neither `cid` nor `client_id` is specified, a realm role is mapped instead.
connection_timeout integer | Controls the HTTP connections timeout period (in seconds) to Keycloak API. Default: `10`
http_agent string | Configures the HTTP User-Agent header. Default: `"Ansible"`
realm string | They Keycloak realm under which this role_representation resides. Default: `"master"`
refresh_token string | Authentication refresh token for Keycloak API.
roles list / elements=dictionary | Roles to be mapped to the user.
id string | The unique identifier for this role_representation. This parameter is not required for updating or deleting a role_representation but providing it reduces the number of API calls required.
name string | Name of the role representation. This parameter is required only when creating or updating the role_representation.
service_account_user_client_id string | Client ID of the service-account-user to be mapped. This parameter is not required for updating or deleting the rolemapping but providing it reduces the number of API calls required.

state

string

State of the user_rolemapping.

On `present`, the user_rolemapping is created if it does not yet exist, or updated with the parameters you provide.

On `absent`, the user_rolemapping is removed if it exists.

Choices:

- `"present"` ← (default)

- `"absent"`

target_username string | Username of the user roles are mapped to. This parameter is not required (can be replaced by uid for less API call).
token string | Authentication token for Keycloak API.
uid string | ID of the user to be mapped. This parameter is not required for updating or deleting the rolemapping but providing it reduces the number of API calls required.

validate_certs

boolean

Verify TLS certificates (do not disable this in production).

Choices:

- `false`

- `true` ← (default)

## Attributes

Attribute | Support | Description
--- | --- | ---
action_group | Action group: middleware_automation.keycloak.keycloak added in middleware_automation.keycloak 3.0.0 | Use `group/middleware_automation.keycloak.keycloak` in `module_defaults` to set defaults for this module.
check_mode | Support: full | Can run in `check_mode` and return changed status prediction without modifying target.
diff_mode | Support: full | Will return details on what has changed (or possibly needs changing in `check_mode`), when in diff mode.

## Examples

    - name: Map a realm role to a user, authentication with credentials
      middleware_automation.keycloak.keycloak_user_rolemapping:
        realm: MyCustomRealm
        auth_client_id: admin-cli
        auth_keycloak_url: https://auth.example.com
        auth_realm: master
        auth_username: USERNAME
        auth_password: PASSWORD
        state: present
        uid: user_uid
        roles:
          - name: role_name1
            id: role_id1
          - name: role_name2
            id: role_id2
      delegate_to: localhost

    - name: Map a client role to a user, authentication with credentials
      middleware_automation.keycloak.keycloak_user_rolemapping:
        realm: MyCustomRealm
        auth_client_id: admin-cli
        auth_keycloak_url: https://auth.example.com
        auth_realm: master
        auth_username: USERNAME
        auth_password: PASSWORD
        state: present
        client_id: client1
        uid: user_uid
        roles:
          - name: role_name1
            id: role_id1
          - name: role_name2
            id: role_id2
      delegate_to: localhost

    - name: Map a client role to a service account user for a client, authentication with credentials
      middleware_automation.keycloak.keycloak_user_rolemapping:
        realm: MyCustomRealm
        auth_client_id: admin-cli
        auth_keycloak_url: https://auth.example.com
        auth_realm: master
        auth_username: USERNAME
        auth_password: PASSWORD
        state: present
        client_id: client1
        service_account_user_client_id: clientIdOfServiceAccount
        roles:
          - name: role_name1
            id: role_id1
          - name: role_name2
            id: role_id2
      delegate_to: localhost

    - name: Map a client role to a user, authentication with token
      middleware_automation.keycloak.keycloak_user_rolemapping:
        realm: MyCustomRealm
        auth_client_id: admin-cli
        auth_keycloak_url: https://auth.example.com
        token: TOKEN
        state: present
        client_id: client1
        target_username: user1
        roles:
          - name: role_name1
            id: role_id1
          - name: role_name2
            id: role_id2
      delegate_to: localhost

    - name: Unmap client role from a user
      middleware_automation.keycloak.keycloak_user_rolemapping:
        realm: MyCustomRealm
        auth_client_id: admin-cli
        auth_keycloak_url: https://auth.example.com
        auth_realm: master
        auth_username: USERNAME
        auth_password: PASSWORD
        state: absent
        client_id: client1
        uid: 70e3ae72-96b6-11e6-9056-9737fd4d0764
        roles:
          - name: role_name1
            id: role_id1
          - name: role_name2
            id: role_id2
      delegate_to: localhost

## Return Values

Common return values are documented here, the following are the fields unique to this module:

Key | Description
--- | ---
end_state dictionary | Representation of client role mapping after module execution. The sample is truncated. Returned: on success Sample: `{"adminUrl": "http://www.example.com/admin_url", "attributes": {"request.object.signature.alg": "RS256"}}`
existing dictionary | Representation of existing client role mapping. The sample is truncated. Returned: always Sample: `{"adminUrl": "http://www.example.com/admin_url", "attributes": {"request.object.signature.alg": "RS256"}}`
msg string | Message as to what action was taken. Returned: always Sample: `"Role role1 assigned to user user1."`
proposed dictionary | Representation of proposed client role mapping. Returned: always Sample: `{"clientId": "test"}`

### Authors

- Dušan Marković (@bratwurzt)

- Ivan Kokalović (@koke1997)