main.yml 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. ---
  2. - name: is there already a subscription?
  3. kubernetes.core.k8s_info:
  4. kubeconfig: "{{ ansible_facts['user_dir'] }}/kubeconfig-ocp4"
  5. validate_certs: no
  6. api_version: operators.coreos.com/v1alpha1
  7. kind: subscription
  8. namespace: openshift-sso
  9. name: rhsso
  10. register: sub
  11. - name: oi - is there already an operator?
  12. kubernetes.core.k8s_info:
  13. kubeconfig: "{{ ansible_facts['user_dir'] }}/kubeconfig-ocp4"
  14. validate_certs: no
  15. api_version: operators.coreos.com/v1alpha1
  16. kind: clusterserviceversion
  17. namespace: openshift-sso
  18. name: "{{ sub.resources[0].status.installedCSV }}"
  19. register: csv
  20. when:
  21. - sub.resources is defined
  22. - (sub.resources | length) > 0
  23. - sub.resources[0].spec.name == "rhsso-operator"
  24. - sub.resources[0].status.installedCSV is defined
  25. - name: assert csv is there
  26. set_fact:
  27. csv_is_there: true
  28. - name: reset the above fact if not the case
  29. set_fact:
  30. csv_is_there: false
  31. when: (csv is not defined) or (csv.resources is not defined) or (csv.resources | length == 0) or (csv.resources[0].status.phase != "Succeeded")
  32. - name: is there a rhsso pod?
  33. kubernetes.core.k8s_info:
  34. kubeconfig: "{{ ansible_facts['user_dir'] }}/kubeconfig-ocp4"
  35. validate_certs: no
  36. api_version: v1
  37. kind: pod
  38. namespace: openshift-sso
  39. label_selectors:
  40. - app = keycloak
  41. - component = keycloak
  42. register: rhsso_pod
  43. - name: assert central is there
  44. set_fact:
  45. rhsso_is_there: true
  46. - name: reset the above fact if not the case
  47. set_fact:
  48. rhsso_is_there: false
  49. when: (rhsso_pod is not defined) or (rhsso_pod.resources is not defined) or (rhsso_pod.resources | length == 0) or (rhsso_pod.resources[0].status.phase != "Running")
  50. #- name: is there anyone home at the api endpoint?
  51. - name: create ns, og, and sub
  52. kubernetes.core.k8s:
  53. kubeconfig: "{{ ansible_facts['user_dir'] }}/kubeconfig-ocp4"
  54. validate_certs: no
  55. template: templates/rhsso-ns-and-sub.yml
  56. when: not csv_is_there
  57. - name: wait until csv is there and ready
  58. kubernetes.core.k8s_info:
  59. kubeconfig: "{{ ansible_facts['user_dir'] }}/kubeconfig-ocp4"
  60. validate_certs: no
  61. api_version: operators.coreos.com/v1alpha1
  62. kind: clusterserviceversion
  63. name: rhsso-operator.{{ sso_z }}
  64. namespace: openshift-sso
  65. when: not csv_is_there
  66. register: csv
  67. until: (csv.resources | length) > 0 and csv.resources[0].status.phase == "Succeeded"
  68. retries: 30
  69. delay: 5
  70. - name: deploy cr
  71. kubernetes.core.k8s:
  72. kubeconfig: "{{ ansible_facts['user_dir'] }}/kubeconfig-ocp4"
  73. validate_certs: no
  74. src: files/rhsso-cr.yml
  75. when: not rhsso_is_there
  76. - name: wait for rhsso pod to be up
  77. kubernetes.core.k8s_info:
  78. kubeconfig: "{{ ansible_facts['user_dir'] }}/kubeconfig-ocp4"
  79. validate_certs: no
  80. api_version: v1
  81. kind: pod
  82. namespace: openshift-sso
  83. label_selectors:
  84. - app = keycloak
  85. - component = keycloak
  86. when: not rhsso_is_there
  87. register: rhsso_pod
  88. until: (rhsso_pod.resources | length) > 0 and rhsso_pod.resources[0].status.phase == "Running"
  89. retries: 30
  90. delay: 5
  91. - name: look up route
  92. kubernetes.core.k8s_info:
  93. kubeconfig: "{{ ansible_facts['user_dir'] }}/kubeconfig-ocp4"
  94. validate_certs: no
  95. api_version: route.openshift.io/v1
  96. kind: route
  97. namespace: openshift-sso
  98. name: keycloak
  99. register: rhsso_route
  100. - assert:
  101. that: rhsso_route.resources | length > 0
  102. fail_msg: "ERROR: RHSSO seems to be there, but route is not present."
  103. success_msg: "OK, got route to RHSSO."
  104. - name: store route hostname as fact
  105. set_fact:
  106. rhsso_ep: "{{ rhsso_route.resources[0].spec.host }}"
  107. - name: store the api endpoint in a file
  108. copy:
  109. dest: "{{ ansible_facts['user_dir'] }}/rhsso-endpoint"
  110. content: "https://{{ rhsso_ep }}/auth/admin"
  111. - name: look up secret
  112. kubernetes.core.k8s_info:
  113. kubeconfig: "{{ ansible_facts['user_dir'] }}/kubeconfig-ocp4"
  114. validate_certs: no
  115. api_version: v1
  116. kind: secret
  117. namespace: openshift-sso
  118. name: credential-rhsso
  119. register: rhsso_secret
  120. - assert:
  121. that: rhsso_secret.resources | length > 0
  122. fail_msg: "ERROR: RHSSO seems to be there, but auth secret is not present."
  123. success_msg: "OK, got secret to RHSSO."
  124. - name: store rhsso pass as fact
  125. set_fact:
  126. rhsso_pass: "{{ rhsso_secret.resources[0].data.ADMIN_PASSWORD }}"
  127. - name: store the password in a file
  128. copy:
  129. dest: "{{ ansible_facts['user_dir'] }}/rhsso-password"
  130. content: "{{ rhsso_pass | string | b64decode }}"
  131. - name: (re)apply the realm if necessary
  132. kubernetes.core.k8s:
  133. kubeconfig: "{{ ansible_facts['user_dir'] }}/kubeconfig-ocp4"
  134. validate_certs: no
  135. src: files/rhsso-realm.yml
  136. # TODO: wait for realm to reconcile
  137. - name: (re)apply the client if necessary
  138. kubernetes.core.k8s:
  139. kubeconfig: "{{ ansible_facts['user_dir'] }}/kubeconfig-ocp4"
  140. validate_certs: no
  141. src: files/rhsso-client.yml
  142. apply: yes
  143. - name: make sure rhsso pod is ready
  144. kubernetes.core.k8s_info:
  145. kubeconfig: "{{ ansible_facts['user_dir'] }}/kubeconfig-ocp4"
  146. validate_certs: no
  147. api_version: v1
  148. kind: pod
  149. namespace: openshift-sso
  150. label_selectors:
  151. - app = keycloak
  152. - component = keycloak
  153. when: not rhsso_is_there
  154. register: rhsso_pod
  155. until: (rhsso_pod.resources | length) > 0 and rhsso_pod.resources[0].status.containerStatuses[0].ready
  156. retries: 60
  157. delay: 5
  158. - name: get an auth token
  159. uri:
  160. method: POST
  161. return_content: true
  162. validate_certs: false
  163. url: "https://{{ rhsso_ep }}/auth/realms/master/protocol/openid-connect/token"
  164. headers:
  165. Accept: application/json
  166. Content-Type: application/x-www-form-urlencoded
  167. body_format: json
  168. body: "client_id=admin-cli&username=admin&password={{ rhsso_pass | string | b64decode }}&grant_type=password"
  169. register: rhsso_token_rsp
  170. - assert:
  171. that: rhsso_token_rsp.json is defined and rhsso_token_rsp.json.access_token is defined
  172. fail_msg: "ERROR: Failed to obtain authentication token from RHSSO."
  173. success_msg: "OK, got auth token; proceeding."
  174. - name: store the token as a fact
  175. set_fact:
  176. rhsso_token: "{{ rhsso_token_rsp.json.access_token }}"
  177. - name: get existing role list
  178. uri:
  179. method: GET
  180. return_content: true
  181. validate_certs: false
  182. url: "https://{{ rhsso_ep }}/auth/admin/realms/rhacs/roles"
  183. headers:
  184. Authorization: Bearer {{ rhsso_token }}
  185. Accept: application/json
  186. register: rhsso_roles_raw
  187. - name: store existing roles as a list
  188. set_fact:
  189. rhsso_roles: "{{ rhsso_roles_raw.json | items2dict(key_name='name', value_name='id') }}"
  190. - name: create roles
  191. uri:
  192. method: POST
  193. return_content: true
  194. validate_certs: false
  195. url: "https://{{ rhsso_ep }}/auth/admin/realms/rhacs/roles"
  196. headers:
  197. Authorization: Bearer {{ rhsso_token }}
  198. Accept: application/json
  199. Content-Type: application/json
  200. body_format: json
  201. body: '{"name": "{{ item | string }}"}'
  202. status_code:
  203. - 200
  204. - 201
  205. loop:
  206. - admins
  207. - analysts
  208. - cicd
  209. - reporters
  210. - developers
  211. when: item not in rhsso_roles.keys()
  212. - name: get existing group list
  213. uri:
  214. method: GET
  215. return_content: true
  216. validate_certs: false
  217. url: "https://{{ rhsso_ep }}/auth/admin/realms/rhacs/groups"
  218. headers:
  219. Authorization: Bearer {{ rhsso_token }}
  220. Accept: application/json
  221. register: rhsso_groups_raw
  222. - name: store existing groups as a list
  223. set_fact:
  224. rhsso_groups: "{{ rhsso_groups_raw.json | items2dict(key_name='name', value_name='id') }}"
  225. - name: create groups
  226. uri:
  227. method: POST
  228. return_content: true
  229. validate_certs: false
  230. url: "https://{{ rhsso_ep }}/auth/admin/realms/rhacs/groups"
  231. headers:
  232. Authorization: Bearer {{ rhsso_token }}
  233. Accept: application/json
  234. Content-Type: application/json
  235. body_format: json
  236. body: '{"name": "{{ item | string }}" }'
  237. status_code:
  238. - 200
  239. - 201
  240. loop:
  241. - admins
  242. - analysts
  243. - cicd
  244. - reporters
  245. - developers
  246. when: item not in rhsso_groups.keys()
  247. - name: retrieve group list again
  248. uri:
  249. method: GET
  250. return_content: true
  251. validate_certs: false
  252. url: "https://{{ rhsso_ep }}/auth/admin/realms/rhacs/groups"
  253. headers:
  254. Authorization: Bearer {{ rhsso_token }}
  255. Accept: application/json
  256. register: rhsso_groups_raw
  257. - name: reformat existing groups as a list
  258. set_fact:
  259. rhsso_groups: "{{ rhsso_groups_raw.json | items2dict(key_name='name', value_name='id') }}"
  260. # XXX for some dumbass reason realmRoles assignment won't stick, either when creating or updating XXX
  261. - name: assign realm roles
  262. uri:
  263. method: POST
  264. return_content: true
  265. validate_certs: false
  266. url: "https://{{ rhsso_ep }}/auth/admin/realms/rhacs/groups"
  267. headers:
  268. Authorization: Bearer {{ rhsso_token }}
  269. Accept: application/json
  270. Content-Type: application/json
  271. body_format: json
  272. body: '{"id": "{{ rhsso_groups[item] }}", "name": "{{ item | string }}", "realmRoles": [ "{{ item | string }}" ]}'
  273. status_code:
  274. - 200
  275. - 201
  276. - 204
  277. loop:
  278. - admins
  279. - analysts
  280. - cicd
  281. - reporters
  282. - developers
  283. - name: create the users
  284. kubernetes.core.k8s:
  285. kubeconfig: "{{ ansible_facts['user_dir'] }}/kubeconfig-ocp4"
  286. validate_certs: no
  287. template: templates/rhsso-users.yml
  288. ...