main.yml 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. - name: (re)apply the client if necessary
  137. kubernetes.core.k8s:
  138. kubeconfig: "{{ ansible_facts['user_dir'] }}/kubeconfig-ocp4"
  139. validate_certs: no
  140. src: files/rhsso-client.yml
  141. apply: yes
  142. - name: make sure rhsso pod is ready
  143. kubernetes.core.k8s_info:
  144. kubeconfig: "{{ ansible_facts['user_dir'] }}/kubeconfig-ocp4"
  145. validate_certs: no
  146. api_version: v1
  147. kind: pod
  148. namespace: openshift-sso
  149. label_selectors:
  150. - app = keycloak
  151. - component = keycloak
  152. when: not rhsso_is_there
  153. register: rhsso_pod
  154. until: (rhsso_pod.resources | length) > 0 and rhsso_pod.resources[0].status.containerStatuses[0].ready
  155. retries: 60
  156. delay: 5
  157. - name: get an auth token
  158. uri:
  159. method: POST
  160. return_content: true
  161. validate_certs: false
  162. url: "https://{{ rhsso_ep }}/auth/realms/master/protocol/openid-connect/token"
  163. headers:
  164. Accept: application/json
  165. Content-Type: application/x-www-form-urlencoded
  166. body_format: json
  167. body: "client_id=admin-cli&username=admin&password={{ rhsso_pass | string | b64decode }}&grant_type=password"
  168. register: rhsso_token_rsp
  169. - assert:
  170. that: rhsso_token_rsp.json is defined and rhsso_token_rsp.json.access_token is defined
  171. fail_msg: "ERROR: Failed to obtain authentication token from RHSSO."
  172. success_msg: "OK, got auth token; proceeding."
  173. - name: store the token as a fact
  174. set_fact:
  175. rhsso_token: "{{ rhsso_token_rsp.json.access_token }}"
  176. - name: get existing role list
  177. uri:
  178. method: GET
  179. return_content: true
  180. validate_certs: false
  181. url: "https://{{ rhsso_ep }}/auth/admin/realms/rhacs/roles"
  182. headers:
  183. Authorization: Bearer {{ rhsso_token }}
  184. Accept: application/json
  185. register: rhsso_roles_raw
  186. - name: store existing roles as a list
  187. set_fact:
  188. rhsso_roles: "{{ rhsso_roles_raw.json | items2dict(key_name='name', value_name='id') }}"
  189. - name: create roles
  190. uri:
  191. method: POST
  192. return_content: true
  193. validate_certs: false
  194. url: "https://{{ rhsso_ep }}/auth/admin/realms/rhacs/roles"
  195. headers:
  196. Authorization: Bearer {{ rhsso_token }}
  197. Accept: application/json
  198. Content-Type: application/json
  199. body_format: json
  200. body: '{"name": "{{ item | string }}"}'
  201. status_code:
  202. - 200
  203. - 201
  204. loop:
  205. - admins
  206. - analysts
  207. - cicd
  208. - reporters
  209. - developers
  210. when: item not in rhsso_roles.keys()
  211. - name: get existing group list
  212. uri:
  213. method: GET
  214. return_content: true
  215. validate_certs: false
  216. url: "https://{{ rhsso_ep }}/auth/admin/realms/rhacs/groups"
  217. headers:
  218. Authorization: Bearer {{ rhsso_token }}
  219. Accept: application/json
  220. register: rhsso_groups_raw
  221. - name: store existing groups as a list
  222. set_fact:
  223. rhsso_groups: "{{ rhsso_groups_raw.json | items2dict(key_name='name', value_name='id') }}"
  224. - name: create groups
  225. uri:
  226. method: POST
  227. return_content: true
  228. validate_certs: false
  229. url: "https://{{ rhsso_ep }}/auth/admin/realms/rhacs/groups"
  230. headers:
  231. Authorization: Bearer {{ rhsso_token }}
  232. Accept: application/json
  233. Content-Type: application/json
  234. body_format: json
  235. body: '{"name": "{{ item | string }}" }'
  236. status_code:
  237. - 200
  238. - 201
  239. loop:
  240. - admins
  241. - analysts
  242. - cicd
  243. - reporters
  244. - developers
  245. when: item not in rhsso_groups.keys()
  246. - name: retrieve group list again
  247. uri:
  248. method: GET
  249. return_content: true
  250. validate_certs: false
  251. url: "https://{{ rhsso_ep }}/auth/admin/realms/rhacs/groups"
  252. headers:
  253. Authorization: Bearer {{ rhsso_token }}
  254. Accept: application/json
  255. register: rhsso_groups_raw
  256. - name: reformat existing groups as a list
  257. set_fact:
  258. rhsso_groups: "{{ rhsso_groups_raw.json | items2dict(key_name='name', value_name='id') }}"
  259. # XXX for some dumbass reason realmRoles assignment won't stick, either when creating or updating XXX
  260. - name: assign realm roles
  261. uri:
  262. method: POST
  263. return_content: true
  264. validate_certs: false
  265. url: "https://{{ rhsso_ep }}/auth/admin/realms/rhacs/groups"
  266. headers:
  267. Authorization: Bearer {{ rhsso_token }}
  268. Accept: application/json
  269. Content-Type: application/json
  270. body_format: json
  271. body: '{"id": "{{ rhsso_groups[item] }}", "name": "{{ item | string }}", "realmRoles": [ "{{ item | string }}" ]}'
  272. status_code:
  273. - 200
  274. - 201
  275. - 204
  276. loop:
  277. - admins
  278. - analysts
  279. - cicd
  280. - reporters
  281. - developers
  282. - name: create the users
  283. kubernetes.core.k8s:
  284. kubeconfig: "{{ ansible_facts['user_dir'] }}/kubeconfig-ocp4"
  285. validate_certs: no
  286. template: templates/rhsso-users.yml
  287. ...