Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make grafana_agent Idempotent #92

Merged
merged 7 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions roles/grafana_agent/handlers/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
- name: Restart Grafana Agent
ansible.builtin.service:
name: grafana-agent
state: restarted
daemon_reload: true
listen: "restart grafana-agent"

- name: Check Grafana Agent is started properly
ansible.builtin.include_tasks: ga-started.yml
listen: "restart grafana-agent"
22 changes: 4 additions & 18 deletions roles/grafana_agent/tasks/configure.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
---
- name: Stop grafana-agent if installed
block:
# this will fail the verify first time of installation if local binary is used
- name: Ensure grafana-agent is stopped
ansible.builtin.systemd:
name: grafana-agent
enabled: true
state: stopped
rescue:
# make sure that the service isn't actually installed
- name: Get the list of services
ansible.builtin.service_facts:

- name: Verify that grafana-agent is not installed
ansible.builtin.assert:
that:
- _grafana_agent_systemd_unit not in services

# these tasks are ran in both install and configure, as directories could have changed
- name: Configure directories
ansible.builtin.import_tasks: install/directories.yaml
Expand All @@ -36,6 +18,7 @@
owner: root
group: root
mode: 0644
notify: "restart grafana-agent"

- name: Create the Service Environment file
ansible.builtin.template:
Expand All @@ -44,6 +27,7 @@
owner: root
group: "{{ grafana_agent_user_group }}"
mode: 0640
notify: "restart grafana-agent"

- name: Create Grafana Agent config
ansible.builtin.template:
Expand All @@ -53,6 +37,7 @@
owner: root
group: "{{ grafana_agent_user_group }}"
mode: 0640
notify: "restart grafana-agent"
when: grafana_agent_provisioned_config_file | length == 0

- name: Copy Grafana Agent config
Expand All @@ -62,4 +47,5 @@
owner: root
group: "{{ grafana_agent_user_group }}"
mode: 0640
notify: "restart grafana-agent"
when: grafana_agent_provisioned_config_file | length > 0
28 changes: 28 additions & 0 deletions roles/grafana_agent/tasks/ga-started.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
- name: Health check Grafana Agent
ansible.builtin.uri:
url: "http://127.0.0.1:12345/-/ready"
follow_redirects: none
method: GET
register: _result
failed_when: false
until: _result.status == 200
retries: 3
delay: 2
changed_when: false

- name: Check system logs if Grafana Agent is not started
when: _result.status != 200
block:
- name: Run journalctl

Check failure on line 17 in roles/grafana_agent/tasks/ga-started.yaml

View workflow job for this annotation

GitHub Actions / Perform Linting

command-instead-of-shell

Use shell only when shell functionality is required.
ansible.builtin.shell:
cmd: "journalctl -u grafana-agent -b -n20 --no-pager"
register: journal_ret
changed_when: false
- name: Output Grafana agent logs
ansible.builtin.debug:
var: journal_ret.stdout_lines
- name: Rise alerts
ansible.builtin.assert:
that: false
fail_msg: "Service grafana-agent hasn't started."
5 changes: 5 additions & 0 deletions roles/grafana_agent/tasks/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@
name: grafana-agent
enabled: true
state: started
failed_when: false
changed_when: false
tags:
- grafana_agent_install
- grafana_agent_configure
- grafana_agent_run

- name: Check Grafana Agent is started properly
ansible.builtin.include_tasks: ga-started.yml
Loading