community.general.keycloak_client_rolescope module – Allows administration of Keycloak client roles scope to restrict the usage of certain roles to a other specific client applications.

Note

This module is part of the community.general collection (version 10.1.0).

You might already have this collection installed if you are using the ansible package. 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 community.general.

To use it in a playbook, specify: community.general.keycloak_client_rolescope.

New in community.general 8.6.0

Synopsis

  • This module allows you to add or remove Keycloak roles from clients scope via the Keycloak REST API. It requires access to the REST API via 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.

  • Client client_id must have full_scope_allowed set to false.

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

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.

client_id

string / required

Roles provided in role_names while be added to this client scope.

client_scope_id

string

If the role_names are client role, the client ID under which it resides.

If this parameter is absent, the roles are considered a realm role.

connection_timeout

integer

added in community.general 4.5.0

Controls the HTTP connections timeout period (in seconds) to Keycloak API.

Default: 10

http_agent

string

added in community.general 5.4.0

Configures the HTTP User-Agent header.

Default: "Ansible"

realm

string

The Keycloak realm under which clients resides.

Default: "master"

role_names

list / elements=string / required

Names of roles to manipulate.

If client_scope_id is present, all roles must be under this client.

If client_scope_id is absent, all roles must be under the realm.

state

string

State of the role mapping.

On present, all roles in role_names will be mapped if not exists yet.

On absent, all roles mapping in role_names will be removed if it exists.

Choices:

  • "present" ← (default)

  • "absent"

token

string

added in community.general 3.0.0

Authentication token for Keycloak API.

validate_certs

boolean

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

Choices:

  • false

  • true ← (default)

Attributes

Attribute

Support

Description

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: Add roles to public client scope
  community.general.keycloak_client_rolescope:
    auth_keycloak_url: https://auth.example.com/auth
    auth_realm: master
    auth_username: USERNAME
    auth_password: PASSWORD
    realm: MyCustomRealm
    client_id: frontend-client-public
    client_scope_id: backend-client-private
    role_names:
    - backend-role-admin
    - backend-role-user

- name: Remove roles from public client scope
  community.general.keycloak_client_rolescope:
    auth_keycloak_url: https://auth.example.com/auth
    auth_realm: master
    auth_username: USERNAME
    auth_password: PASSWORD
    realm: MyCustomRealm
    client_id: frontend-client-public
    client_scope_id: backend-client-private
    role_names:
    - backend-role-admin
    state: absent

- name: Add realm roles to public client scope
  community.general.keycloak_client_rolescope:
    auth_keycloak_url: https://auth.example.com/auth
    auth_realm: master
    auth_username: USERNAME
    auth_password: PASSWORD
    realm: MyCustomRealm
    client_id: frontend-client-public
    role_names:
    - realm-role-admin
    - realm-role-user

Return Values

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

Key

Description

end_state

list / elements=dictionary

Representation of role role scope after module execution.

Returned: on success

Sample: [{"clientRole": false, "composite": false, "containerId": "MyCustomRealm", "id": "47293104-59a6-46f0-b460-2e9e3c9c424c", "name": "backend-role-admin"}, {"clientRole": false, "composite": false, "containerId": "MyCustomRealm", "id": "39c62a6d-542c-4715-92d2-41021eb33967", "name": "backend-role-user"}]

msg

string

Message as to what action was taken.

Returned: always

Sample: "Client role scope for frontend-client-public has been updated"

Authors

  • Andre Desrosiers (@desand01)