diff --git a/roles/grafana_agent/defaults/main.yml b/roles/grafana_agent/defaults/main.yml new file mode 100644 index 00000000..fb65ef4a --- /dev/null +++ b/roles/grafana_agent/defaults/main.yml @@ -0,0 +1,72 @@ +--- +grafana_agent_version: latest +# grafana_agent_mode: "flow" +grafana_agent_mode: "static" +grafana_agent_package: "grafana-agent{% if grafana_agent_mode == 'flow' %}-flow{% endif %}{{ (grafana_agent_version != 'latest') | ternary('=' ~ grafana_agent_version, '') }}" +grafana_agent_manage_repo: true +grafana_agent_dependencies: [] + +# Grafana Agent Apt Repository Configuration +grafana_agent_apt_release_channel: stable +grafana_agent_apt_arch: "{{ 'arm64' if ansible_architecture == 'aarch64' else 'amd64' }}" +grafana_agent_apt_repo: "deb [arch={{ grafana_agent_apt_arch }} signed-by=/usr/share/keyrings/grafana.key] https://apt.grafana.com/ {{ grafana_agent_apt_release_channel }} main" +grafana_agent_apt_key: "https://apt.grafana.com/gpg.key" + +# Grafana Agent Yum Repository Configuration +grafana_agent_yum_repo: https://rpm.grafana.com +grafana_agent_yum_key: https://rpm.grafana.com/gpg.key + +grafana_agent_user_group: grafana-agent + +grafana_agent_static_config: + server: + log_level: warn + + metrics: + global: + scrape_interval: 1m + wal_directory: '/var/lib/grafana-agent' + configs: + # Example Prometheus scrape configuration to scrape the agent itself for metrics. + # This is not needed if the agent integration is enabled. + # - name: agent + # host_filter: false + # scrape_configs: + # - job_name: agent + # static_configs: + # - targets: ['127.0.0.1:9090'] + + integrations: + agent: + enabled: true + node_exporter: + enabled: true + include_exporter_metrics: true + disable_collectors: + - "mdadm" + +grafana_agent_flow_config: | + logging { + level = "warn" + } + + prometheus.exporter.unix "default" { + include_exporter_metrics = true + disable_collectors = ["mdadm"] + } + + prometheus.scrape "default" { + targets = concat( + prometheus.exporter.unix.default.targets, + [{ + // Self-collect metrics + job = "agent", + __address__ = "127.0.0.1:12345", + }], + ) + + forward_to = [ + // TODO: components to forward metrics to (like prometheus.remote_write or + // prometheus.relabel). + ] + } diff --git a/roles/grafana_agent/handlers/main.yaml b/roles/grafana_agent/handlers/main.yaml deleted file mode 100644 index b5b01aab..00000000 --- a/roles/grafana_agent/handlers/main.yaml +++ /dev/null @@ -1,12 +0,0 @@ ---- -- name: Restart Grafana Agent - become: true - ansible.builtin.systemd: - 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.yaml - listen: "restart grafana-agent" diff --git a/roles/grafana_agent/handlers/main.yml b/roles/grafana_agent/handlers/main.yml new file mode 100644 index 00000000..d11adbb9 --- /dev/null +++ b/roles/grafana_agent/handlers/main.yml @@ -0,0 +1,19 @@ +--- +- name: Restart Grafana Agent with daemon reload + become: true + ansible.builtin.systemd: + name: grafana-agent{% if grafana_agent_mode == 'flow' %}-flow{% endif %} + state: restarted + daemon_reload: true + +- name: Restart Grafana Agent + become: true + ansible.builtin.systemd: + name: grafana-agent{% if grafana_agent_mode == 'flow' %}-flow{% endif %} + state: restarted + +- name: Reload Grafana Agent + become: true + ansible.builtin.systemd: + name: grafana-agent{% if grafana_agent_mode == 'flow' %}-flow{% endif %} + state: reloaded diff --git a/roles/grafana_agent/meta/main.yaml b/roles/grafana_agent/meta/main.yaml deleted file mode 100644 index 7ef93089..00000000 --- a/roles/grafana_agent/meta/main.yaml +++ /dev/null @@ -1,25 +0,0 @@ ---- -dependencies: [] - -galaxy_info: - role_name: grafana_agent - author: Ishan Jain - description: Ansible Role to deploy Grafana Agent on Linux hosts. - license: "GPL-3.0-or-later" - min_ansible_version: "2.11" - platforms: - - name: Fedora - versions: - - "all" - - name: Debian - versions: - - "all" - - name: Ubuntu - versions: - - "all" - - name: EL - versions: - - "all" - galaxy_tags: - - grafana - - observability diff --git a/roles/grafana_agent/molecule/default/converge.yml b/roles/grafana_agent/molecule/default/converge.yml new file mode 100644 index 00000000..08b1eadb --- /dev/null +++ b/roles/grafana_agent/molecule/default/converge.yml @@ -0,0 +1,6 @@ +--- +- name: Converge + hosts: all + gather_facts: true + roles: + - role: grafana_agent diff --git a/roles/grafana_agent/molecule/default/molecule.yml b/roles/grafana_agent/molecule/default/molecule.yml new file mode 100644 index 00000000..ed97d539 --- /dev/null +++ b/roles/grafana_agent/molecule/default/molecule.yml @@ -0,0 +1 @@ +--- diff --git a/roles/grafana_agent/molecule/grafana-agent-flow/converge.yml b/roles/grafana_agent/molecule/grafana-agent-flow/converge.yml new file mode 100644 index 00000000..710e157f --- /dev/null +++ b/roles/grafana_agent/molecule/grafana-agent-flow/converge.yml @@ -0,0 +1,8 @@ +--- +- name: Converge + hosts: all + gather_facts: true + vars: + grafana_agent_mode: flow + roles: + - role: grafana_agent diff --git a/roles/grafana_agent/molecule/grafana-agent-flow/molecule.yml b/roles/grafana_agent/molecule/grafana-agent-flow/molecule.yml new file mode 100644 index 00000000..ed97d539 --- /dev/null +++ b/roles/grafana_agent/molecule/grafana-agent-flow/molecule.yml @@ -0,0 +1 @@ +--- diff --git a/roles/grafana_agent/tasks/configure.yaml b/roles/grafana_agent/tasks/configure.yaml deleted file mode 100644 index 6e2bdf7f..00000000 --- a/roles/grafana_agent/tasks/configure.yaml +++ /dev/null @@ -1,51 +0,0 @@ ---- -# these tasks are ran in both install and configure, as directories could have changed -- name: Configure directories - ansible.builtin.import_tasks: install/directories.yaml - -- name: Create a symbolic link - ansible.builtin.file: - src: "{{ grafana_agent_install_dir }}/{{ grafana_agent_binary }}" - dest: "/usr/local/bin/{{ grafana_agent_binary }}" - owner: root - group: root - state: link - -- name: Overwrite/Create Grafana Agent service - ansible.builtin.template: - src: grafana-agent.service.j2 - dest: "{{ _grafana_agent_systemd_dir }}/{{ _grafana_agent_systemd_unit }}" - owner: root - group: root - mode: 0644 - notify: "restart grafana-agent" - -- name: Create the Service Environment file - ansible.builtin.template: - src: EnvironmentFile.j2 - dest: "{{ grafana_agent_config_dir }}/{{ grafana_agent_env_file }}" - owner: root - group: "{{ grafana_agent_user_group }}" - mode: 0640 - notify: "restart grafana-agent" - -- name: Create Grafana Agent config - ansible.builtin.template: - src: config.yaml.j2 - dest: "{{ grafana_agent_config_dir }}/{{ grafana_agent_config_filename }}" - force: true - 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 - ansible.builtin.copy: - src: "{{ grafana_agent_provisioned_config_file }}" - dest: "{{ grafana_agent_config_dir }}/{{ grafana_agent_config_filename }}" - owner: root - group: "{{ grafana_agent_user_group }}" - mode: 0640 - notify: "restart grafana-agent" - when: grafana_agent_provisioned_config_file | length > 0 diff --git a/roles/grafana_agent/tasks/configure.yml b/roles/grafana_agent/tasks/configure.yml new file mode 100644 index 00000000..3d58bb67 --- /dev/null +++ b/roles/grafana_agent/tasks/configure.yml @@ -0,0 +1,20 @@ +--- +- name: Create Grafana Agent config - static + when: grafana_agent_mode == 'static' + ansible.builtin.template: + src: grafana-agent-static.yaml.j2 + dest: /etc/grafana-agent.yaml + owner: root + group: "{{ grafana_agent_user_group }}" + mode: "0640" + notify: Restart Grafana Agent + +- name: Create Grafana Agent config - flow + when: grafana_agent_mode == 'flow' + ansible.builtin.template: + src: grafana-agent-flow.river.j2 + dest: /etc/grafana-agent-flow.river + owner: root + group: "{{ grafana_agent_user_group }}" + mode: "0640" + notify: Reload Grafana Agent diff --git a/roles/grafana_agent/tasks/ga-started.yaml b/roles/grafana_agent/tasks/ga-started.yaml deleted file mode 100644 index cf66893d..00000000 --- a/roles/grafana_agent/tasks/ga-started.yaml +++ /dev/null @@ -1,29 +0,0 @@ ---- -- 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 - when: not ansible_check_mode - -- name: Check system logs if Grafana Agent is not started - when: not ansible_check_mode and _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." diff --git a/roles/grafana_agent/tasks/install.yaml b/roles/grafana_agent/tasks/install.yaml deleted file mode 100644 index ff101eb7..00000000 --- a/roles/grafana_agent/tasks/install.yaml +++ /dev/null @@ -1,19 +0,0 @@ ---- -# user and group creation -- name: Configure user groups - ansible.builtin.import_tasks: install/user-group.yaml - -# directory creation -- name: Configure directories - ansible.builtin.import_tasks: install/directories.yaml - -# download and install agent -- name: Download and install Grafana Agent - ansible.builtin.import_tasks: install/download-install.yaml - when: (grafana_agent_local_binary_file is not defined) or (grafana_agent_local_binary_file | length == 0) - -# local install of agent -- name: Local install of Grafana Agent - ansible.builtin.import_tasks: - file: install/local-install.yaml - when: __grafana_agent_local_install diff --git a/roles/grafana_agent/tasks/install.yml b/roles/grafana_agent/tasks/install.yml new file mode 100644 index 00000000..caa879c0 --- /dev/null +++ b/roles/grafana_agent/tasks/install.yml @@ -0,0 +1,60 @@ +--- +- name: Gather variables for each operating system + ansible.builtin.include_vars: "{{ distrovars }}" + vars: + distrovars: "{{ lookup('first_found', params, errors='ignore') }}" + params: + skip: true + files: + - "{{ 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" + paths: + - "vars/" + tags: + - grafana_agent_install + - grafana_agent_configure + +- name: Install dependencies + ansible.builtin.package: + name: "{{ grafana_agent_dependencies }}" + state: present + +- name: Prepare apt + when: + - "ansible_pkg_mgr == 'apt'" + - grafana_agent_manage_repo + block: + - name: Import Grafana apt gpg key + ansible.builtin.get_url: + url: "{{ grafana_agent_apt_key }}" + dest: /usr/share/keyrings/grafana.key + mode: "0644" + + - name: Add Grafana apt repository + ansible.builtin.apt_repository: + repo: "{{ grafana_agent_apt_repo }}" + state: present + update_cache: true + +- name: Add Grafana yum/dnf repository + when: + - "ansible_pkg_mgr in ['yum', 'dnf']" + - grafana_agent_manage_repo + ansible.builtin.yum_repository: + name: grafana + description: grafana + baseurl: "{{ grafana_agent_yum_repo }}" + enabled: true + gpgkey: "{{ grafana_agent_yum_key | default(omit) }}" + sslverify: true + repo_gpgcheck: "{{ true if (grafana_agent_yum_key) else omit }}" + gpgcheck: "{{ true if (grafana_agent_yum_key) else omit }}" + +- name: "Install Grafana Agent" + ansible.builtin.package: + name: "{{ grafana_agent_package }}" + state: "{{ (grafana_agent_version == 'latest') | ternary('latest', 'present') }}" + notify: Restart Grafana Agent diff --git a/roles/grafana_agent/tasks/install/directories.yaml b/roles/grafana_agent/tasks/install/directories.yaml deleted file mode 100644 index 13f3851a..00000000 --- a/roles/grafana_agent/tasks/install/directories.yaml +++ /dev/null @@ -1,26 +0,0 @@ ---- -- name: Create install directories - block: - - name: Create Grafana Agent install directory - ansible.builtin.file: - path: "{{ grafana_agent_install_dir }}" - state: directory - owner: root - group: "{{ grafana_agent_user_group }}" - mode: 0770 - - - name: Create Grafana Agent conf directory - ansible.builtin.file: - path: "{{ grafana_agent_config_dir }}" - state: directory - owner: root - group: "{{ grafana_agent_user_group }}" - mode: 0770 - - - name: Create Grafana Agent data directory - ansible.builtin.file: - path: "{{ grafana_agent_data_dir }}" - state: directory - owner: root - group: "{{ grafana_agent_user_group }}" - mode: 0775 diff --git a/roles/grafana_agent/tasks/install/download-install.yaml b/roles/grafana_agent/tasks/install/download-install.yaml deleted file mode 100644 index 6cb1c96b..00000000 --- a/roles/grafana_agent/tasks/install/download-install.yaml +++ /dev/null @@ -1,48 +0,0 @@ ---- -- name: Download Grafana Agent binary to controller (localhost) - block: - - name: Create Grafana Agent temp directory - become: false - ansible.builtin.file: - path: "{{ grafana_agent_local_tmp_dir }}" - state: directory - mode: 0751 - delegate_to: localhost - check_mode: false - run_once: true - - - name: Download Grafana Agent archive to local folder - become: false - ansible.builtin.get_url: - url: "{{ _grafana_agent_download_url }}" - dest: "{{ grafana_agent_local_tmp_dir }}/grafana-agent_{{ _grafana_agent_cpu_arch }}_{{ grafana_agent_version }}.zip" - mode: 0664 - register: _download_archive - until: _download_archive is succeeded - retries: 5 - delay: 2 - delegate_to: localhost - check_mode: false - run_once: true - - - name: Extract grafana-agent.zip - become: false - ansible.builtin.unarchive: - src: "{{ grafana_agent_local_tmp_dir }}/grafana-agent_{{ _grafana_agent_cpu_arch }}_{{ grafana_agent_version }}.zip" - dest: "{{ grafana_agent_local_tmp_dir }}" - remote_src: false - delegate_to: localhost - run_once: true - - - name: Set local path - ansible.builtin.set_fact: - __grafana_agent_local_binary_file: "{{ grafana_agent_local_tmp_dir }}/{{ grafana_agent_binary }}" - - - name: Propagate downloaded binary - ansible.builtin.copy: - src: "{{ grafana_agent_local_tmp_dir }}/{{ _grafana_agent_download_binary_file }}" - dest: "{{ grafana_agent_install_dir }}/{{ grafana_agent_binary }}" - mode: 0755 - owner: root - group: root - when: not ansible_check_mode diff --git a/roles/grafana_agent/tasks/install/local-install.yaml b/roles/grafana_agent/tasks/install/local-install.yaml deleted file mode 100644 index a2859bb4..00000000 --- a/roles/grafana_agent/tasks/install/local-install.yaml +++ /dev/null @@ -1,10 +0,0 @@ ---- -- name: Install from local - block: - - name: "Propagate local binary {{ grafana_agent_local_binary_file }}" - ansible.builtin.copy: - src: "{{ grafana_agent_local_binary_file }}" - dest: "{{ grafana_agent_install_dir }}/{{ grafana_agent_binary }}" - mode: 0755 - owner: root - group: root diff --git a/roles/grafana_agent/tasks/install/user-group.yaml b/roles/grafana_agent/tasks/install/user-group.yaml deleted file mode 100644 index 7b6ba7a0..00000000 --- a/roles/grafana_agent/tasks/install/user-group.yaml +++ /dev/null @@ -1,38 +0,0 @@ ---- -- name: Grafana Agent group creation - block: - - name: "Check if the group exists ({{ grafana_agent_user_group }})" - ansible.builtin.getent: - database: group - key: "{{ grafana_agent_user_group }}" - fail_key: false - - - name: Set whether not the user group exists - ansible.builtin.set_fact: - __grafana_agent_user_group_exists: "{{ ansible_facts.getent_group[grafana_agent_user_group] is not none }}" - - - name: Add user group "{{ grafana_agent_user_group }}" - ansible.builtin.group: - name: "{{ grafana_agent_user_group }}" - system: true - state: present - when: not __grafana_agent_user_group_exists and grafana_agent_user_group != 'root' - - - name: Grafana Agent user group exists - ansible.builtin.debug: - msg: |- - The user group \"{{ grafana_agent_user_group }}\" already exists and will not be modified, - if modifying permissions please perform a separate task - when: __grafana_agent_user_group_exists - -- name: Grafana Agent user creation - block: - - name: Add user "{{ grafana_agent_user }}" - ansible.builtin.user: - name: "{{ grafana_agent_user }}" - comment: "Grafana Agent account" - groups: "{{ [ grafana_agent_user_group ] + grafana_agent_user_groups }}" - system: true - shell: "{{ grafana_agent_user_shell }}" - createhome: "{{ grafana_agent_user_createhome }}" - when: grafana_agent_user != 'root' diff --git a/roles/grafana_agent/tasks/main.yaml b/roles/grafana_agent/tasks/main.yml similarity index 51% rename from roles/grafana_agent/tasks/main.yaml rename to roles/grafana_agent/tasks/main.yml index 7677b0a6..6a546839 100644 --- a/roles/grafana_agent/tasks/main.yaml +++ b/roles/grafana_agent/tasks/main.yml @@ -1,48 +1,37 @@ --- -- name: Preflight tasks +- name: Preflight ansible.builtin.include_tasks: - file: preflight.yaml + file: preflight.yml apply: - become: true tags: - grafana_agent_install - grafana_agent_configure - - grafana_agent_run - tags: - - grafana_agent_install - - grafana_agent_configure - - grafana_agent_run -- name: Install tasks +- name: Install ansible.builtin.include_tasks: - file: install.yaml + file: install.yml apply: become: true tags: - grafana_agent_install - tags: - - grafana_agent_install -- name: Configuration tasks +- name: Configure ansible.builtin.include_tasks: - file: configure.yaml + file: configure.yml apply: become: true tags: - grafana_agent_configure - tags: - - grafana_agent_configure - name: Flush handlers ansible.builtin.meta: flush_handlers -- name: Ensure Grafana Agent is started and enabled on boot +- name: Start and enable Grafana Agent become: true - ansible.builtin.systemd: - name: grafana-agent - enabled: true + ansible.builtin.service: + name: grafana-agent{% if grafana_agent_mode == 'flow' %}-flow{% endif %} state: started + enabled: true tags: - grafana_agent_install - grafana_agent_configure - - grafana_agent_run diff --git a/roles/grafana_agent/tasks/preflight.yaml b/roles/grafana_agent/tasks/preflight.yaml deleted file mode 100644 index 09b86d6b..00000000 --- a/roles/grafana_agent/tasks/preflight.yaml +++ /dev/null @@ -1,12 +0,0 @@ ---- -- name: Preflight variable checks - ansible.builtin.import_tasks: preflight/vars.yaml - -- name: Systemd variable checks - ansible.builtin.import_tasks: preflight/systemd.yaml - -- name: Install variable checks - ansible.builtin.import_tasks: preflight/install.yaml - -- name: Download variable checks - ansible.builtin.import_tasks: preflight/download.yaml diff --git a/roles/grafana_agent/tasks/preflight.yml b/roles/grafana_agent/tasks/preflight.yml new file mode 100644 index 00000000..ed97d539 --- /dev/null +++ b/roles/grafana_agent/tasks/preflight.yml @@ -0,0 +1 @@ +--- diff --git a/roles/grafana_agent/tasks/preflight/download.yaml b/roles/grafana_agent/tasks/preflight/download.yaml deleted file mode 100644 index d885c9b6..00000000 --- a/roles/grafana_agent/tasks/preflight/download.yaml +++ /dev/null @@ -1,36 +0,0 @@ ---- -- name: Get Grafana Agent version from Github - when: grafana_agent_version == 'latest' and not __grafana_agent_local_install - block: - - name: Get the latest published Grafana Agent # noqa command-instead-of-module - ansible.builtin.shell: | - curl -s https://api.github.com/repos/{{ _grafana_agent_github_org }}/{{ _grafana_agent_github_repo }}/releases/latest \ - | grep -m 1 tag_name \ - | cut -d '"' -f 4 | cut -c 2- - changed_when: false - run_once: true - delegate_to: localhost - become: false - register: _grafana_agent_version_request - - - name: Fail if cannot get Grafana Agent Version - ansible.builtin.fail: - msg: Issue getting the Grafana Agent Version - when: _grafana_agent_version_request == "" - - - name: Set the Grafana Agent version - ansible.builtin.set_fact: - grafana_agent_version: "{{ _grafana_agent_version_request.stdout }}" - - - name: Grafana Agent version to download - ansible.builtin.debug: - var: grafana_agent_version - -- name: Set the Grafana Agent download URL - ansible.builtin.set_fact: - _grafana_agent_download_url: |- - {{ grafana_agent_base_download_url }}/v{{ grafana_agent_version }}/{{ _grafana_agent_download_archive_file }} - -- name: Grafana Agent download URL - ansible.builtin.debug: - var: _grafana_agent_download_url diff --git a/roles/grafana_agent/tasks/preflight/install.yaml b/roles/grafana_agent/tasks/preflight/install.yaml deleted file mode 100644 index 833ececc..00000000 --- a/roles/grafana_agent/tasks/preflight/install.yaml +++ /dev/null @@ -1,72 +0,0 @@ ---- -- name: Default to non-local install - ansible.builtin.set_fact: - __grafana_agent_local_install: false - -- name: Fail when grafana_agent_local_binary_file is defined but the file doesn't exist - when: grafana_agent_local_binary_file is defined and grafana_agent_local_binary_file | length > 0 - block: - - name: Check if grafana_agent_local_binary_file exists - ansible.builtin.stat: - path: "{{ grafana_agent_local_binary_file }}" - register: __grafana_agent_local_binary_check - become: false - delegate_to: localhost - check_mode: false - - - name: Fail when the grafana_agent_local_binary_file does not exist - ansible.builtin.fail: - msg: "The grafana_agent_local_binary_file ({{ grafana_agent_local_binary_file }}) was specified but does not exist" - when: not __grafana_agent_local_binary_check.stat.exists - - - name: Change to local install - ansible.builtin.set_fact: - __grafana_agent_local_install: true - -- name: Fail when grafana_agent_provisioned_config_file is defined but the file doesn't exist - when: grafana_agent_provisioned_config_file is defined and grafana_agent_provisioned_config_file | length > 0 - block: - - name: Check if grafana_agent_provisioned_config_file exists - ansible.builtin.stat: - path: "{{ grafana_agent_provisioned_config_file }}" - register: __grafana_agent_provisioned_config_file_check - become: false - delegate_to: localhost - check_mode: false - - - name: Fail when the grafana_agent_provisioned_config_file does not exist - ansible.builtin.fail: - msg: "The grafana_agent_provisioned_config_file ({{ grafana_agent_provisioned_config_file }}) was specified but does not exist" - when: not __grafana_agent_provisioned_config_file_check.stat.exists - -- name: Check if grafana_agent is already installed on the host - ansible.builtin.stat: - path: "{{ grafana_agent_install_dir }}/{{ grafana_agent_binary }}" - register: __grafana_agent_is_installed - check_mode: false - -- name: Is Grafana Agent already installed on the host - ansible.builtin.debug: - var: __grafana_agent_is_installed.stat.exists - -- name: Install checks - when: __grafana_agent_is_installed.stat.exists and not __grafana_agent_local_install - block: - - name: Gather currently installed grafana-agent version from the host - ansible.builtin.shell: - cmd: | - {{ grafana_agent_install_dir }}/{{ grafana_agent_binary }} --version | \ - head -n 1 | \ - awk '{ print $3; }' | \ - cut -d 'v' -f 2 - changed_when: false - register: __grafana_agent_current_version_output - check_mode: false - - - name: Set Grafana Agent installed version for the host - ansible.builtin.set_fact: - __grafana_agent_installed_version: "{{ __grafana_agent_current_version_output.stdout }}" - - - name: Grafana Agent installed version on host - ansible.builtin.debug: - var: __grafana_agent_installed_version diff --git a/roles/grafana_agent/tasks/preflight/systemd.yaml b/roles/grafana_agent/tasks/preflight/systemd.yaml deleted file mode 100644 index c66b62e9..00000000 --- a/roles/grafana_agent/tasks/preflight/systemd.yaml +++ /dev/null @@ -1,28 +0,0 @@ ---- -- name: Assert usage of systemd as an init system - ansible.builtin.assert: - that: ansible_service_mgr == 'systemd' - msg: This role only works with systemd - -- name: Get systemd version # noqa command-instead-of-module - ansible.builtin.command: systemctl --version - changed_when: false - check_mode: false - register: __systemd_version - -- name: Set systemd version fact - ansible.builtin.set_fact: - grafana_agent_systemd_version: "{{ __systemd_version.stdout_lines[0] | regex_replace('^systemd\\s(\\d+).*$', '\\1') }}" - -- name: Fail when _grafana_agent_systemd_dir the directory doesn't exist - block: - - name: Check if _grafana_agent_systemd_dir exists - ansible.builtin.stat: - path: "{{ _grafana_agent_systemd_dir }}" - register: ___grafana_agent_systemd_dir_check - check_mode: false - - - name: Fail when the _grafana_agent_systemd_dir directory does not exist - ansible.builtin.fail: - msg: "The _grafana_agent_systemd_dir ({{ _grafana_agent_systemd_dir }}) does not exist" - when: not ___grafana_agent_systemd_dir_check.stat.exists diff --git a/roles/grafana_agent/tasks/preflight/vars.yaml b/roles/grafana_agent/tasks/preflight/vars.yaml deleted file mode 100644 index dc133a4a..00000000 --- a/roles/grafana_agent/tasks/preflight/vars.yaml +++ /dev/null @@ -1,72 +0,0 @@ ---- -# Performs initial variable validation -- name: Fail when variables are not defined - ansible.builtin.fail: - msg: "The {{ item }} property must be set" - when: ( vars[item] is not defined ) - with_items: - - grafana_agent_version - - grafana_agent_install_dir - - grafana_agent_binary - - grafana_agent_config_dir - - grafana_agent_config_filename - - grafana_agent_env_file - - grafana_agent_local_tmp_dir - - grafana_agent_wal_dir - - grafana_agent_positions_dir - - grafana_agent_mode - - _grafana_agent_systemd_dir - - _grafana_agent_systemd_unit - - grafana_agent_user - - grafana_agent_user_group - - grafana_agent_user_shell - - grafana_agent_user_createhome - - grafana_agent_local_binary_file - - grafana_agent_flags_extra - - grafana_agent_env_vars - - grafana_agent_env_file_vars - - grafana_agent_provisioned_config_file - - grafana_agent_metrics_config - - grafana_agent_logs_config - - grafana_agent_traces_config - - grafana_agent_integrations_config - -- name: Fail when variables do not have a length - ansible.builtin.fail: - msg: "The {{ item }} property must be valued" - when: ( vars[item] | string | length == 0 ) - with_items: - - grafana_agent_version - - grafana_agent_install_dir - - grafana_agent_binary - - grafana_agent_config_dir - - grafana_agent_config_filename - - grafana_agent_env_file - - grafana_agent_local_tmp_dir - - grafana_agent_wal_dir - - grafana_agent_positions_dir - - grafana_agent_mode - - _grafana_agent_systemd_dir - - _grafana_agent_systemd_unit - - grafana_agent_user - - grafana_agent_user_group - - grafana_agent_user_shell - - grafana_agent_user_createhome - -- name: Fail when variables are not a number - ansible.builtin.fail: - msg: "The {{ item }} property must be number" - when: ( vars[item] is defined and vars[item] is not number) - with_items: [] - -- name: Fail when flags are not a boolean - ansible.builtin.fail: - msg: "The {{ item }} property must be a boolean true or false" - when: ( vars[item] | bool | string | lower ) not in ['true', 'false'] - with_items: - - grafana_agent_user_createhome - -- name: Fail when the agent mode is not "flow" or "static" - ansible.builtin.fail: - msg: "The grafana_agent_mode property must be a boolean 'flow' or 'static'" - when: grafana_agent_mode not in ['flow', 'static'] diff --git a/roles/grafana_agent/templates/EnvironmentFile.j2 b/roles/grafana_agent/templates/EnvironmentFile.j2 deleted file mode 100644 index 875d941a..00000000 --- a/roles/grafana_agent/templates/EnvironmentFile.j2 +++ /dev/null @@ -1,9 +0,0 @@ -{{ ansible_managed | comment }} -# Grafana Agent Environment File -AGENT_MODE={{ grafana_agent_mode }} - -GOMAXPROCS={{ ansible_processor_vcpus|default(ansible_processor_count) }} - -{% for key, value in grafana_agent_env_file_vars.items() %} -{{key}}={{value}} -{% endfor %} diff --git a/roles/grafana_agent/templates/config.yaml.j2 b/roles/grafana_agent/templates/config.yaml.j2 deleted file mode 100644 index 0e427756..00000000 --- a/roles/grafana_agent/templates/config.yaml.j2 +++ /dev/null @@ -1,37 +0,0 @@ ---- -{{ ansible_managed | comment }} - -################################################################################################# -# Configures the server of the Agent used to enable self-scraping # -################################################################################################# -# Docs: https://grafana.com/docs/agent/latest/configuration/server-config/ -server: - {{ grafana_agent_server_config | to_nice_yaml(indent=2,sort_keys=False) | indent(2) }} - -################################################################################################# -# Configures metric collection # -################################################################################################# -# Docs: https://grafana.com/docs/agent/latest/configuration/metrics-config/ -metrics: - {{ grafana_agent_metrics_config | to_nice_yaml(indent=2,sort_keys=False) | indent(2) }} - -################################################################################################# -# Configures logs collection # -################################################################################################# -# Docs: https://grafana.com/docs/agent/latest/configuration/logs-config/ -logs: - {{ grafana_agent_logs_config | to_nice_yaml(indent=2,sort_keys=False) | indent(2) }} - -################################################################################################# -# Configures traces collection # -################################################################################################# -# Docs: https://grafana.com/docs/agent/latest/configuration/traces-config/ -traces: - {{ grafana_agent_traces_config | to_nice_yaml(indent=2,sort_keys=False) | indent(2) }} - -################################################################################################# -# Configures integrations for the agent # -################################################################################################# -# Docs: https://grafana.com/docs/agent/latest/configuration/integrations/ -integrations: - {{ grafana_agent_integrations_config | to_nice_yaml(indent=2,sort_keys=False) | indent(2) }} diff --git a/roles/grafana_agent/templates/grafana-agent-flow.river.j2 b/roles/grafana_agent/templates/grafana-agent-flow.river.j2 new file mode 100644 index 00000000..63438110 --- /dev/null +++ b/roles/grafana_agent/templates/grafana-agent-flow.river.j2 @@ -0,0 +1,3 @@ +{{ ansible_managed | comment('c') }} +// For a full configuration reference, see https://grafana.com/docs/agent/latest/flow/ +{{ grafana_agent_flow_config }} diff --git a/roles/grafana_agent/templates/grafana-agent-static.yaml.j2 b/roles/grafana_agent/templates/grafana-agent-static.yaml.j2 new file mode 100644 index 00000000..1606d02e --- /dev/null +++ b/roles/grafana_agent/templates/grafana-agent-static.yaml.j2 @@ -0,0 +1,5 @@ +--- +{{ ansible_managed | comment }} +# For a full configuration reference, see: https://grafana.com/docs/agent/latest/configuration/. +{# The to_nice_yaml filter behaves strange therefore we first read in as yaml and then print as yaml again #} +{{ grafana_agent_static_config | from_yaml | to_nice_yaml(sort_keys=False, width=1000,indent=2) }} diff --git a/roles/grafana_agent/vars/debian.yml b/roles/grafana_agent/vars/debian.yml new file mode 100644 index 00000000..cdd38a0e --- /dev/null +++ b/roles/grafana_agent/vars/debian.yml @@ -0,0 +1,5 @@ +_grafana_agent_dependencies: + - apt-transport-https + - adduser + - ca-certificates + - gnupg2 diff --git a/roles/grafana_agent/vars/main.yaml b/roles/grafana_agent/vars/main.yaml deleted file mode 100644 index 75353fc8..00000000 --- a/roles/grafana_agent/vars/main.yaml +++ /dev/null @@ -1,29 +0,0 @@ ---- -_grafana_agent_github_org: grafana -_grafana_agent_github_repo: agent - -# set the go cpu arch -_download_cpu_arch_map: - i386: '386' - x86_64: amd64 - aarch64: arm64 - armv7l: armv7 - armv6l: armv6 - -_grafana_agent_cpu_arch: "{{ _download_cpu_arch_map[ansible_architecture] | default(ansible_architecture) }}" - -# set the go os family -_grafana_agent_os_family: "{{ ansible_system | lower }}" - -# set the name of the archive file to download -_grafana_agent_download_archive_file: "grafana-agent-{{ _grafana_agent_os_family }}-{{ _grafana_agent_cpu_arch }}.zip" - -# set the name of the binary file -_grafana_agent_download_binary_file: "grafana-agent-{{ _grafana_agent_os_family }}-{{ _grafana_agent_cpu_arch }}" - -# 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" diff --git a/roles/grafana_agent/vars/redhat.yml b/roles/grafana_agent/vars/redhat.yml new file mode 100644 index 00000000..af97ce0f --- /dev/null +++ b/roles/grafana_agent/vars/redhat.yml @@ -0,0 +1,4 @@ +--- +# https://unix.stackexchange.com/questions/534463/cant-enable-grafana-on-boot-in-fedora-because-systemd-sysv-install-missing +grafana_agent_dependencies: + - chkconfig