From a948debbf8792eba5847699a857b6bf82b1bb855 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Tue, 30 Aug 2022 07:50:35 -0500 Subject: [PATCH] add promtail role --- roles/promtail/defaults/main.yaml | 51 ++++++++++++++++++++ roles/promtail/handlers/main.yaml | 6 +++ roles/promtail/tasks/configure.yaml | 49 +++++++++++++++++++ roles/promtail/tasks/default.yaml | 0 roles/promtail/tasks/install.yaml | 29 +++++++++++ roles/promtail/tasks/main.yaml | 30 ++++++++++++ roles/promtail/tasks/pre.yaml | 50 +++++++++++++++++++ roles/promtail/templates/promtail.service.j2 | 19 ++++++++ roles/promtail/templates/promtail.yaml.j2 | 21 ++++++++ roles/promtail/vars/default.yaml | 0 10 files changed, 255 insertions(+) create mode 100644 roles/promtail/defaults/main.yaml create mode 100644 roles/promtail/handlers/main.yaml create mode 100644 roles/promtail/tasks/configure.yaml create mode 100644 roles/promtail/tasks/default.yaml create mode 100644 roles/promtail/tasks/install.yaml create mode 100644 roles/promtail/tasks/main.yaml create mode 100644 roles/promtail/tasks/pre.yaml create mode 100644 roles/promtail/templates/promtail.service.j2 create mode 100644 roles/promtail/templates/promtail.yaml.j2 create mode 100644 roles/promtail/vars/default.yaml diff --git a/roles/promtail/defaults/main.yaml b/roles/promtail/defaults/main.yaml new file mode 100644 index 0000000..0028cf7 --- /dev/null +++ b/roles/promtail/defaults/main.yaml @@ -0,0 +1,51 @@ +--- +promtail_go_arch_map: + i386: '386' + x86_64: 'amd64' + +promtail_go_arch: "{{ promtail_go_arch_map[ansible_architecture] | default('amd64') }}" + +promtail_service_name: promtail.service +promtail_service_enabled: true +promtail_service_state: started + +promtail_version_regex: ^promtail, version ([\d.]+) + +promtail_github_project_url: https://github.com/grafana/loki +promtail_release_file: "promtail-{{ ansible_system | lower }}-{{ promtail_go_arch }}.zip" +promtail_release_url: "{{ promtail_github_project_url }}/releases/download/v{{ promtail_version }}/{{ promtail_release_file }}" +promtail_checksum_url: "{{ promtail_github_project_url }}/releases/download/v{{ promtail_version }}/SHA256SUMS" +promtail_download_path: "/tmp/{{ promtail_release_file }}" +promtail_unarchive_dest_path: /tmp +promtail_extracted_path: "{{ promtail_download_path | replace('.zip', '') }}" + +promtail_user: promtail +promtail_user_state: present +promtail_user_shell: /usr/sbin/nologin + +promtail_group: promtail +promtail_groups: adm +promtail_group_state: "{{ promtail_user_state | default('present') }}" + +promtail_config_path: /etc/promtail.yaml + +promtail_var_path: /var/lib/promtail +promtail_var_owner: "{{ promtail_user }}" +promtail_var_group: "{{ promtail_group }}" +promtail_var_mode: "0755" + +promtail_bin_path: /usr/local/bin + +promtail_auth_enabled: false + +promtail_server: + http_listen_port: 9080 + grpc_listen_port: 0 + +promtail_positions: + filename: "{{ promtail_var_path }}/positions.yaml" + +promtail_clients: + - url: http://localhost:3100/loki/api/v1/push + +# promtail_scrape_configs: [] diff --git a/roles/promtail/handlers/main.yaml b/roles/promtail/handlers/main.yaml new file mode 100644 index 0000000..d450f92 --- /dev/null +++ b/roles/promtail/handlers/main.yaml @@ -0,0 +1,6 @@ +--- +- name: restart promtail + systemd: + name: promtail.service + daemon_reload: true + state: restarted diff --git a/roles/promtail/tasks/configure.yaml b/roles/promtail/tasks/configure.yaml new file mode 100644 index 0000000..0792b70 --- /dev/null +++ b/roles/promtail/tasks/configure.yaml @@ -0,0 +1,49 @@ +--- +- name: create group + group: + name: "{{ promtail_group }}" + system: true + state: "{{ promtail_group_state | default('present') }}" + +- name: create user + user: + name: "{{ promtail_user }}" + system: true + shell: "{{ promtail_user_shell }}" + group: "{{ promtail_group }}" + groups: "{{ promtail_groups }}" + createhome: false + home: "{{ promtail_var_path }}" + state: "{{ promtail_user_state | default('present') }}" + +- name: configure + template: + src: promtail.yaml.j2 + dest: "{{ promtail_config_path }}" + owner: root + group: root + mode: 0444 + notify: restart promtail + +- name: create var path + file: + path: "{{ promtail_var_path }}" + state: directory + owner: "{{ promtail_var_owner }}" + group: "{{ promtail_var_group }}" + mode: "{{ promtail_var_mode }}" + +- name: configure systemd template + template: + src: "{{ promtail_service_name }}.j2" + dest: "/etc/systemd/system/{{ promtail_service_name }}" + owner: root + group: root + mode: 0444 + notify: restart promtail + +- name: manage service + service: + name: "{{ promtail_service_name }}" + enabled: "{{ promtail_service_enabled }}" + state: "{{ promtail_service_state }}" diff --git a/roles/promtail/tasks/default.yaml b/roles/promtail/tasks/default.yaml new file mode 100644 index 0000000..e69de29 diff --git a/roles/promtail/tasks/install.yaml b/roles/promtail/tasks/install.yaml new file mode 100644 index 0000000..0103d9f --- /dev/null +++ b/roles/promtail/tasks/install.yaml @@ -0,0 +1,29 @@ +--- +- block: + - name: download archive + get_url: + url: "{{ promtail_release_url }}" + dest: "{{ promtail_download_path }}" + checksum: "{{ promtail_checksum }}" + register: dl + until: dl is success + retries: 5 + delay: 10 + + - name: extract archive + unarchive: + src: "{{ promtail_download_path }}" + dest: "{{ promtail_unarchive_dest_path }}" + creates: "{{ promtail_extracted_path }}/promtail" + remote_src: true + + - name: install binaries + copy: + src: "{{ promtail_extracted_path }}" + dest: "{{ promtail_bin_path }}/promtail" + owner: root + group: root + mode: 0755 + remote_src: true + notify: restart promtail + when: promtail_version != promtail_local_version diff --git a/roles/promtail/tasks/main.yaml b/roles/promtail/tasks/main.yaml new file mode 100644 index 0000000..89aed0c --- /dev/null +++ b/roles/promtail/tasks/main.yaml @@ -0,0 +1,30 @@ +--- +- name: gather os specific variables + include_vars: "{{ lookup('first_found', possible_files) }}" + vars: + possible_files: + files: + - "{{ ansible_distribution }}-{{ ansible_distribution_version }}.yaml" + - "{{ ansible_distribution }}.yaml" + - "{{ ansible_os_family }}.yaml" + - "default.yaml" + paths: + - vars + +- name: include os specific tasks + include_tasks: "{{ lookup('first_found', possible_files) }}" + vars: + possible_files: + files: + - "{{ ansible_distribution }}-{{ ansible_distribution_version }}.yaml" + - "{{ ansible_distribution }}.yaml" + - "{{ ansible_os_family }}.yaml" + - "default.yaml" + paths: + - tasks + +- include: pre.yaml + +- include: install.yaml + +- include: configure.yaml diff --git a/roles/promtail/tasks/pre.yaml b/roles/promtail/tasks/pre.yaml new file mode 100644 index 0000000..01fa80a --- /dev/null +++ b/roles/promtail/tasks/pre.yaml @@ -0,0 +1,50 @@ +--- +- name: determine if installed + stat: + path: "{{ promtail_bin_path }}/promtail" + register: st + +- name: set promtail_installed + set_fact: + promtail_installed: "{{ st.stat.exists | bool }}" + +- block: + - name: determine latest version + uri: + url: https://api.github.com/repos/grafana/loki/releases/latest + return_content: true + body_format: json + register: _latest_version + until: _latest_version.status == 200 + retries: 3 + + - name: set promtail_version + set_fact: + promtail_version: "{{ _latest_version.json['tag_name'] | regex_replace('^v', '') }}" + +- block: + - name: determine installed version + command: "{{ promtail_bin_path }}/promtail --version" + register: _installed_version_string + changed_when: false + + - name: set promtail_local_version + set_fact: + promtail_local_version: "{{ _installed_version_string.stdout | regex_search(promtail_version_regex, '\\1') | first }}" + when: promtail_installed + +- name: set promtail_local_version to 0 + set_fact: + promtail_local_version: "0" + when: not promtail_installed + +- block: + - name: get checksums + set_fact: + _checksums: "{{ lookup('url', promtail_checksum_url, wantlist=True) }}" + + - name: set promtail_checksum + set_fact: + promtail_checksum: "sha256:{{ item.split(' ') | first }}" + loop: "{{ _checksums }}" + when: "promtail_release_file in item" diff --git a/roles/promtail/templates/promtail.service.j2 b/roles/promtail/templates/promtail.service.j2 new file mode 100644 index 0000000..ab6f959 --- /dev/null +++ b/roles/promtail/templates/promtail.service.j2 @@ -0,0 +1,19 @@ +{{ ansible_managed | comment }} + +[Unit] +Description=Loki +After=network-online.target + +[Service] +Type=simple +User={{ promtail_user }} +Group={{ promtail_group }} +ExecStart={{ promtail_bin_path }}/promtail \ + -config.file {{ promtail_config_path }} +WorkingDirectory={{ promtail_var_path }} + +Restart=always +RestartSec=1 + +[Install] +WantedBy=multi-user.target diff --git a/roles/promtail/templates/promtail.yaml.j2 b/roles/promtail/templates/promtail.yaml.j2 new file mode 100644 index 0000000..71a35ac --- /dev/null +++ b/roles/promtail/templates/promtail.yaml.j2 @@ -0,0 +1,21 @@ +{{ ansible_managed | comment }} +--- +{% if promtail_server is defined %} +server: + {{ promtail_server | to_nice_yaml(indent=2) | indent(2, False) }} +{% endif -%} + +{% if promtail_positions is defined %} +positions: + {{ promtail_positions | to_nice_yaml(indent=2) | indent(2, False) }} +{% endif -%} + +{% if promtail_clients is defined %} +clients: + {{ promtail_clients | to_nice_yaml(indent=2) | indent(2, False) }} +{% endif -%} + +{% if promtail_scrape_configs is defined %} +scrape_configs: + {{ promtail_scrape_configs | to_nice_yaml(indent=2) | indent(2, False) }} +{% endif -%} diff --git a/roles/promtail/vars/default.yaml b/roles/promtail/vars/default.yaml new file mode 100644 index 0000000..e69de29