add ansible

This commit is contained in:
Vassiliy Yegorov
2022-11-15 10:30:03 +07:00
parent 2d44f9f8f8
commit a08e7b0120
31 changed files with 1172 additions and 0 deletions

View File

@@ -0,0 +1,104 @@
---
- name: Ensure unzip package is available
package:
name: unzip
state: present
update_cache: "{{ promtail_apt_update_cache }}"
- name: Create promtail group
group:
name: "{{ promtail_system_group }}"
state: present
system: True
when: promtail_system_group != "root"
- name: Create the promtail user
user:
name: "{{ promtail_system_user }}"
group: "{{ promtail_system_group }}"
groups: "{{ promtail_user_additional_groups }}"
append: True
shell: /usr/sbin/nologin
system: True
createhome: False
home: /
when: promtail_system_user != "root"
- name: Ensure /usr/local/bin exists
file:
path: /usr/local/bin
state: directory
mode: 0755
- name: Create config directories
file:
path: "{{ item }}"
state: directory
owner: root
group: "{{ promtail_system_group }}"
mode: 0770
loop:
- "{{ promtail_config_dir }}"
- "{{ promtail_config_file_sd_dir }}"
- "{{ promtail_positions_directory }}"
- name: Create application dirs
file:
path: "{{ item }}"
state: directory
owner: "{{ promtail_system_user }}"
group: "{{ promtail_system_group }}"
mode: 0755
with_items:
- "{{ promtail_install_dir }}"
- "{{ promtail_install_dir }}/{{ promtail_version }}"
- name: Check promtail binary
stat:
path: "{{ promtail_install_dir }}/{{ promtail_version }}/promtail-linux-{{ go_arch }}"
register: promtail_binary
- name: Download promtail binaries
get_url:
url: "{{ promtail_dist_url }}"
dest: "{{ promtail_tmp_dir }}/{{ promtail_version }}_promtail-linux-{{ go_arch }}.zip"
force: True
checksum: "{{ promtail_custom_checksum if promtail_custom_checksum else 'sha256:' + __promtail_checksum }}"
when: not promtail_binary.stat.exists
- name: Unpack promtail binaries
ignore_errors: "{{ ansible_check_mode }}"
unarchive:
src: "{{ promtail_tmp_dir }}/{{ promtail_version }}_promtail-linux-{{ go_arch }}.zip"
dest: "{{ promtail_install_dir }}/{{ promtail_version }}"
creates: "{{ promtail_install_dir }}/{{ promtail_version }}/promtail-linux-{{ go_arch }}"
mode: 0755
remote_src: True
- name: Create symlink to latest version
notify:
- Restart promtail
ignore_errors: "{{ ansible_check_mode }}"
file:
state: link
src: "{{ promtail_install_dir }}/{{ promtail_version }}/promtail-linux-{{ go_arch }}"
dest: /usr/local/bin/promtail
mode: 0755
- name: Write config
notify:
- Restart promtail
template:
src: config.j2
dest: "{{ promtail_config_file }}"
owner: root
group: "{{ promtail_system_group }}"
mode: 0644
- name: Create systemd service unit
notify:
- Restart promtail
template:
src: "{{ promtail_systemd_service_template_file }}"
dest: "/etc/systemd/system/{{ promtail_systemd_service }}.service"
mode: 0644

View File

@@ -0,0 +1,22 @@
---
- import_tasks: preflight.yml
tags:
- promtail_install
- promtail
- import_tasks: install.yml
become: True
tags:
- promtail_install
- promtail
- name: Ensure promtail service is started and enabled
become: True
systemd:
daemon_reload: True
name: "{{ promtail_systemd_service }}"
state: started
enabled: True
tags:
- promtail_run
- promtail

View File

@@ -0,0 +1,43 @@
---
- 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 promtail version to {{ _latest_release.json.tag_name[1:] }}"
set_fact:
promtail_version: "{{ _latest_release.json.tag_name[1:] }}"
when:
- promtail_version == "latest"
delegate_to: localhost
run_once: True
- name: "Get checksum list"
set_fact:
__promtail_checksums: "{{ lookup('url', 'https://github.com/grafana/loki/releases/download/v' + promtail_version + '/SHA256SUMS', wantlist=True) | list }}"
run_once: True
delegate_to: localhost
- name: "Get checksum for {{ go_arch }} architecture"
set_fact:
__promtail_checksum: "{{ item.split(' ')[0] }}"
with_items: "{{ __promtail_checksums }}"
when:
- "('promtail-linux-' + go_arch + '.zip') in item"