blob: cbffb40968277103fcc97db394f46a3a46f1c0e3 [file] [log] [blame]
Hung-Wei Chiu6a075af2021-09-09 22:33:06 +00001---
2# keycloak tasks/main.yml
3#
4# SPDX-FileCopyrightText: © 2021 Open Networking Foundation <support@opennetworking.org>
5# SPDX-License-Identifier: Apache-2.0
6
7- name: include OS-specific tasks
8 include_tasks: "{{ ansible_os_family }}.yml"
9
10- name: Create group for Keycloak
11 group:
12 name: "{{ keycloak_groupname }}"
13
14- name: Create user for Keycloak
15 user:
16 name: "{{ keycloak_username }}"
17 group: "{{ keycloak_groupname }}"
18 comment: "{{ keycloak_comment }}"
19 shell: "{{ keycloak_shell }}"
20 system: true
21 password_lock: true
22
23- name: Create keycloak dist directory
24 file:
25 path: "{{ keycloak_dist_dir }}"
26 owner: "{{ keycloak_username }}"
27 group: "{{ keycloak_groupname }}"
28 state: directory
29 mode: 0700
30
31- name: Download keycloak
32 get_url:
33 url: "{{ keycloak_download_url }}"
34 dest: "{{ keycloak_dist_dir }}"
35 timeout: 30
36 checksum: "{{ keycloak_checksum }}"
37 become: true
38 become_user: "{{ keycloak_username }}"
39
40- name: Extract keycloak
41 unarchive:
42 remote_src: true
43 src: "{{ keycloak_dist_dir }}/keycloak-{{ keycloak_version }}.tar.gz"
44 dest: "{{ keycloak_base_dir }}"
45 owner: "{{ keycloak_username }}"
46 group: "{{ keycloak_groupname }}"
47 creates: "{{ keycloak_base_dir }}/keycloak-{{ keycloak_version }}"
48
49- name: Link Keycloak distro to working dir
50 file:
51 state: "link"
52 src: "{{ keycloak_base_dir }}/keycloak-{{ keycloak_version }}"
53 dest: "{{ keycloak_working_dir }}"
54
55- name: Check admin account
56 command:
57 chdir: "{{ keycloak_working_dir }}/bin/"
58 cmd: >
59 ./kcadm.sh get users
Hung-Wei Chiu7260e782021-09-14 18:33:46 +000060 --server {{ keycloak_server }}/auth
Hung-Wei Chiu6a075af2021-09-09 22:33:06 +000061 --realm master --user {{ keycloak_admin_username }}
62 --password {{ keycloak_admin_password }}
63 register: kcadm_result
64 changed_when: false
65 failed_when: false
66
Hung-Wei Chiu817f8b42021-09-16 06:08:36 +000067# search proxy-address-forwarding in the following link
68# https://www.keycloak.org/docs/15.0/server_installation/#_setting-up-a-load-balancer-or-proxy
69- name: Enable HTTPS Reverse Proxy(Modify standalone.xml)
70 community.general.xml:
71 path: "/opt/keycloak/standalone/configuration/standalone.xml"
72 xpath: "/m:server/m:profile/n:subsystem/n:server/n:http-listener"
73 attribute: "proxy-address-forwarding"
74 value: "true"
75 namespaces:
76 m: "urn:jboss:domain:16.0"
77 n: "urn:jboss:domain:undertow:12.0"
78 when: (keycloak_behind_reverse_proxy is defined) and (keycloak_behind_reverse_proxy)
79 notify:
80 - "start-keycloak"
81 - "restart-keycloak"
82
83# search proxy-address-forwarding in the following link
84# https://www.keycloak.org/docs/15.0/server_installation/#_setting-up-a-load-balancer-or-proxy
85- name: Disable HTTPS Reverse Proxy(Modify standalone.xml)
86 community.general.xml:
87 path: "/opt/keycloak/standalone/configuration/standalone.xml"
88 xpath: "/m:server/m:profile/n:subsystem/n:server/n:http-listener/@proxy-address-forwarding"
89 value: "true"
90 state: absent
91 namespaces:
92 m: "urn:jboss:domain:16.0"
93 n: "urn:jboss:domain:undertow:12.0"
94 when: (keycloak_behind_reverse_proxy is not defined) or (not keycloak_behind_reverse_proxy)
95 notify:
96 - "start-keycloak"
97 - "restart-keycloak"
98
Hung-Wei Chiu6a075af2021-09-09 22:33:06 +000099- name: Create admin account
100 command:
101 chdir: "{{ keycloak_working_dir }}/bin/"
102 cmd: >
103 ./add-user-keycloak.sh
104 --realm master --user {{ keycloak_admin_username }}
105 --password {{ keycloak_admin_password }}
106 when: kcadm_result.rc != 0
107 notify:
108 - "start-keycloak"
109 - "restart-keycloak"
Hung-Wei Chiu718cd262021-09-13 18:20:21 +0000110- name: Flush handlers to start keycloak server before configuring it via API
111 meta: flush_handlers
112
113- name: Wait for Keycloak to be ready
114 uri:
115 url: "http://localhost:9990/health"
116 method: GET
117 return_content: "yes"
118 timeout: 5
119 body_format: raw
120 follow_redirects: "no"
121 status_code: 200
122 register: result
123 until: result.status == 200
124 retries: 60
125 delay: 5
126
127- name: Configure Keycloak client
128 community.general.keycloak_client:
Hung-Wei Chiu7260e782021-09-14 18:33:46 +0000129 auth_keycloak_url: "{{ keycloak_server }}/auth"
Hung-Wei Chiu718cd262021-09-13 18:20:21 +0000130 auth_realm: "{{ item.auth_realm }}"
131 auth_username: "{{ keycloak_admin_username }}"
132 auth_password: "{{ keycloak_admin_password }}"
133 client_id: "{{ item.client_id }}"
134 name: "{{ item.name }}"
135 protocol: "{{ item.protocol }}"
136 description: "{{ item.description }}"
137 attributes: "{{ item.attributes }}"
138 redirect_uris: "{{ item.redirect_uris }}"
139 protocol_mappers: "{{ item.protocol_mappers }}"
140 state: present
141 with_items: "{{ keycloak_client_settings }}"
Hung-Wei Chiu7260e782021-09-14 18:33:46 +0000142
143- name: "Create Token for Keycloak service"
144 uri:
145 url: "{{ keycloak_server }}/auth/realms/master/protocol/openid-connect/token"
146 method: POST
147 body_format: form-urlencoded
148 body:
149 username: "{{ keycloak_admin_username }}"
150 password: "{{ keycloak_admin_password }}"
151 grant_type: "password"
152 client_id: "admin-cli"
153 register: keycloak_token
154
155- name: "Get existing LDAP configuration"
156 uri:
157 url: "{{ keycloak_admin_api }}/components?type=org.keycloak.storage.UserStorageProvider"
158 method: GET
159 headers:
160 Accept: "application/json"
161 Authorization: "Bearer {{ keycloak_token.json.access_token }}"
162 register: keycloak_components_list
163
164- name: Check if the Keycloak already has the LDAP configuration
165 set_fact:
166 ldap_id: "{{ item.id }}"
167 with_items: "{{ keycloak_components_list.json }}"
168 when: item.name == "ldap"
169
170- name: Generate a local json file for LDAP configuration
171 become: false
172 delegate_to: localhost
173 template:
174 src: "ldap.config.j2"
175 dest: "/tmp/ldap.config"
176 mode: "0600"
177 changed_when: false
178
179- name: "Create LDAP Provider if not exist"
180 uri:
181 url: "{{ keycloak_admin_api }}/components"
182 method: POST
183 src: /tmp/ldap.config
184 status_code: [201]
185 headers:
186 Content-Type: application/json
187 Authorization: "Bearer {{ keycloak_token.json.access_token }}"
188 register: keycloak_create_ldap_response
189 when: ldap_id is not defined
190
191- name: "Update LDAP Provider if exist"
192 uri:
193 url: "{{ keycloak_admin_api }}/components/{{ ldap_id }}"
194 method: PUT
195 src: /tmp/ldap.config
196 status_code: [204]
197 headers:
198 Content-Type: application/json
199 Authorization: "Bearer {{ keycloak_token.json.access_token }}"
200 when: ldap_id is defined
201
202- name: Update LDAP_ID with new created LDAP components
203 set_fact:
204 ldap_id: "{{ keycloak_create_ldap_response.location | basename }}"
205 when: ldap_id is not defined
206
207- name: Generate a local json file for LDAP mapper configuration
208 become: false
209 delegate_to: localhost
210 template:
211 src: "{{ item }}.j2"
212 dest: "/tmp/{{ item }}"
213 mode: "0600"
214 with_items:
215 - ldap.mapper.group
216 - keycloak.event.config
217 changed_when: false
218
219- name: Create LDAP mapper from local json configuraiton
220 uri:
221 url: "{{ keycloak_admin_api }}/components/"
222 method: POST
223 src: "/tmp/{{ item }}"
224 status_code: [201]
225 headers:
226 Content-Type: application/json
227 Authorization: "Bearer {{ keycloak_token.json.access_token }}"
228 with_items:
229 - ldap.mapper.group
230
231- name: Create LDAP mapper from local json configuraiton
232 uri:
233 url: "{{ keycloak_admin_api }}/events/config"
234 method: PUT
235 src: "/tmp/keycloak.event.config"
236 status_code: [204]
237 headers:
238 Content-Type: application/json
239 Authorization: "Bearer {{ keycloak_token.json.access_token }}"
240
241- name: Remove local LDAP json file
242 delegate_to: localhost
243 file:
244 path: "/tmp/{{ item }}"
245 state: absent
246 with_items:
247 - ldap.config
248 - ldap.mapper.group
249 - keycloak.event.config
250 changed_when: false