install_prometheus.yaml 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. ---
  2. # set facts
  3. - include_tasks: facts.yaml
  4. # namespace
  5. - name: Add prometheus project
  6. oc_project:
  7. state: present
  8. name: "{{ openshift_prometheus_namespace }}"
  9. node_selector: "{{ openshift_prometheus_node_selector | lib_utils_oo_selector_to_string_list() }}"
  10. description: Prometheus
  11. # secrets
  12. - name: Set alert, alertmanager and prometheus secrets
  13. oc_secret:
  14. state: present
  15. name: "{{ item }}-proxy"
  16. namespace: "{{ openshift_prometheus_namespace }}"
  17. contents:
  18. - path: session_secret
  19. data: "{{ 43 | lib_utils_oo_random_word }}="
  20. with_items:
  21. - prometheus
  22. - alerts
  23. - alertmanager
  24. # serviceaccount
  25. - name: create prometheus serviceaccount
  26. oc_serviceaccount:
  27. state: present
  28. name: "{{ openshift_prometheus_service_name }}"
  29. namespace: "{{ openshift_prometheus_namespace }}"
  30. changed_when: no
  31. # TODO remove this when annotations are supported by oc_serviceaccount
  32. - name: annotate serviceaccount
  33. command: >
  34. {{ openshift_client_binary }} annotate --overwrite -n {{ openshift_prometheus_namespace }}
  35. serviceaccount {{ openshift_prometheus_service_name }} {{ item }}
  36. with_items:
  37. "{{ openshift_prometheus_serviceaccount_annotations }}"
  38. # create clusterrolebinding for prometheus serviceaccount
  39. - name: Set cluster-reader permissions for prometheus
  40. oc_adm_policy_user:
  41. state: present
  42. namespace: "{{ openshift_prometheus_namespace }}"
  43. resource_kind: cluster-role
  44. resource_name: cluster-reader
  45. user: "system:serviceaccount:{{ openshift_prometheus_namespace }}:{{ openshift_prometheus_service_name }}"
  46. - name: create services for prometheus
  47. oc_service:
  48. name: "{{ openshift_prometheus_service_name }}"
  49. namespace: "{{ openshift_prometheus_namespace }}"
  50. labels:
  51. name: prometheus
  52. annotations:
  53. oprometheus.io/scrape: 'true'
  54. oprometheus.io/scheme: https
  55. service.alpha.openshift.io/serving-cert-secret-name: prometheus-tls
  56. ports:
  57. - name: prometheus
  58. port: "{{ openshift_prometheus_service_port }}"
  59. targetPort: "{{ openshift_prometheus_service_targetport }}"
  60. protocol: TCP
  61. selector:
  62. app: prometheus
  63. - name: create services for alert buffer
  64. oc_service:
  65. name: "{{ openshift_prometheus_alerts_service_name }}"
  66. namespace: "{{ openshift_prometheus_namespace }}"
  67. labels:
  68. name: prometheus
  69. annotations:
  70. service.alpha.openshift.io/serving-cert-secret-name: alerts-tls
  71. ports:
  72. - name: prometheus
  73. port: "{{ openshift_prometheus_service_port }}"
  74. targetPort: "{{ openshift_prometheus_alerts_service_targetport }}"
  75. protocol: TCP
  76. selector:
  77. app: prometheus
  78. - name: create services for alertmanager
  79. oc_service:
  80. name: "{{ openshift_prometheus_alertmanager_service_name }}"
  81. namespace: "{{ openshift_prometheus_namespace }}"
  82. labels:
  83. name: prometheus
  84. annotations:
  85. service.alpha.openshift.io/serving-cert-secret-name: alertmanager-tls
  86. ports:
  87. - name: prometheus
  88. port: "{{ openshift_prometheus_service_port }}"
  89. targetPort: "{{ openshift_prometheus_alertmanager_service_targetport }}"
  90. protocol: TCP
  91. selector:
  92. app: prometheus
  93. # create prometheus and alerts routes
  94. # TODO: oc_route module should support insecureEdgeTerminationPolicy: Redirect
  95. - name: create prometheus and alerts routes
  96. oc_route:
  97. state: present
  98. name: "{{ item.name }}"
  99. host: "{{ item.host }}"
  100. namespace: "{{ openshift_prometheus_namespace }}"
  101. service_name: "{{ item.name }}"
  102. tls_termination: reencrypt
  103. with_items:
  104. - name: prometheus
  105. host: "{{ openshift_prometheus_hostname }}"
  106. - name: alerts
  107. host: "{{ openshift_prometheus_alerts_hostname }}"
  108. - name: alertmanager
  109. host: "{{ openshift_prometheus_alertmanager_hostname }}"
  110. # Storage
  111. - name: create prometheus pvc
  112. oc_pvc:
  113. namespace: "{{ openshift_prometheus_namespace }}"
  114. name: "{{ openshift_prometheus_pvc_name }}"
  115. access_modes: "{{ openshift_prometheus_pvc_access_modes }}"
  116. volume_capacity: "{{ openshift_prometheus_pvc_size }}"
  117. selector: "{{ openshift_prometheus_pvc_pv_selector }}"
  118. storage_class_name: "{{ openshift_prometheus_sc_name }}"
  119. when: openshift_prometheus_storage_type == 'pvc'
  120. - name: create alertmanager pvc
  121. oc_pvc:
  122. namespace: "{{ openshift_prometheus_namespace }}"
  123. name: "{{ openshift_prometheus_alertmanager_pvc_name }}"
  124. access_modes: "{{ openshift_prometheus_alertmanager_pvc_access_modes }}"
  125. volume_capacity: "{{ openshift_prometheus_alertmanager_pvc_size }}"
  126. selector: "{{ openshift_prometheus_alertmanager_pvc_pv_selector }}"
  127. storage_class_name: "{{ openshift_prometheus_alertmanager_sc_name }}"
  128. when: openshift_prometheus_alertmanager_storage_type == 'pvc'
  129. - name: create alertbuffer pvc
  130. oc_pvc:
  131. namespace: "{{ openshift_prometheus_namespace }}"
  132. name: "{{ openshift_prometheus_alertbuffer_pvc_name }}"
  133. access_modes: "{{ openshift_prometheus_alertbuffer_pvc_access_modes }}"
  134. volume_capacity: "{{ openshift_prometheus_alertbuffer_pvc_size }}"
  135. selector: "{{ openshift_prometheus_alertbuffer_pvc_pv_selector }}"
  136. storage_class_name: "{{ openshift_prometheus_alertbuffer_sc_name }}"
  137. when: openshift_prometheus_alertbuffer_storage_type == 'pvc'
  138. # prometheus configmap
  139. # Copy the additional rules file if it is defined
  140. - name: Copy additional rules file to host
  141. copy:
  142. src: "{{ openshift_prometheus_additional_rules_file }}"
  143. dest: "{{ tempdir }}/prometheus.additional.rules"
  144. when:
  145. - openshift_prometheus_additional_rules_file is defined
  146. - openshift_prometheus_additional_rules_file is not none
  147. - openshift_prometheus_additional_rules_file | trim | length > 0
  148. - stat:
  149. path: "{{ tempdir }}/prometheus.additional.rules"
  150. register: additional_rules_stat
  151. - template:
  152. src: prometheus.yml.j2
  153. dest: "{{ tempdir }}/prometheus.yml"
  154. changed_when: no
  155. - template:
  156. src: prometheus.rules.j2
  157. dest: "{{ tempdir }}/prometheus.rules"
  158. changed_when: no
  159. # In prometheus configmap create "additional.rules" section if file exists
  160. - name: Set prometheus configmap
  161. oc_configmap:
  162. state: present
  163. name: "prometheus"
  164. namespace: "{{ openshift_prometheus_namespace }}"
  165. from_file:
  166. prometheus.rules: "{{ tempdir }}/prometheus.rules"
  167. prometheus.additional.rules: "{{ tempdir }}/prometheus.additional.rules"
  168. prometheus.yml: "{{ tempdir }}/prometheus.yml"
  169. when: additional_rules_stat.stat.exists == True
  170. - name: Set prometheus configmap
  171. oc_configmap:
  172. state: present
  173. name: "prometheus"
  174. namespace: "{{ openshift_prometheus_namespace }}"
  175. from_file:
  176. prometheus.rules: "{{ tempdir }}/prometheus.rules"
  177. prometheus.yml: "{{ tempdir }}/prometheus.yml"
  178. when: additional_rules_stat.stat.exists == False
  179. # alertmanager configmap
  180. - template:
  181. src: alertmanager.yml.j2
  182. dest: "{{ tempdir }}/alertmanager.yml"
  183. changed_when: no
  184. - name: Set alertmanager configmap
  185. oc_configmap:
  186. state: present
  187. name: "alertmanager"
  188. namespace: "{{ openshift_prometheus_namespace }}"
  189. from_file:
  190. alertmanager.yml: "{{ tempdir }}/alertmanager.yml"
  191. # create prometheus stateful set
  192. - name: Set prometheus template
  193. template:
  194. src: prometheus.j2
  195. dest: "{{ tempdir }}/templates/prometheus.yaml"
  196. vars:
  197. namespace: "{{ openshift_prometheus_namespace }}"
  198. # prom_replicas: "{{ openshift_prometheus_replicas }}"
  199. - name: Set prometheus stateful set
  200. oc_obj:
  201. state: present
  202. name: "prometheus"
  203. namespace: "{{ openshift_prometheus_namespace }}"
  204. kind: statefulset
  205. files:
  206. - "{{ tempdir }}/templates/prometheus.yaml"
  207. delete_after: true