--- - name: add debian repository include_role: name: apt-repo/grafana - name: generate apt pin file for grafana package when: grafana_version is defined copy: dest: "/etc/apt/preferences.d/grafana.pref" content: | Package: grafana Pin: version {{ grafana_version }} Pin-Priority: 1001 - name: remove apt pin file for grafana package when: grafana_version is not defined file: path: "/etc/apt/preferences.d/grafana.pref" state: absent - name: install apt packages apt: name: "grafana{% if grafana_version is defined %}={{ grafana_version }}{% endif %}" state: present allow_downgrade: yes - name: configure grafana server loop: "{{ grafana_config_server | dict2items }}" loop_control: label: "{{ item.key }}" ini_file: path: /etc/grafana/grafana.ini section: server option: "{{ item.key }}" value: "{{ item.value | string }}" notify: restart grafana - name: configure grafana analytics loop: "{{ grafana_config_analytics | dict2items }}" loop_control: label: "{{ item.key }}" ini_file: path: /etc/grafana/grafana.ini section: analytics option: "{{ item.key }}" value: "{{ item.value | string }}" notify: restart grafana - name: configure grafana security loop: "{{ grafana_config_security | dict2items }}" loop_control: label: "{{ item.key }}" ini_file: path: /etc/grafana/grafana.ini section: security option: "{{ item.key }}" value: "{{ item.value | string }}" notify: restart grafana - name: configure grafana users loop: "{{ grafana_config_users | dict2items }}" loop_control: label: "{{ item.key }}" ini_file: path: /etc/grafana/grafana.ini section: users option: "{{ item.key }}" value: "{{ item.value | string }}" notify: restart grafana - name: install datasources copy: dest: "/etc/grafana/provisioning/datasources/ansible.yml" content: | apiVersion: 1 deleteDatasources: [] {{ {'datasources': grafana_datasources} | to_nice_yaml(indent=2) }} group: grafana mode: 0640 no_log: yes notify: restart grafana - name: configure dashboard provisioning copy: dest: "/etc/grafana/provisioning/dashboards/ansible.yml" content: | apiVersion: 1 providers: - name: ansible' folder: '' options: path: /var/lib/grafana/dashboards/ansible foldersFromFilesStructure: true group: grafana mode: 0640 notify: restart grafana - name: create directory for dashboards file: path: /var/lib/grafana/dashboards/ansible state: directory group: grafana mode: 0750 - name: install dashboards from file loop: "{{ grafana_dashboards | selectattr('file', 'defined') }}" loop_control: label: "{{ item.file }} -> {{ item.datasource }}" copy: content: "{{ lookup('file', 'dashboard-'+item.file+'.json') | regex_replace('\"(?:\\${)?DS_[A-Z0-9_-]+(?:})?\"', '\"'+item.datasource+'\"') }}\n" dest: "/var/lib/grafana/dashboards/ansible/{{ item.datasource }}_{{ item.file }}.json" register: grafana_dasboards_from_file - name: install dashboards from content loop: "{{ grafana_dashboards | selectattr('content', 'defined') }}" loop_control: label: "{{ item.name }} -> {{ item.datasource }}" copy: content: "{{ item.content | regex_replace('\"(?:\\${)?DS_[A-Z0-9_-]+(?:})?\"', '\"'+item.datasource+'\"') }}\n" dest: "/var/lib/grafana/dashboards/ansible/{{ item.datasource }}_{{ item.name }}.json" register: grafana_dasboards_from_content ## TODO: implement this - name: install dashboards from grafana.com loop: "{{ grafana_dashboards | selectattr('id', 'defined') }}" loop_control: label: "https://grafana.com/api/dashboards/{{ item.id }} -> {{ item.datasource }}" debug: msg: 'installing dashboards from grafana.com is not yet supported!' # register: grafana_dasboards_from_grafanacom - name: fetch list of installed dashboards find: paths: /var/lib/grafana/dashboards/ansible patterns: "*.json" register: grafana_dashboards_installed - name: remove superfluous dashboards loop: "{{ grafana_dashboards_installed.files | map(attribute='path') | difference(grafana_dasboards_from_file.results | map(attribute='dest')) | difference(grafana_dasboards_from_content.results | map(attribute='dest')) | list }}" file: path: "{{ item }}" state: absent - name: make sure grafana-server is enabled and started systemd: name: grafana-server state: started enabled: yes - name: manage grafana users include_tasks: users.yml