install_grafana.yaml 7.1 KB

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