From 468a0ebe33469c9e7ddfcca9b9dda8884bcfbd54 Mon Sep 17 00:00:00 2001 From: Steve Brasier Date: Tue, 19 Mar 2024 16:57:21 +0000 Subject: [PATCH 1/4] create runtime taskfile --- tasks/{main.yml => runtime.yml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tasks/{main.yml => runtime.yml} (100%) diff --git a/tasks/main.yml b/tasks/runtime.yml similarity index 100% rename from tasks/main.yml rename to tasks/runtime.yml From 242b91b692cfa2f67e2823235028f331998a2ac5 Mon Sep 17 00:00:00 2001 From: Steve Brasier Date: Tue, 19 Mar 2024 16:59:48 +0000 Subject: [PATCH 2/4] recreate main.yml --- tasks/install.yml | 15 +++++++++++++++ tasks/main.yml | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 tasks/main.yml diff --git a/tasks/install.yml b/tasks/install.yml index 29fdcba..b2aab6a 100644 --- a/tasks/install.yml +++ b/tasks/install.yml @@ -1,5 +1,20 @@ --- +- name: Gather variables for each operating system + include_vars: "{{ item }}" + with_first_found: + - "{{ ansible_distribution | lower }}-{{ ansible_distribution_version | lower }}.yml" + - "{{ ansible_distribution | lower }}-{{ ansible_distribution_major_version | lower }}.yml" + - "{{ ansible_os_family | lower }}-{{ ansible_distribution_major_version | lower }}.yml" + - "{{ ansible_distribution | lower }}.yml" + - "{{ ansible_os_family | lower }}.yml" + tags: + - grafana_install + - grafana_configure + - grafana_datasources + - grafana_notifications + - grafana_dashboards + - block: - name: Update apt cache apt: diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..b40267c --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,33 @@ +--- +- name: Gather variables for each operating system + include_vars: "{{ item }}" + with_first_found: + - "{{ ansible_distribution | lower }}-{{ ansible_distribution_version | lower }}.yml" + - "{{ ansible_distribution | lower }}-{{ ansible_distribution_major_version | lower }}.yml" + - "{{ ansible_os_family | lower }}-{{ ansible_distribution_major_version | lower }}.yml" + - "{{ ansible_distribution | lower }}.yml" + - "{{ ansible_os_family | lower }}.yml" + tags: + - grafana_install + - grafana_configure + - grafana_datasources + - grafana_notifications + - grafana_dashboards + +- include: preflight.yml + tags: + - grafana_install + - grafana_configure + - grafana_datasources + - grafana_notifications + - grafana_dashboards + +- include: install.yml + become: true + tags: + - grafana_install + +- include: runtime.yml + become: true + tags: + - grafana_runtime From b8a135b5ffe9cb8b60823d88c5a9ef47751fdc5d Mon Sep 17 00:00:00 2001 From: Steve Brasier Date: Tue, 19 Mar 2024 17:18:41 +0000 Subject: [PATCH 3/4] remove install from runtime --- tasks/runtime.yml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tasks/runtime.yml b/tasks/runtime.yml index 45c087a..0d32d96 100644 --- a/tasks/runtime.yml +++ b/tasks/runtime.yml @@ -16,17 +16,11 @@ - include: preflight.yml tags: - - grafana_install - grafana_configure - grafana_datasources - grafana_notifications - grafana_dashboards -- include: install.yml - become: true - tags: - - grafana_install - - include: configure.yml become: true tags: From b991ac22688a103b8c0f180b496158b3e3553455 Mon Sep 17 00:00:00 2001 From: Steve Brasier Date: Wed, 20 Mar 2024 09:00:14 +0000 Subject: [PATCH 4/4] provide toplevel 'configure' task file --- tasks/configure.yml | 174 ++++++++++++++++++++++----------------- tasks/core_configure.yml | 87 ++++++++++++++++++++ tasks/install.yml | 4 + tasks/main.yml | 15 ++-- tasks/runtime.yml | 107 ------------------------ 5 files changed, 193 insertions(+), 194 deletions(-) create mode 100644 tasks/core_configure.yml delete mode 100644 tasks/runtime.yml diff --git a/tasks/configure.yml b/tasks/configure.yml index 94e3b2a..bfd5d6d 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -1,87 +1,107 @@ --- -- name: Ensure grafana configuration directories exist - file: - path: "{{ item }}" - state: directory - owner: root - group: grafana - with_items: - - "/etc/grafana" - - "/etc/grafana/datasources" - - "/etc/grafana/provisioning" - - "/etc/grafana/provisioning/datasources" - - "/etc/grafana/provisioning/dashboards" - - "/etc/grafana/provisioning/notifiers" - - "/etc/grafana/provisioning/plugins" +- name: Gather variables for each operating system + include_vars: "{{ item }}" + with_first_found: + - "{{ ansible_distribution | lower }}-{{ ansible_distribution_version | lower }}.yml" + - "{{ ansible_distribution | lower }}-{{ ansible_distribution_major_version | lower }}.yml" + - "{{ ansible_os_family | lower }}-{{ ansible_distribution_major_version | lower }}.yml" + - "{{ ansible_distribution | lower }}.yml" + - "{{ ansible_os_family | lower }}.yml" + tags: + - grafana_install + - grafana_configure + - grafana_datasources + - grafana_notifications + - grafana_dashboards -- name: Create grafana main configuration file - template: - src: grafana.ini.j2 - dest: /etc/grafana/grafana.ini - owner: root - group: grafana - mode: 0640 - no_log: true - notify: restart grafana +- include: preflight.yml + tags: + - grafana_configure + - grafana_datasources + - grafana_notifications + - grafana_dashboards -- name: Create grafana LDAP configuration file - template: - src: ldap.toml.j2 - dest: "{{ grafana_auth.ldap.config_file | default('/etc/grafana/ldap.toml') }}" - owner: root - group: grafana - mode: 0640 - when: - - "'ldap' in grafana_auth" - - "'enabled' not in grafana_auth.ldap or grafana_auth.ldap.enabled" - no_log: true - notify: restart grafana +- include: core_configure.yml + become: true + tags: + - grafana_configure -- name: Create grafana directories - file: - path: "{{ item }}" - state: directory - mode: 0755 - owner: "grafana" - group: "grafana" - with_items: - - "{{ grafana_logs_dir }}" - - "{{ grafana_data_dir }}" - - "{{ grafana_data_dir }}/dashboards" - - "{{ grafana_data_dir }}/plugins" +- include: plugins.yml + when: grafana_plugins != [] + tags: + - grafana_configure -- block: - - name: Create grafana socket directory - file: - path: "{{ grafana_server.socket | dirname }}" - state: directory - mode: 0775 - owner: "grafana" - group: "grafana" +- name: Restart grafana before configuring datasources and dashboards + meta: flush_handlers + tags: + - grafana_install + - grafana_configure + - grafana_datasources + - grafana_notifications + - grafana_dashboards + - grafana_run - - name: Ensure grafana socket directory created on startup - template: - src: tmpfiles.j2 - dest: /etc/tmpfiles.d/grafana.conf - owner: root - group: root - mode: 0644 +- name: Wait for grafana to start (http/s) + wait_for: + host: "{{ grafana_address }}" + port: "{{ grafana_port }}" when: - - grafana_server.protocol is defined and grafana_server.protocol == 'socket' - - grafana_server.socket | dirname != '/var/run' + - grafana_state != 'stopped' + - grafana_server.protocol is undefined or grafana_server.protocol in ['http', 'https'] + tags: + - grafana_install + - grafana_configure + - grafana_datasources + - grafana_notifications + - grafana_dashboards + - grafana_run -- name: Enable grafana to ports lower than port 1024 - capabilities: - path: /usr/sbin/grafana-server - capability: CAP_NET_BIND_SERVICE+ep - state: present +- name: Wait for grafana to start (socket) + wait_for: + path: "{{ grafana_server.socket }}" when: - - grafana_port | int <= 1024 - - grafana_cap_net_bind_service + - grafana_state != 'stopped' + - grafana_server.protocol is defined and grafana_server.protocol == 'socket' + tags: + - grafana_install + - grafana_configure + - grafana_datasources + - grafana_notifications + - grafana_dashboards + - grafana_run + +- include: api_keys.yml + when: grafana_api_keys | length > 0 + tags: + - grafana_configure + - grafana_run + +- include: datasources.yml + when: grafana_datasources != [] + tags: + - grafana_configure + - grafana_datasources + - grafana_run + +- include: notifications.yml + when: grafana_alert_notifications | length > 0 + tags: + - grafana_configure + - grafana_notifications + - grafana_run + +- name: "Check if there are any dashboards in local {{ grafana_dashboards_dir }}" + become: false + set_fact: + found_dashboards: "{{ lookup('fileglob', grafana_dashboards_dir + '/*.json', wantlist=True) }}" + tags: + - grafana_configure + - grafana_dashboards + - grafana_run -- name: Enable and start Grafana systemd unit - systemd: - name: grafana-server - enabled: "{{ grafana_enabled }}" - state: "{{ 'stopped' if grafana_state == 'stopped' else 'started' }}" - daemon_reload: true +- include: dashboards.yml + when: grafana_dashboards | length > 0 or found_dashboards | length > 0 + tags: + - grafana_configure + - grafana_dashboards + - grafana_run diff --git a/tasks/core_configure.yml b/tasks/core_configure.yml new file mode 100644 index 0000000..94e3b2a --- /dev/null +++ b/tasks/core_configure.yml @@ -0,0 +1,87 @@ +--- +- name: Ensure grafana configuration directories exist + file: + path: "{{ item }}" + state: directory + owner: root + group: grafana + with_items: + - "/etc/grafana" + - "/etc/grafana/datasources" + - "/etc/grafana/provisioning" + - "/etc/grafana/provisioning/datasources" + - "/etc/grafana/provisioning/dashboards" + - "/etc/grafana/provisioning/notifiers" + - "/etc/grafana/provisioning/plugins" + +- name: Create grafana main configuration file + template: + src: grafana.ini.j2 + dest: /etc/grafana/grafana.ini + owner: root + group: grafana + mode: 0640 + no_log: true + notify: restart grafana + +- name: Create grafana LDAP configuration file + template: + src: ldap.toml.j2 + dest: "{{ grafana_auth.ldap.config_file | default('/etc/grafana/ldap.toml') }}" + owner: root + group: grafana + mode: 0640 + when: + - "'ldap' in grafana_auth" + - "'enabled' not in grafana_auth.ldap or grafana_auth.ldap.enabled" + no_log: true + notify: restart grafana + +- name: Create grafana directories + file: + path: "{{ item }}" + state: directory + mode: 0755 + owner: "grafana" + group: "grafana" + with_items: + - "{{ grafana_logs_dir }}" + - "{{ grafana_data_dir }}" + - "{{ grafana_data_dir }}/dashboards" + - "{{ grafana_data_dir }}/plugins" + +- block: + - name: Create grafana socket directory + file: + path: "{{ grafana_server.socket | dirname }}" + state: directory + mode: 0775 + owner: "grafana" + group: "grafana" + + - name: Ensure grafana socket directory created on startup + template: + src: tmpfiles.j2 + dest: /etc/tmpfiles.d/grafana.conf + owner: root + group: root + mode: 0644 + when: + - grafana_server.protocol is defined and grafana_server.protocol == 'socket' + - grafana_server.socket | dirname != '/var/run' + +- name: Enable grafana to ports lower than port 1024 + capabilities: + path: /usr/sbin/grafana-server + capability: CAP_NET_BIND_SERVICE+ep + state: present + when: + - grafana_port | int <= 1024 + - grafana_cap_net_bind_service + +- name: Enable and start Grafana systemd unit + systemd: + name: grafana-server + enabled: "{{ grafana_enabled }}" + state: "{{ 'stopped' if grafana_state == 'stopped' else 'started' }}" + daemon_reload: true diff --git a/tasks/install.yml b/tasks/install.yml index b2aab6a..d4382f9 100644 --- a/tasks/install.yml +++ b/tasks/install.yml @@ -15,6 +15,10 @@ - grafana_notifications - grafana_dashboards +- include: preflight.yml + tags: + - grafana_install + - block: - name: Update apt cache apt: diff --git a/tasks/main.yml b/tasks/main.yml index b40267c..603cd0a 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -14,20 +14,15 @@ - grafana_notifications - grafana_dashboards -- include: preflight.yml - tags: - - grafana_install - - grafana_configure - - grafana_datasources - - grafana_notifications - - grafana_dashboards - - include: install.yml become: true tags: - grafana_install -- include: runtime.yml +- include: configure.yml become: true tags: - - grafana_runtime + - grafana_configure + - grafana_datasources + - grafana_notifications + - grafana_dashboards diff --git a/tasks/runtime.yml b/tasks/runtime.yml deleted file mode 100644 index 0d32d96..0000000 --- a/tasks/runtime.yml +++ /dev/null @@ -1,107 +0,0 @@ ---- -- name: Gather variables for each operating system - include_vars: "{{ item }}" - with_first_found: - - "{{ ansible_distribution | lower }}-{{ ansible_distribution_version | lower }}.yml" - - "{{ ansible_distribution | lower }}-{{ ansible_distribution_major_version | lower }}.yml" - - "{{ ansible_os_family | lower }}-{{ ansible_distribution_major_version | lower }}.yml" - - "{{ ansible_distribution | lower }}.yml" - - "{{ ansible_os_family | lower }}.yml" - tags: - - grafana_install - - grafana_configure - - grafana_datasources - - grafana_notifications - - grafana_dashboards - -- include: preflight.yml - tags: - - grafana_configure - - grafana_datasources - - grafana_notifications - - grafana_dashboards - -- include: configure.yml - become: true - tags: - - grafana_configure - -- include: plugins.yml - when: grafana_plugins != [] - tags: - - grafana_configure - -- name: Restart grafana before configuring datasources and dashboards - meta: flush_handlers - tags: - - grafana_install - - grafana_configure - - grafana_datasources - - grafana_notifications - - grafana_dashboards - - grafana_run - -- name: Wait for grafana to start (http/s) - wait_for: - host: "{{ grafana_address }}" - port: "{{ grafana_port }}" - when: - - grafana_state != 'stopped' - - grafana_server.protocol is undefined or grafana_server.protocol in ['http', 'https'] - tags: - - grafana_install - - grafana_configure - - grafana_datasources - - grafana_notifications - - grafana_dashboards - - grafana_run - -- name: Wait for grafana to start (socket) - wait_for: - path: "{{ grafana_server.socket }}" - when: - - grafana_state != 'stopped' - - grafana_server.protocol is defined and grafana_server.protocol == 'socket' - tags: - - grafana_install - - grafana_configure - - grafana_datasources - - grafana_notifications - - grafana_dashboards - - grafana_run - -- include: api_keys.yml - when: grafana_api_keys | length > 0 - tags: - - grafana_configure - - grafana_run - -- include: datasources.yml - when: grafana_datasources != [] - tags: - - grafana_configure - - grafana_datasources - - grafana_run - -- include: notifications.yml - when: grafana_alert_notifications | length > 0 - tags: - - grafana_configure - - grafana_notifications - - grafana_run - -- name: "Check if there are any dashboards in local {{ grafana_dashboards_dir }}" - become: false - set_fact: - found_dashboards: "{{ lookup('fileglob', grafana_dashboards_dir + '/*.json', wantlist=True) }}" - tags: - - grafana_configure - - grafana_dashboards - - grafana_run - -- include: dashboards.yml - when: grafana_dashboards | length > 0 or found_dashboards | length > 0 - tags: - - grafana_configure - - grafana_dashboards - - grafana_run