summaryrefslogtreecommitdiffstats
path: root/roles/grafana/tasks/main.yml
blob: 088d61c7f2f6ba02079ec136093e5c12660ffc61 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
---
- name: "Include OS-specific variables"
  ansible.builtin.include_vars: "{{ ansible_facts['os_family'] | lower }}.yml"

- name: "Include OS-specific installation tasks"
  ansible.builtin.include_tasks: "install-{{ ansible_facts['os_family'] | lower }}.yml"

- name: "Create grafana group"
  become: true
  ansible.builtin.group:
    name: "grafana"
    system: true

- name: "Create grafana user"
  become: true
  ansible.builtin.user:
    name: "grafana"
    group: "grafana"
    system: true
    shell: "/usr/sbin/nologin"
    create_home: false

- name: "Create grafana configuration directories"
  become: true
  ansible.builtin.file:
    path: "{{ item }}"
    state: "directory"
    owner: "grafana"
    group: "grafana"
    mode: "0755"
  loop:
    - "/etc/grafana"
    - "/etc/grafana/provisioning"
    - "/etc/grafana/provisioning/datasources"
    - "/etc/grafana/provisioning/plugins"
    - "/etc/grafana/provisioning/dashboards"

- name: "Template grafana configurations"
  become: true
  ansible.builtin.template:
    src: "{{ item.src }}"
    dest: "{{ item.dest }}"
    owner: "root"
    group: "grafana"
    mode: "0640"
  loop:
    - src: "grafana.ini.j2"
      dest: "{{ grafana_config_file }}"
    - src: "datasources/prometheus.yml.j2"
      dest: "/etc/grafana/provisioning/datasources/prometheus.yml"
    - src: "dashboards/dashboards.yml.j2"
      dest: "/etc/grafana/provisioning/dashboards/dashboards.yml"
  notify: "Restart grafana"
  no_log: true

- name: "Provision Grafana dashboards"
  notify: "Restart grafana"
  # grafana downloaded dashboards are being rewritten after successful download
  # this is to correct the datasource mapping in the downloaded files
  # This is why these tasks are not idempotent and can't be.
  tags:
    - molecule-idempotence-notest
  block:
    - name: "Download Grafana dashboards"
      become: true
      ansible.builtin.get_url:
        url: "{{ item.url }}"
        dest: "/etc/grafana/provisioning/dashboards/{{ item.name }}.json"
        owner: "grafana"
        group: "grafana"
        mode: "0644"
        force: false
      loop: "{{ grafana_dashboards }}"

    - name: "Fix datasource references in downloaded dashboards"
      become: true
      ansible.builtin.replace:
        path: "/etc/grafana/provisioning/dashboards/{{ item.0.name }}.json"
        regexp: "{{ item.1.key | regex_escape }}"
        replace: "{{ item.1.value }}"
      loop: "{{ grafana_dashboards | subelements('datasource_mappings', skip_missing=True) }}"

- name: "Start and enable grafana"
  become: true
  ansible.builtin.systemd_service:
    name: "{{ grafana_service }}"
    state: "started"
    enabled: true

- name: "Create grafana nginx vhost configuration"
  become: true
  when: proxy_type == "nginx" and grafana_hostname | length > 0
  ansible.builtin.template:
    src: "grafana.nginx.conf.j2"
    dest: "/etc/nginx/vhosts.d/grafana.conf"
    owner: "root"
    group: "root"
    mode: "0644"
    backup: true
    validate: >
      bash -c 'echo "events { worker_connections 64; } http { include %s; }" > /tmp/nginx-vhost.conf;
        sudo nginx -T -c /tmp/nginx-vhost.conf; ec=$?; rm -f /tmp/nginx-vhost.conf; exit $ec'
  notify: "Restart proxy"

- name: "Create grafana caddy configuration"
  become: true
  when: proxy_type == "caddy" and grafana_hostname | length > 0
  ansible.builtin.template:
    src: "grafana.caddy.j2"
    dest: "/etc/caddy/sites/grafana.caddy"
    owner: "root"
    group: "caddy"
    mode: "0640"
  notify: "Restart proxy"