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 all commits
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
12 changes: 12 additions & 0 deletions roles/grafana_agent/handlers/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
- name: Restart Grafana Agent
become: true
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: "{{ _grafana_agent_healthcheck_endpoint }}"
follow_redirects: none
method: GET
register: _result
failed_when: false
until: _result.status == 200
retries: 3
delay: 5
changed_when: false

- name: Check system logs if Grafana Agent is not started
when: _result.status != 200
block:
- name: Run journalctl
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."
2 changes: 2 additions & 0 deletions roles/grafana_agent/tasks/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
name: grafana-agent
enabled: true
state: started
failed_when: false
changed_when: false
tags:
- grafana_agent_install
- grafana_agent_configure
Expand Down
3 changes: 3 additions & 0 deletions roles/grafana_agent/vars/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@
# systemd info
_grafana_agent_systemd_dir: /lib/systemd/system/
_grafana_agent_systemd_unit: grafana-agent.service

# Server http address, used in self health check after start
_grafana_agent_healthcheck_endpoint: "http://{{ grafana_agent_flags_extra['server.http.address'] if grafana_agent_flags_extra['server.http.address'] is defined else '127.0.0.1:12345' }}/-/ready"
ishanjainn marked this conversation as resolved.
Show resolved Hide resolved