main.yml 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. # TODO: MAKE SURE RHSSO IS UP AND RESPONDING!
  143. - name: get an auth token
  144. uri:
  145. method: POST
  146. return_content: true
  147. validate_certs: false
  148. url: "https://{{ rhsso_ep }}/auth/realms/master/protocol/openid-connect/token"
  149. headers:
  150. Accept: application/json
  151. body_format: json
  152. body: "client_id=admin-cli&username=admin&password={{ rhsso_pass | string | b64decode }}&grant_type=password"
  153. register: rhsso_token_rsp
  154. - assert:
  155. that: rhsso_token_rsp.json is defined and rhsso_token_rsp.json.access_token is defined
  156. fail_msg: "ERROR: Failed to obtain authentication token from RHSSO."
  157. success_msg: "OK, got auth token; proceeding."
  158. - name: store the token as a fact
  159. set_fact:
  160. rhsso_token: "{{ rhsso_token_rsp.json.access_token }}"
  161. - name: get existing group list
  162. uri:
  163. method: GET
  164. return_content: true
  165. validate_certs: false
  166. url: "https://{{ rhsso_ep }}/auth/admin/realms/rhacs/groups"
  167. headers:
  168. Authorization: Bearer {{ rhsso_token }}
  169. Accept: application/json
  170. register: rhsso_groups_raw
  171. - name: store existing groups as a list
  172. set_fact:
  173. rhsso_groups: "{{ rhsso_groups_raw.json | items2dict(key_name='name', value_name='id') }}"
  174. - name: create groups
  175. uri:
  176. method: POST
  177. return_content: true
  178. validate_certs: false
  179. url: "https://{{ rhsso_ep }}/auth/admin/realms/rhacs/groups"
  180. headers:
  181. Authorization: Bearer {{ rhsso_token }}
  182. Accept: application/json
  183. Content-Type: application/json
  184. body_format: json
  185. body: '{"name": "{{ item | string }}"}'
  186. status_code:
  187. - 200
  188. - 201
  189. loop:
  190. - admins
  191. - analysts
  192. - cicd
  193. - reporters
  194. - developers
  195. when: item not in rhsso_groups.keys()
  196. - name: create the users
  197. kubernetes.core.k8s:
  198. kubeconfig: "{{ ansible_facts['user_dir'] }}/kubeconfig-ocp4"
  199. validate_certs: no
  200. template: templates/rhsso-users.yml
  201. ...