commit 3dcd1b88fc4de2eb7453ad16a1e9b20043c2c6af Author: SebClem Date: Sat Jan 15 18:25:03 2022 +0100 Init diff --git a/README.md b/README.md new file mode 100644 index 0000000..035f4c3 --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +![publish](https://github.com/dzervas/ansible-vector/workflows/publish/badge.svg) + +# Vector ansible role + +This is an ansible role to set up [vector](https://vector.dev). +It translates the YAML configuration to TOML, so any configuration is possible. + +For available variables check out [defaults](roles/vector/defaults/main.yml) + +Currently only amd64, arch64, arch7 through deb and rpm packages are supported diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..534df01 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,23 @@ +vector_template: vector.toml.j2 +vector_config_file: /etc/vector/vector.toml +vector_nightly: no +vector_version: "{{ vector_nightly | ternary('nightly','latest') }}" +add_vector_docker_group: no # Add vector user to "docker" group +add_vector_journal_group: no # Add vector user to "systemd-journal" group + +sources: + journald: + type: journald + current_boot_only: true + +transforms: + grok: + type: grok_parser + inputs: + - journald + pattern: '(?\\d+)%{GREEDYDATA}' +sinks: + vector: + type: vector + inputs: ["journald"] + address: "vector.example.com:9000" diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..9081f3d --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,5 @@ +- name: restart vector + service: + state: restarted + daemon_reload: yes + name: vector diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..cef098b --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,20 @@ +--- + +galaxy_info: + role_name: vector + namespace: dzervas + author: Dimitris Zervas + description: vector.dev ansible role + license: MIT + platforms: + - name: Debian + versions: + - all + - name: Ubuntu + versions: + - all + galaxy_tags: + - vector + - logging + - monitoring + min_ansible_version: 2.9 diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml new file mode 100644 index 0000000..6d39e2e --- /dev/null +++ b/molecule/default/converge.yml @@ -0,0 +1,7 @@ +--- +- name: Converge + hosts: all + tasks: + - name: "Include vector" + include_role: + name: "vector" diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml new file mode 100644 index 0000000..38186a5 --- /dev/null +++ b/molecule/default/molecule.yml @@ -0,0 +1,61 @@ +--- +dependency: + name: galaxy +driver: + name: docker +lint: | + set -e + yamllint . + ansible-lint + flake8 +platforms: + - name: debian10 + image: jrei/systemd-debian:10 + privileged: true + command: /lib/systemd/systemd + tmpfs: + - /run + - /tmp + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:ro + - name: debian9 + image: jrei/systemd-debian:9 + privileged: true + command: /lib/systemd/systemd + tmpfs: + - /run + - /tmp + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:ro + - name: ubuntu1804 + image: jrei/systemd-ubuntu:18.04 + privileged: true + command: /lib/systemd/systemd + tmpfs: + - /run + - /tmp + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:ro + - name: ubuntu2004 + image: jrei/systemd-ubuntu:20.04 + privileged: true + command: /lib/systemd/systemd + tmpfs: + - /run + - /tmp + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:ro + - name: centos8 + image: jrei/systemd-centos:8 + privileged: true + command: /usr/sbin/init + tmpfs: + - /run + - /tmp + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:ro + +provisioner: + name: ansible +verifier: + name: ansible diff --git a/molecule/default/verify.yml b/molecule/default/verify.yml new file mode 100644 index 0000000..e5586a0 --- /dev/null +++ b/molecule/default/verify.yml @@ -0,0 +1,20 @@ +--- +- name: Verify + hosts: all + gather_facts: false + tasks: + - name: Execute vector + command: vector --version + changed_when: false + register: vector_version_rc + - name: Check grok pattern is not mangled + lineinfile: + path: /etc/vector/vector.toml + line: ' pattern = "(?\\d+)%{GREEDYDATA}"' + diff: true + register: grok_pattern_rc + - name: Assert vector is installed + assert: + that: + - vector_version_rc is success + - grok_pattern_rc is not changed diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..81dbd93 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,65 @@ +# Workaround for latest version being named against a version number +- name: Get latest version + uri: + url: https://s3.amazonaws.com/packages.timber.io/?prefix=vector/latest&max-keys=1 + return_content: true + register: bucket_content + when: vector_version == "latest" + +- name: Set latest version + set_fact: + vector_version: "{{ bucket_content.content | regex_replace('.*\\n.*[^-]+-(?P[\\d\\.]+)-.*','\\g') }}" + when: vector_version == "latest" + +- name: Install Vector (Debian) + apt: + deb: "https://packages.timber.io/vector/{{ version }}/vector-{{ version }}-{{ arch }}.deb" + install_recommends: yes + notify: + - restart vector + vars: + version: "{{ (vector_version == 'nightly')| bool | ternary('nightly/latest', vector_version) }}" + arch: "{{ vector_debian_arch[ansible_machine] }}" + when: ansible_os_family == 'Debian' + +- name: Install Vector (RedHat) + yum: + name: "https://packages.timber.io/vector/{{ version }}/vector-{{ package_version }}.{{ arch }}.rpm" + state: present + disable_gpg_check: yes # package is not signed + notify: + - restart vector + vars: + version: "{{ (vector_version == 'nightly')| bool | ternary('nightly/latest', vector_version) }}" + package_version: "{{ vector_version is match('latest') | ternary(vector_version, vector_version ~ '-1') }}" + arch: "{{ vector_redhat_arch[ansible_machine] }}" + when: ansible_os_family == 'RedHat' + +- name: Copy config + template: + src: "{{ vector_template }}" + dest: "{{ vector_config_file }}" + mode: 0644 + notify: restart vector + +- name: Add vector user to docker group + user: + name: vector + groups: docker + append: yes + when: add_vector_docker_group | default(no) + notify: restart vector + +- name: Add vector user to systemd-journal group + user: + name: vector + groups: systemd-journal + append: yes + when: add_vector_journal_group | default(no) + notify: restart vector + +- name: Start Vector + service: + state: started + enabled: yes + name: vector diff --git a/templates/vector.toml.j2 b/templates/vector.toml.j2 new file mode 100644 index 0000000..a59c084 --- /dev/null +++ b/templates/vector.toml.j2 @@ -0,0 +1,24 @@ +# Set global options +data_dir = "/var/lib/vector" + +{% set loop_helper = { + "sources": (sources | default({})), + "transforms": (transforms | default({})), + "sinks": (sinks | default({})) +} %} + +{% for name, cat in loop_helper.items() | sort(attribute='0') %} +{% for key, value in cat.items() | sort(attribute='0') %} +[{{ name }}.{{ key }}] + {% if value %} + {%- for skey, svalue in value.items() | sort(attribute='0') %} +{%- if svalue is string %} + {{ skey }} = "{{ svalue }}" +{% else %} + {{ skey }} = {{ svalue | tojson }} +{% endif %} + {% endfor %} + {%- endif %} + +{% endfor %} +{% endfor %} diff --git a/vars/main.yml b/vars/main.yml new file mode 100644 index 0000000..af90c4b --- /dev/null +++ b/vars/main.yml @@ -0,0 +1,8 @@ +vector_debian_arch: + armv7l: armhf + aarch64: arm64 + x86_64: amd64 +vector_redhat_arch: + armv7l: armv7hl + aarch64: aarch64 + x86_64: x86_64