configure_nginx_prom_grafana.yml 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # Copyright 2022 Dell Inc. or its subsidiaries. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. ---
  15. - name: Install Nginx
  16. package:
  17. name: nginx
  18. state: present
  19. when: ansible_facts['distribution'] != opensuse_os_name
  20. - name: Install Nginx
  21. zypper:
  22. name: nginx
  23. state: present
  24. when: ansible_facts['distribution'] == opensuse_os_name
  25. - name: Get prometheus service IP
  26. command: kubectl get svc -l app=prometheus,component=server -o=jsonpath='{.items[0].spec.clusterIP}'
  27. changed_when: false
  28. register: prometheus_svc_ip
  29. - name: Configure nginx.conf (1/2)
  30. replace:
  31. path: "{{ role_path }}/templates/nginx.conf.j2"
  32. regexp: ' server_name .*'
  33. replace: " server_name {{ ansible_default_ipv4.address }};"
  34. delegate_to: localhost
  35. - name: Configure nginx.conf (2/2)
  36. replace:
  37. path: "{{ role_path }}/templates/nginx.conf.j2"
  38. regexp: ' proxy_pass http://.*'
  39. replace: " proxy_pass http://{{ prometheus_svc_ip.stdout }};"
  40. delegate_to: localhost
  41. - name: Load nginx conf
  42. template:
  43. src: nginx.conf.j2
  44. dest: "{{ nginx_conf_file_path }}"
  45. mode: "{{ nginx_conf_file_mode }}"
  46. - name: Validate nginx conf file
  47. command: nginx -t
  48. changed_when: false
  49. - name: Start and enable nginx service
  50. service:
  51. name: nginx
  52. state: restarted
  53. enabled: yes
  54. - block:
  55. - name: Create prometheus datasource in grafana
  56. community.grafana.grafana_datasource:
  57. name: "hpc-prometheus-{{ ansible_default_ipv4.address }}"
  58. grafana_url: "http://{{ grafana_svc_ip }}:{{ grafana_svc_port }}"
  59. grafana_user: "{{ hostvars['127.0.0.1']['grafana_username'] }}"
  60. grafana_password: "{{ hostvars['127.0.0.1']['grafana_password'] }}"
  61. ds_type: prometheus
  62. ds_url: "http://{{ ansible_default_ipv4.address }}:{{ nginx_listen_port }}"
  63. access: direct
  64. delegate_to: localhost
  65. no_log: true
  66. register: create_k8s_prom_datasource
  67. rescue:
  68. - name: Create prometheus datasource in grafana failed
  69. fail:
  70. msg: "Error: {{ create_k8s_prom_datasource.msg }}"