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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
- name: Set up Grafana
  hosts: lotus-land-story
  vars:
    version: "{{ versions.grafana }}"
  tasks:

    - name: Grafana user
      ansible.builtin.user:
        name: grafana
      register: grafana_user

    - name: Create Grafana dir
      ansible.builtin.file:
        path: /mnt/lotus-land-story/grafana/provisioning/{{ item }}
        state: directory
        mode: "0755"
        owner: "{{ grafana_user.name }}"
      loop:
        - datasources
        - dashboards

    - name: Configure Grafana
      ansible.builtin.copy:
        dest: /mnt/lotus-land-story/grafana/grafana.ini
        content: |
          [log]
          # level = debug

          [metrics]
          enabled             = true
          disable_total_stats = false

          [server]
          domain = grafana
          http_addr = 0.0.0.0
          root_url = https://grafana

          # https://grafana.com/docs/grafana/latest/setup-grafana/configure-security/configure-authentication/auth-proxy/
          [auth]
          disable_signout_menu = true

          [auth.proxy]
          enabled = true
          header_name = X-WEBAUTH-USER
          header_property = email
          auto_sign_up = false

          [smtp]
          enabled = true
          host = smtp.sendgrid.net:465
          user = apikey
          password = {{ grafana.smtp_password }}
          from_address = grafana@kejadlen.dev

          [plugins]
          plugin_admin_enabled = true
        mode: "0600"
        owner: "{{ grafana_user.name }}"

    - name: Provision VictoriaLogs
      ansible.builtin.copy:
        dest: /mnt/lotus-land-story/grafana/provisioning/datasources/victoria-logs.yml
        content: |
          apiVersion: 1

          datasources:
            - name: VictoriaLogs
              type: victoriametrics-logs-datasource
              access: proxy
              url: http://victoria-logs:9428
        mode: "0644"

    - name: Provision VictoriaMetrics
      ansible.builtin.copy:
        dest: /mnt/lotus-land-story/grafana/provisioning/datasources/victoria-metrics.yml
        content: |
          apiVersion: 1

          # List of data sources to insert/update depending on what's
          # available in the database.
          datasources:
            # <string, required> Name of the VictoriaMetrics datasource
            # displayed in Grafana panels and queries.
            - name: VictoriaMetrics
              # <string, required> Sets the data source type.
              type: victoriametrics-metrics-datasource
                # <string, required> Sets the access mode, either
                # proxy or direct (Server or Browser in the UI).
                # Some data sources are incompatible with any setting
              # but proxy (Server).
              access: proxy
              # <string> Sets default URL of the single node version of VictoriaMetrics
              url: http://victoria-metrics:8428
              # <string> Sets the pre-selected datasource for new panels.
              # You can set only one default data source per organization.
              isDefault: true
        mode: "0644"

    # Jaeger rather than Tempo: VictoriaTraces' Tempo API needs v0.9.4+.
    - name: Provision VictoriaTraces
      ansible.builtin.copy:
        dest: /mnt/lotus-land-story/grafana/provisioning/datasources/victoria-traces.yml
        content: |
          apiVersion: 1

          datasources:
            - name: VictoriaTraces
              type: jaeger
              access: proxy
              url: http://victoria-traces:10428/select/jaeger
        mode: "0644"

    # allowUiUpdates is false so the repository stays the source of truth.
    - name: Provision dashboard provider
      ansible.builtin.copy:
        dest: /mnt/lotus-land-story/grafana/provisioning/dashboards/provider.yml
        content: |
          apiVersion: 1

          providers:
            - name: default
              type: file
              allowUiUpdates: false
              updateIntervalSeconds: 30
              options:
                path: /etc/grafana/provisioning/dashboards
        mode: "0644"

    - name: Provision dashboards
      ansible.builtin.copy:
        src: "{{ item }}"
        dest: /mnt/lotus-land-story/grafana/provisioning/dashboards/
        mode: "0644"
      loop: "{{ query('ansible.builtin.fileglob', playbook_dir ~ '/templates/dashboards/*.json', wantlist=true) }}"

    - name: Create Grafana data dir
      ansible.builtin.file:
        path: /mnt/lotus-land-story/grafana/data
        state: directory
        mode: "0755"
        owner: "{{ grafana_user.name }}"

    - name: Run Grafana
      community.docker.docker_container:
        name: grafana
        image: grafana/grafana:{{ version }}
        volumes:
          - /mnt/lotus-land-story/grafana/grafana.ini:/etc/grafana/grafana.ini
          - /mnt/lotus-land-story/grafana/provisioning:/etc/grafana/provisioning
          - /mnt/lotus-land-story/grafana/data:/var/lib/grafana
        env:
          GF_PLUGINS_PREINSTALL: victoriametrics-logs-datasource, victoriametrics-metrics-datasource
        restart_policy: unless-stopped
        networks:
          - name: lotus_land_story
        etc_hosts:
          host.docker.internal: host-gateway
        user: "{{ grafana_user.uid }}"

# vim: ft=yaml.ansible