install_grafana.yaml 7.1 KB

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