diff --git a/roles/loki/defaults/main.yaml b/roles/loki/defaults/main.yaml new file mode 100644 index 0000000..8d5aa44 --- /dev/null +++ b/roles/loki/defaults/main.yaml @@ -0,0 +1,74 @@ +--- +loki_go_arch_map: + i386: '386' + x86_64: 'amd64' + +loki_go_arch: "{{ loki_go_arch_map[ansible_architecture] | default('amd64') }}" + +loki_service_name: loki.service +loki_service_enabled: true +loki_service_state: started + +loki_version_regex: ^loki, version ([\d.]+) + +loki_github_project_url: https://github.com/grafana/loki +loki_release_file: "loki-{{ ansible_system | lower }}-{{ loki_go_arch }}.zip" +loki_release_url: "{{ loki_github_project_url }}/releases/download/v{{ loki_version }}/{{ loki_release_file }}" +loki_checksum_url: "{{ loki_github_project_url }}/releases/download/v{{ loki_version }}/SHA256SUMS" +loki_download_path: "/tmp/{{ loki_release_file }}" +loki_unarchive_dest_path: /tmp +loki_extracted_path: "{{ loki_download_path | replace('.zip', '') }}" + +loki_user: loki +loki_user_state: present +loki_user_shell: /usr/sbin/nologin + +loki_group: loki +loki_group_state: "{{ loki_user_state | default('present') }}" + +loki_config_path: /etc/loki.yaml + +loki_var_path: /var/lib/loki +loki_var_owner: "{{ loki_user }}" +loki_var_group: "{{ loki_group }}" +loki_var_mode: "0755" + +loki_bin_path: /usr/local/bin + +loki_auth_enabled: false + +loki_server: + http_listen_port: 3100 + +loki_ingester: + lifecycler: + address: 127.0.0.1 + ring: + kvstore: + store: inmemory + replication_factor: 1 + final_sleep: 0s + chunk_idle_period: 5m + chunk_retain_period: 30s + +loki_schema_config: + configs: + - from: 2020-05-15 + store: boltdb + object_store: filesystem + schema: v11 + index: + prefix: index_ + period: 168h + +loki_storage_config: + boltdb: + directory: "{{ loki_var_path }}/index" + filesystem: + directory: "{{ loki_var_path }}/chunks" + +loki_limits_config: + enforce_metric_name: false + reject_old_samples: true + reject_old_samples_max_age: 168h + ingestion_burst_size_mb: 16 diff --git a/roles/loki/handlers/main.yaml b/roles/loki/handlers/main.yaml new file mode 100644 index 0000000..52aefa8 --- /dev/null +++ b/roles/loki/handlers/main.yaml @@ -0,0 +1,6 @@ +--- +- name: restart loki + systemd: + name: loki.service + daemon_reload: true + state: restarted diff --git a/roles/loki/tasks/configure.yaml b/roles/loki/tasks/configure.yaml new file mode 100644 index 0000000..7ae94b6 --- /dev/null +++ b/roles/loki/tasks/configure.yaml @@ -0,0 +1,48 @@ +--- +- name: create group + group: + name: "{{ loki_group }}" + system: true + state: "{{ loki_group_state | default('present') }}" + +- name: create user + user: + name: "{{ loki_user }}" + system: true + shell: "{{ loki_user_shell }}" + group: "{{ loki_group }}" + createhome: false + home: "{{ loki_var_path }}" + state: "{{ loki_user_state | default('present') }}" + +- name: configure + template: + src: loki.yaml.j2 + dest: "{{ loki_config_path }}" + owner: root + group: root + mode: 0444 + notify: restart loki + +- name: create var path + file: + path: "{{ loki_var_path }}" + state: directory + owner: "{{ loki_var_owner }}" + group: "{{ loki_var_group }}" + mode: "{{ loki_var_mode }}" + +- name: configure systemd template + template: + src: "{{ loki_service_name }}.j2" + dest: "/etc/systemd/system/{{ loki_service_name }}" + owner: root + group: root + mode: 0444 + notify: restart loki + +- name: manage service + service: + name: "{{ loki_service_name }}" + enabled: "{{ loki_service_enabled }}" + state: "{{ loki_service_state }}" diff --git a/roles/loki/tasks/default.yaml b/roles/loki/tasks/default.yaml new file mode 100644 index 0000000..e69de29 diff --git a/roles/loki/tasks/install.yaml b/roles/loki/tasks/install.yaml new file mode 100644 index 0000000..36491d2 --- /dev/null +++ b/roles/loki/tasks/install.yaml @@ -0,0 +1,29 @@ +--- +- block: + - name: download archive + get_url: + url: "{{ loki_release_url }}" + dest: "{{ loki_download_path }}" + checksum: "{{ loki_checksum }}" + register: dl + until: dl is success + retries: 5 + delay: 10 + + - name: extract archive + unarchive: + src: "{{ loki_download_path }}" + dest: "{{ loki_unarchive_dest_path }}" + creates: "{{ loki_extracted_path }}/loki" + remote_src: true + + - name: install binaries + copy: + src: "{{ loki_extracted_path }}" + dest: "{{ loki_bin_path }}/loki" + owner: root + group: root + mode: 0755 + remote_src: true + notify: restart loki + when: loki_version != loki_local_version diff --git a/roles/loki/tasks/main.yaml b/roles/loki/tasks/main.yaml new file mode 100644 index 0000000..89aed0c --- /dev/null +++ b/roles/loki/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/loki/tasks/pre.yaml b/roles/loki/tasks/pre.yaml new file mode 100644 index 0000000..0d1525d --- /dev/null +++ b/roles/loki/tasks/pre.yaml @@ -0,0 +1,50 @@ +--- +- name: determine if installed + stat: + path: "{{ loki_bin_path }}/loki" + register: st + +- name: set loki_installed + set_fact: + loki_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 loki_version + set_fact: + loki_version: "{{ _latest_version.json['tag_name'] | regex_replace('^v', '') }}" + +- block: + - name: determine installed version + command: "{{ loki_bin_path }}/loki --version" + register: _installed_version_string + changed_when: false + + - name: set loki_local_version + set_fact: + loki_local_version: "{{ _installed_version_string.stdout | regex_search(loki_version_regex, '\\1') | first }}" + when: loki_installed + +- name: set loki_local_version to 0 + set_fact: + loki_local_version: "0" + when: not loki_installed + +- block: + - name: get checksums + set_fact: + _checksums: "{{ lookup('url', loki_checksum_url, wantlist=True) }}" + + - name: set loki_checksum + set_fact: + loki_checksum: "sha256:{{ item.split(' ') | first }}" + loop: "{{ _checksums }}" + when: "loki_release_file in item" diff --git a/roles/loki/templates/loki.service.j2 b/roles/loki/templates/loki.service.j2 new file mode 100644 index 0000000..ca4057c --- /dev/null +++ b/roles/loki/templates/loki.service.j2 @@ -0,0 +1,19 @@ +{{ ansible_managed | comment }} + +[Unit] +Description=Loki +After=network-online.target + +[Service] +Type=simple +User={{ loki_user }} +Group={{ loki_group }} +ExecStart={{ loki_bin_path }}/loki \ + -config.file {{ loki_config_path }} +WorkingDirectory={{ loki_var_path }} + +Restart=always +RestartSec=1 + +[Install] +WantedBy=multi-user.target diff --git a/roles/loki/templates/loki.yaml.j2 b/roles/loki/templates/loki.yaml.j2 new file mode 100644 index 0000000..762c220 --- /dev/null +++ b/roles/loki/templates/loki.yaml.j2 @@ -0,0 +1,30 @@ +{{ ansible_managed | comment }} +--- +{% if loki_auth_enabled is defined %} +auth_enabled: {{ loki_auth_enabled | bool | lower }} +{% endif %} + +{% if loki_server is defined %} +server: + {{ loki_server | to_nice_yaml(indent=2) | indent(2, False) }} +{% endif -%} + +{% if loki_ingester is defined %} +ingester: + {{ loki_ingester | to_nice_yaml(indent=2) | indent(2, False) }} +{% endif -%} + +{% if loki_schema_config is defined %} +schema_config: + {{ loki_schema_config | to_nice_yaml(indent=2) | indent(2, False) }} +{% endif -%} + +{% if loki_storage_config is defined %} +storage_config: + {{ loki_storage_config | to_nice_yaml(indent=2) | indent(2, False) }} +{% endif -%} + +{% if loki_limits_config is defined %} +limits_config: + {{ loki_limits_config | to_nice_yaml(indent=2) | indent(2, False) }} +{% endif -%} diff --git a/roles/loki/vars/default.yaml b/roles/loki/vars/default.yaml new file mode 100644 index 0000000..e69de29