community.general.keycloak_user_rolemapping 模块 – 允许使用 Keycloak API 管理 Keycloak 用户角色映射

注意

此模块是 community.general 集合(版本 10.1.0)的一部分。

如果您正在使用 ansible 包,您可能已经安装了此集合。它不包含在 ansible-core 中。要检查是否已安装,请运行 ansible-galaxy collection list

要安装它,请使用:ansible-galaxy collection install community.general

要在剧本中使用它,请指定:community.general.keycloak_user_rolemapping

community.general 5.7.0 中新增

概要

  • 此模块允许您使用 Keycloak REST API 添加、删除或修改 Keycloak user_rolemapping。它需要通过 OpenID Connect 访问 REST API;连接的用户和使用的客户端必须具有必要的访问权限。在默认的 Keycloak 安装中,admin-cli 和管理员用户可以使用,具有根据您的需求定制范围的单独客户端定义以及具有预期角色的用户也可以使用。

  • 模块选项的名称是 Keycloak API 及其文档(网址为 https://keycloak.java.net.cn/docs-api/8.0/rest-api/index.html)中找到的驼峰式名称的蛇形化版本。

  • 属性在 Keycloak API 中是多值的。所有属性都是单个值的列表,并且将由此模块以这种方式返回。您可以在调用模块时传递单个属性值,这将被转换为适合 API 的列表。

  • 在更新 user_rolemapping 时,请尽可能向模块提供角色 ID。这会删除对 API 的查找,以将名称转换为角色 ID。

参数

参数

注释

auth_client_id

字符串

OpenID Connect client_id 用于向 API 验证身份。

默认值: "admin-cli"

auth_client_secret

字符串

auth_client_id 结合使用的客户端密钥(如果需要)。

auth_keycloak_url

别名:url

字符串 / 必需

Keycloak 实例的 URL。

auth_password

别名:password

字符串

用于 API 访问的身份验证密码。

auth_realm

字符串

Keycloak 域名称,用于向 API 验证身份。

auth_username

别名:username

字符串

用于 API 访问的身份验证用户名。

cid

字符串

要映射的客户端的 ID。

此参数不是更新或删除角色映射所必需的,但提供它可以减少所需的 API 调用次数。

client_id

字符串

要映射的客户端的名称(与 cid 不同)。

如果未提供 cid,则此参数是必需的(可以用 cid 代替以减少必须进行的 API 调用次数)。

connection_timeout

整数

在 community.general 4.5.0 中添加

控制到 Keycloak API 的 HTTP 连接超时时间(以秒为单位)。

默认值: 10

http_agent

字符串

在 community.general 5.4.0 中添加

配置 HTTP User-Agent 标头。

默认值: "Ansible"

realm

字符串

此 role_representation 所在的 Keycloak 域。

默认值: "master"

roles

列表 / 元素=字典

要映射到用户的角色。

id

字符串

此 role_representation 的唯一标识符。

此参数不是更新或删除 role_representation 所必需的,但提供它可以减少所需的 API 调用次数。

name

字符串

角色表示的名称。

此参数仅在创建或更新 role_representation 时是必需的。

service_account_user_client_id

字符串

要映射的服务帐户用户的客户端 ID。

此参数不是更新或删除角色映射所必需的,但提供它可以减少所需的 API 调用次数。

state

字符串

user_rolemapping 的状态。

present 上,如果 user_rolemapping 尚不存在,则会创建它,或者使用您提供的参数更新它。

absent 上,如果 user_rolemapping 存在,则会删除它。

选择

  • "present" ← (默认)

  • "absent"

target_username

字符串

角色映射到的用户的用户名。

此参数不是必需的(可以用 uid 代替,以减少 API 调用)。

token

字符串

在 community.general 3.0.0 中添加

用于 Keycloak API 的身份验证令牌。

uid

字符串

要映射的用户的 ID。

此参数不是更新或删除角色映射所必需的,但提供它可以减少所需的 API 调用次数。

validate_certs

布尔值

验证 TLS 证书(请勿在生产环境中禁用此选项)。

选择

  • false

  • true ← (默认)

属性

属性

支持

描述

check_mode

支持:完全

可以在 check_mode 中运行并返回更改状态预测,而无需修改目标。

diff_mode

支持:完全

在差异模式下,当启用 check_mode 时,将返回已更改的内容(或可能需要更改的内容)的详细信息。

示例

- name: Map a client role to a user, authentication with credentials
  community.general.keycloak_user_rolemapping:
    realm: MyCustomRealm
    auth_client_id: admin-cli
    auth_keycloak_url: https://auth.example.com/auth
    auth_realm: master
    auth_username: USERNAME
    auth_password: PASSWORD
    state: present
    client_id: client1
    user_id: user1Id
    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
  community.general.keycloak_user_rolemapping:
    realm: MyCustomRealm
    auth_client_id: admin-cli
    auth_keycloak_url: https://auth.example.com/auth
    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
  community.general.keycloak_user_rolemapping:
    realm: MyCustomRealm
    auth_client_id: admin-cli
    auth_keycloak_url: https://auth.example.com/auth
    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
  community.general.keycloak_user_rolemapping:
    realm: MyCustomRealm
    auth_client_id: admin-cli
    auth_keycloak_url: https://auth.example.com/auth
    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

返回值

常见的返回值已在此处记录 此处,以下是此模块特有的字段

描述

end_state

字典

模块执行后客户端角色映射的表示。

示例已截断。

返回: 成功时

示例: {"adminUrl": "http://www.example.com/admin_url", "attributes": {"request.object.signature.alg": "RS256"}}

existing

字典

现有客户端角色映射的表示。

示例已截断。

返回: 始终

示例: {"adminUrl": "http://www.example.com/admin_url", "attributes": {"request.object.signature.alg": "RS256"}}

msg

字符串

关于采取了什么操作的消息。

返回: 始终

示例: "角色 role1 已分配给 用户 user1。"

proposed

字典

提议的客户端角色映射的表示。

返回: 始终

示例: {"clientId": "test"}

作者

  • Dušan Marković (@bratwurzt)