From fba33111d71a9e89a01d78e7194a5072209dec43 Mon Sep 17 00:00:00 2001 From: Diogenes Pelisson Date: Sun, 19 Apr 2020 17:28:06 -0300 Subject: [PATCH] initial commit --- LICENSE | 13 +++++++ README.md | 33 ++++++++++++++++ defaults/main.yml | 36 ++++++++++++++++++ handlers/main.yml | 13 +++++++ meta/main.yml | 25 ++++++++++++ tasks/install.yml | 80 +++++++++++++++++++++++++++++++++++++++ tasks/main.yml | 5 +++ tasks/preflight.yml | 44 +++++++++++++++++++++ templates/loki.service.j2 | 0 templates/loki.yml.j2 | 47 +++++++++++++++++++++++ vars/main.yml | 9 +++++ 11 files changed, 305 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 defaults/main.yml create mode 100644 handlers/main.yml create mode 100644 meta/main.yml create mode 100644 tasks/install.yml create mode 100644 tasks/main.yml create mode 100644 tasks/preflight.yml create mode 100644 templates/loki.service.j2 create mode 100644 templates/loki.yml.j2 create mode 100644 vars/main.yml diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a319f68 --- /dev/null +++ b/LICENSE @@ -0,0 +1,13 @@ + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + Version 2, December 2004 + + Copyright (C) 2004 Diogenes Pelisson + + Everyone is permitted to copy and distribute verbatim or modified + copies of this license document, and changing it is allowed as long + as the name is changed. + + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. You just DO WHAT THE FUCK YOU WANT TO. diff --git a/README.md b/README.md new file mode 100644 index 0000000..d3d9b77 --- /dev/null +++ b/README.md @@ -0,0 +1,33 @@ +Role Name +========= + +Deploy and configure [Loki/Promtail](https://github.com/grafana/loki) using Ansible. + +Requirements +------------ + +- Ansible >= 2.9 + +Role Variables +-------------- + +A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. + +Example Playbook +---------------- + +Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: + + - hosts: servers + roles: + - { role: username.rolename, x: 42 } + +License +------- + +WTFPL see [LICENSE](license) + +Author Information +------------------ + +An optional section for the role authors to include contact information, or a website (HTML is not allowed). diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..0acfadd --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,36 @@ +--- +# loki version to install +loki_version: 'latest' + +# loki bins to install +loki_bins: + - loki + - logcli + - promtail + +loki_user: loki +loki_group: loki +loki_config_dir: /etc/loki + +loki_binary_local_dir: '' + +loki_target: all +loki_auth_enabled: true + +loki_server_config: + http_listen_address: 0.0.0.0 + http_listen_port: 80 + grpc_listen_address: 0.0.0.0 + grpc_listen_port: 9095 + +loki_distributor_config: [] +loki_querier_config: [] +loki_ingester_client_config: [] +loki_ingester_config: [] +loki_storage_config: [] +loki_chunk_store_config: [] +loki_schema_config: [] +loki_limits_config: [] +loki_frontend_worker_config: [] +loki_table_manager_config: [] +loki_runtime_config: [] diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..cc960c3 --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,13 @@ +--- +- name: restart loki + become: true + systemd: + daemon_reload: true + name: loki + state: restarted + +- name: reload loki + become: true + systemd: + name: loki + state: reloaded diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..b02709d --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,25 @@ +galaxy_info: + author: Diogenes Pelisson + description: Deploy and configure Loki and Promtail. + company: none + issue_tracker_url: https://github.com/diogenxs/ansible-role-loki/issues + license: WTFPL + min_ansible_version: 2.9 + platforms: + - name: Ubuntu + versions: + - bionic + - xenial + - name: Debian + versions: + - stretch + - buster + galaxy_tags: + - loki + - promtail + - logging + - collector + - monitoring + - grafana + - prometheus +dependencies: [] \ No newline at end of file diff --git a/tasks/install.yml b/tasks/install.yml new file mode 100644 index 0000000..760104a --- /dev/null +++ b/tasks/install.yml @@ -0,0 +1,80 @@ +--- +- name: create {{ loki_group }} system group + group: + name: "{{ loki_group }}" + system: true + state: present + +- name: create {{ loki_user }} system user + user: + name: "{{ loki_user }}" + system: true + shell: "/usr/sbin/nologin" + group: "{{ loki_group }}" + createhome: false + # home: "{{ loki_db_dir }}" + +# - name: create loki data directory +# file: +# path: "{{ loki_db_dir }}" +# state: directory +# owner: loki +# group: loki +# mode: 0755 + +- name: create loki configuration directories + file: + path: "{{ item }}" + state: directory + owner: root + group: "{{ loki_group }}" + mode: 0770 + with_items: + - "{{ loki_config_dir }}" + +- block: + - name: download binaries to local folder + become: false + get_url: + url: "https://github.com/grafana/loki/releases/download/v{{ loki_version }}/{{ item }}-linux-{{ go_arch }}.zip" + dest: "/tmp/{{ item }}-{{ loki_version }}-linux-{{ go_arch }}.zip" + checksum: "sha256:{{ __loki_bins_checksum[item] }}" + register: _download_archive + until: _download_archive is succeeded + retries: 5 + delay: 2 + loop: "{{ loki_bins }}" + + - name: unpack binaries + become: false + unarchive: + src: "/tmp/{{ item }}-{{ loki_version }}-linux-{{ go_arch }}.zip" + dest: "/tmp" + creates: "/tmp/{{ item }}-{{ loki_version }}-linux-{{ go_arch }}" + loop: "{{ loki_bins }}" + + - name: propagate official binaries + copy: + src: "/tmp/{{ item }}-linux-{{ go_arch }}" + dest: "/usr/local/bin/{{ item }}" + mode: 0755 + owner: root + group: root + loop: "{{ loki_bins }}" + notify: + - restart loki + +- name: create systemd service unit + template: + src: loki.service.j2 + dest: /etc/systemd/system/loki.service + owner: root + group: root + mode: 0644 + notify: + - restart loki + loop: "{{ loki_bins }}" + when: + - item == 'loki' or + - item == 'loki-canary' or + - item == 'promtail' diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..c1b2ee7 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,5 @@ +--- +- include: preflight.yml + +- include: install.yml + become: true diff --git a/tasks/preflight.yml b/tasks/preflight.yml new file mode 100644 index 0000000..7ee819c --- /dev/null +++ b/tasks/preflight.yml @@ -0,0 +1,44 @@ +--- +# - name: Assert usage of systemd as an init system +# assert: +# that: ansible_service_mgr == 'systemd' +# msg: "This module only works with systemd" + +- block: + - name: Get latest release + uri: + url: "https://api.github.com/repos/grafana/loki/releases/latest" + method: GET + return_content: true + status_code: 200 + body_format: json + validate_certs: false + user: "{{ lookup('env', 'GH_USER') | default(omit) }}" + password: "{{ lookup('env', 'GH_TOKEN') | default(omit) }}" + no_log: "{{ not lookup('env', 'ANSIBLE_DEBUG') | bool }}" + register: _latest_release + until: _latest_release.status == 200 + retries: 5 + + - name: "Set loki version to {{ _latest_release.json.tag_name[1:] }}" + set_fact: + loki_version: "{{ _latest_release.json.tag_name[1:] }}" + when: + - loki_version == "latest" + - loki_binary_local_dir | length == 0 + +- block: + - name: "Get checksum list" + set_fact: + __loki_checksums: "{{ lookup('url', 'https://github.com/grafana/loki/releases/download/v' + loki_version + '/SHA256SUMS', wantlist=True) | list }}" + run_once: true + + - name: "Get checksum for {{ item[1] }} in {{ go_arch }} architecture" + set_fact: + __loki_bins_checksum: "{{ __loki_bins_checksum | default({}) | combine({item[1]: item[0].split()[0]}) }}" + loop: "{{ lookup('nested', __loki_checksums, loki_bins, wantlist=True) }}" + when: + - "(item[1] + '-linux-' + go_arch + '.zip') in item[0].split()[1]" + delegate_to: localhost + when: + - loki_binary_local_dir | length == 0 diff --git a/templates/loki.service.j2 b/templates/loki.service.j2 new file mode 100644 index 0000000..e69de29 diff --git a/templates/loki.yml.j2 b/templates/loki.yml.j2 new file mode 100644 index 0000000..b7e4b10 --- /dev/null +++ b/templates/loki.yml.j2 @@ -0,0 +1,47 @@ +#jinja2: trim_blocks: True, lstrip_blocks: True +{{ ansible_managed | comment }} +# https://github.com/grafana/loki/blob/master/docs/configuration/README.md#configuration-file-reference + +{% if loki_target != "" %} +target: {{ loki_target }} +{% endif %}} + +{% if loki_auth_enabled %} +auth_enabled: {{ loki_auth_enabled }} +{% endif %}} +{% if loki_server_config != [] %} +server: {{ loki_server_config }} +{% endif %}} +{% if loki_distributor_config != [] %} +distributor: {{ loki_distributor_config }} +{% endif %}} +{% if loki_querier_config != [] %} +querier: {{ loki_querier_config }} +{% endif %}} +{% if loki_ingester_client_config != [] %} +ingester_client: {{ loki_ingester_client_config }} +{% endif %}} +{% if loki_ingester_config != [] %} +ingester: {{ loki_ingester_config }} +{% endif %}} +{% if loki_storage_config != [] %} +storage_config: {{ loki_storage_config }} +{% endif %}} +{% if loki_chunk_store_config != [] %} +chunk_store_config: {{ loki_chunk_store_config }} +{% endif %}} +{% if loki_schema_config != [] %} +schema_config: {{ loki_schema_config }} +{% endif %}} +{% if loki_limits_config != [] %} +limits_config: {{ loki_limits_config }} +{% endif %}} +{% if loki_frontend_worker_config != [] %} +frontend_worker_config: {{ loki_frontend_worker_config }} +{% endif %}} +{% if loki_table_manager_config != [] %} +table_manager_config: {{ loki_table_manager_config }} +{% endif %}} +{% if loki_runtime_config != [] %} +runtime_config: {{ loki_runtime_config }} +{% endif %}} diff --git a/vars/main.yml b/vars/main.yml new file mode 100644 index 0000000..fdcd126 --- /dev/null +++ b/vars/main.yml @@ -0,0 +1,9 @@ +--- +go_arch_map: + i386: '386' + x86_64: 'amd64' + aarch64: 'arm64' + armv7l: 'armv7' + armv6l: 'armv6' + +go_arch: "{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}"