install_grafana.yaml 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. ---
  2. - name: Ensure that Grafana has nodes to run on
  3. fail:
  4. msg: |-
  5. No schedulable nodes found matching node selector for Grafana - '{{ grafana_node_selector }}'
  6. when:
  7. - openshift_schedulable_node_labels | lib_utils_oo_has_no_matching_selector(grafana_node_selector)
  8. - name: Create grafana namespace
  9. oc_project:
  10. state: present
  11. name: "{{ grafana_namespace }}"
  12. node_selector: "{{ grafana_node_selector | lib_utils_oo_selector_to_string_list() }}"
  13. description: Grafana
  14. - name: create grafana_serviceaccount_name serviceaccount
  15. oc_serviceaccount:
  16. state: present
  17. name: "{{ grafana_serviceaccount_name }}"
  18. namespace: "{{ grafana_namespace }}"
  19. changed_when: no
  20. # TODO remove this when annotations are supported by oc_serviceaccount
  21. - name: annotate serviceaccount
  22. command: >
  23. {{ openshift_client_binary }} annotate --overwrite -n {{ grafana_namespace }}
  24. serviceaccount {{ grafana_serviceaccount_name }} {{ item }}
  25. with_items:
  26. "{{ grafana_serviceaccount_annotations }}"
  27. # create clusterrolebinding for prometheus serviceaccount
  28. - name: Set cluster-reader permissions for grafana
  29. oc_adm_policy_user:
  30. state: present
  31. namespace: "{{ grafana_namespace }}"
  32. resource_kind: cluster-role
  33. resource_name: cluster-reader
  34. user: "system:serviceaccount:{{ grafana_namespace }}:{{ grafana_serviceaccount_name }}"
  35. - name: create grafana routes
  36. oc_route:
  37. state: present
  38. name: "{{ item.name }}"
  39. host: "{{ item.host }}"
  40. namespace: "{{ grafana_namespace }}"
  41. service_name: "{{ item.name }}"
  42. tls_termination: reencrypt
  43. with_items:
  44. - name: grafana
  45. host: "{{ grafana_hostname }}"
  46. - name: create services for grafana
  47. oc_service:
  48. name: "{{ grafana_service_name }}"
  49. namespace: "{{ grafana_namespace }}"
  50. labels:
  51. name: grafana
  52. annotations:
  53. prometheus.io/scrape: "true"
  54. prometheus.io/scheme: https
  55. service.alpha.openshift.io/serving-cert-secret-name: grafana-tls
  56. ports:
  57. - name: grafana
  58. port: "{{ grafana_service_port }}"
  59. targetPort: "{{ grafana_service_targetport }}"
  60. protocol: TCP
  61. selector:
  62. app: grafana
  63. - name: Set grafana secrets
  64. oc_secret:
  65. state: present
  66. name: "{{ item }}-proxy"
  67. namespace: "{{ grafana_namespace }}"
  68. contents:
  69. - path: session_secret
  70. data: "{{ 43 | lib_utils_oo_random_word }}="
  71. with_items:
  72. - grafana
  73. # Storage
  74. - name: create grafana pvc
  75. oc_pvc:
  76. namespace: "{{ grafana_namespace }}"
  77. name: "{{ grafana_pvc_name }}"
  78. access_modes: "{{ grafana_pvc_access_modes }}"
  79. volume_capacity: "{{ grafana_pvc_size }}"
  80. selector: "{{ grafana_pvc_pv_selector }}"
  81. storage_class_name: "{{ grafana_sc_name }}"
  82. when: grafana_storage_type == 'pvc'
  83. - name: template grafana components
  84. template:
  85. src: "{{ item }}.j2"
  86. dest: "{{ mktemp.stdout }}/{{ item }}"
  87. changed_when: no
  88. with_items:
  89. - "grafana.yml"
  90. - "grafana-config.yml"
  91. - name: Set grafana configmap
  92. oc_configmap:
  93. state: present
  94. name: "grafana-config"
  95. namespace: "{{ grafana_namespace }}"
  96. from_file:
  97. defaults.ini: "{{ mktemp.stdout }}/grafana-config.yml"
  98. - name: Set grafana deployment
  99. oc_obj:
  100. state: present
  101. name: "grafana"
  102. namespace: "{{ grafana_namespace }}"
  103. kind: deployment
  104. files:
  105. - "{{ mktemp.stdout }}/grafana.yml"
  106. - name: Copy Grafana files
  107. copy:
  108. src: "dashboards/{{ item }}"
  109. dest: "{{ mktemp.stdout }}/{{ item }}"
  110. with_items:
  111. - "{{ grafana_dashboards }}"
  112. - name: Wait for grafana pod
  113. oc_obj:
  114. namespace: "{{ grafana_namespace }}"
  115. kind: pod
  116. state: list
  117. selector: "app=grafana"
  118. register: grafana_pod
  119. until:
  120. - "grafana_pod.results.results[0]['items'] | count > 0"
  121. # Pod's 'Ready' status must be True
  122. - "grafana_pod.results.results[0]['items'] | lib_utils_oo_collect(attribute='status.conditions') | lib_utils_oo_collect(attribute='status', filters={'type': 'Ready'}) | map('bool') | select | list | count == 1"
  123. delay: 10
  124. retries: "{{ (grafana_timeout | int / 10) | int }}"
  125. - name: Get the prometheus SA
  126. shell: oc sa get-token {{ grafana_prometheus_serviceaccount }} -n {{ grafana_prometheus_namespace }}
  127. register: prometheus_sa_token
  128. - name: Get prometheus route
  129. oc_route:
  130. state: list
  131. name: prometheus
  132. namespace: "{{ grafana_prometheus_namespace }}"
  133. register: prometheus_route
  134. - name: Get prometheus route
  135. oc_route:
  136. state: list
  137. name: grafana
  138. namespace: "{{ grafana_namespace }}"
  139. register: grafana_route
  140. - name: set facts
  141. set_fact:
  142. payload_data: "{{ grafana_datasource_json | regex_replace('grafana_name', grafana_datasource_name ) | regex_replace('prometheus_url', prometheus_route.results[0].spec.host ) | regex_replace('satoken', prometheus_sa_token.stdout ) }}"
  143. grafana_route: "https://{{ grafana_route.results[0].spec.host }}"
  144. - name: Add new datasource to grafana
  145. uri:
  146. url: "{{ grafana_route }}/api/datasources"
  147. user: admin
  148. password: admin
  149. method: POST
  150. body: '{{ payload_data }}'
  151. body_format: json
  152. headers:
  153. Content-Type: "Content-Type: application/json"
  154. register: add_ds
  155. - name: Regex set data soure name for openshift dashboard
  156. replace:
  157. path: "{{ mktemp.stdout }}/openshift-cluster-monitoring.json"
  158. regexp: '{{ item.regexp }}'
  159. replace: '{{ item.replace }}'
  160. backup: yes
  161. with_items:
  162. - regexp: '##DS_PR##'
  163. replace: '{{ grafana_datasource_name }}'
  164. - regexp: 'Xs'
  165. replace: '{{ grafana_graph_granularity }}'
  166. - name: Regex set data soure name for node exporter
  167. replace:
  168. path: "{{ mktemp.stdout }}/node-exporter-full-dashboard.json"
  169. regexp: '{{ item.regexp }}'
  170. replace: '{{ item.replace }}'
  171. backup: yes
  172. with_items:
  173. - regexp: '##DS_PR##'
  174. replace: '{{ grafana_datasource_name }}'
  175. - regexp: 'Xs'
  176. replace: '{{ grafana_graph_granularity }}'
  177. when: grafana_node_exporter | default(false) | bool == true
  178. - set_fact:
  179. cluster_monitoring_dashboard: "{{ mktemp.stdout }}/openshift-cluster-monitoring.json"
  180. node_exporter_dashboard: "{{ mktemp.stdout }}/node-exporter-full-dashboard.json"
  181. - name: Slurp dashboard file
  182. slurp:
  183. src: "{{ cluster_monitoring_dashboard }}"
  184. register: slurpfile
  185. - name: Add openshift dashboard
  186. uri:
  187. url: "{{ grafana_route }}/api/dashboards/db"
  188. user: admin
  189. password: admin
  190. method: POST
  191. body: '{{ slurpfile["content"] | b64decode }}'
  192. body_format: json
  193. headers:
  194. Content-Type: "Content-Type: application/json"
  195. register: add_ds
  196. - name: Slurp dashboard file
  197. slurp:
  198. src: "{{ node_exporter_dashboard }}"
  199. register: slurpfile
  200. - name: Add node exporter dashboard
  201. uri:
  202. url: "{{ grafana_route }}/api/dashboards/db"
  203. user: admin
  204. password: admin
  205. method: POST
  206. body: '{{ slurpfile["content"] | b64decode }}'
  207. body_format: json
  208. headers:
  209. Content-Type: "Content-Type: application/json"
  210. register: add_ds
  211. when: grafana_node_exporter | default(false) | bool == true