diff --git a/.yamllint b/.yamllint new file mode 100644 index 0000000..8827676 --- /dev/null +++ b/.yamllint @@ -0,0 +1,33 @@ +--- +# Based on ansible-lint config +extends: default + +rules: + braces: + max-spaces-inside: 1 + level: error + brackets: + max-spaces-inside: 1 + level: error + colons: + max-spaces-after: -1 + level: error + commas: + max-spaces-after: -1 + level: error + comments: disable + comments-indentation: disable + document-start: disable + empty-lines: + max: 3 + level: error + hyphens: + level: error + indentation: disable + key-duplicates: enable + line-length: disable + new-line-at-end-of-file: disable + new-lines: + type: unix + trailing-spaces: disable + truthy: disable diff --git a/README.md b/README.md index e8eb5c6..80ba032 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # ansible-vector -[Скачивает](https://yum.vector.dev/stable/vector-0/x86_64/), устанавливает *rpm* пакет и создает демонстрационный конфигурационный файл для интеграции c *ClickHouse*. +Производит установку и настройку утилиты [Vector](https://vector.dev/)

@@ -9,17 +9,38 @@ ## Переменные -| Название | Значение по умолчанию | Описание | -| :--: | :--: | :--: | -| *vector_version* | 0.42.0 | Версия пакета | -| *vector_ch_address* | 127.0.0.1 | Адрес подключения к *ClickHouse* | -| *vector_ch_port* | 8123 | Порт подключения к *ClickHouse* | -| *vector_ch_db_name* | db_example | Имя БД в *ClickHouse* | -| *vector_ch_table_name* | table_example | Имя таблицы для записи логов в *ClickHouse* | + - Вы можете указать версию пакета *Vector* +```yaml +vector_version: "0.42.0" +``` + +
+ + - Вы можете создать любое количество конфигурационных файлов *Vector* +```yaml +vector_configs_add: + default: "{{ lookup('template', ./templates/default.yaml.j2') }}" + clickhouse: "{{ lookup('template', ./templates/clickhouse.yaml.j2') }}" + zabbix: "{{ lookup('template', ./templates/zabbix.yaml.j2') }}" +``` +Ключ это название будущего конфигурационного файла, а значение - функция которая формирует содержимое конфигурационного файла из *Jinja* шаблона по указанному пути. + +
+ + - Вы можете удалить уже созданные ранее конфигурационные файлы *Vector* +```yaml +vector_configs_del: + - default + - clickhouse + - zabbix + ... +```

## Теги -Поддерживает тег *vector* для возможности запустить только эту роль в playbook. +| Название | Описание | +| :--: | :--- | +| *vector* | Позволяет запустить целиком роль в playbook | diff --git a/defaults/main.yml b/defaults/main.yml index 7f852a1..2dcbd29 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -2,7 +2,7 @@ # defaults file for vector vector_version: "0.42.0" -vector_ch_address: "127.0.0.1" -vector_ch_port: "8123" -vector_ch_db_name: "db_example" -vector_ch_table_name: "table_example" +vector_configs_add: + default: "{{ lookup('template', './templates/default.yaml.j2') }}" + +vector_configs_del: [] diff --git a/handlers/main.yml b/handlers/main.yml index 05839ee..ae1dde8 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -1,14 +1,29 @@ --- # handlers file for vector -- name: Systemd daemon reload - become: true - ansible.builtin.systemd: - daemon_reload: true - listen: daemon_reload - - name: Start vector service - become: true + become: "{{ true if 'sudo' in ansible_facts.packages else false }}" ansible.builtin.systemd: name: vector state: restarted listen: vector_restart + +- name: Stop vector service + become: "{{ true if 'sudo' in ansible_facts.packages else false }}" + ansible.builtin.systemd: + name: vector + state: stopped + listen: vector_stop + +- name: Enable vector service + become: "{{ true if 'sudo' in ansible_facts.packages else false }}" + ansible.builtin.systemd: + name: vector + enabled: true + listen: vector_enable + +- name: Disable vector service + become: "{{ true if 'sudo' in ansible_facts.packages else false }}" + ansible.builtin.systemd: + name: vector + enabled: false + listen: vector_disable diff --git a/meta/main.yml b/meta/main.yml index c572acc..c404d15 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -1,7 +1,7 @@ galaxy_info: - author: your name - description: your role description - company: your company (optional) + role_name: vector + namespace: cachmc + author: Vladislav Shishkov # If the issue tracker for your role is not on github, uncomment the # next line and provide a value @@ -14,9 +14,9 @@ galaxy_info: # - GPL-3.0-only # - Apache-2.0 # - CC-BY-4.0 - license: license (GPL-2.0-or-later, MIT, etc) + license: MIT - min_ansible_version: 2.1 + min_ansible_version: 2.13 # If this a Container Enabled role, provide the minimum Ansible Container version. # min_ansible_container_version: diff --git a/molecule/centos_8/Dockerfile.j2 b/molecule/centos_8/Dockerfile.j2 new file mode 100644 index 0000000..e420efb --- /dev/null +++ b/molecule/centos_8/Dockerfile.j2 @@ -0,0 +1,13 @@ +{% if item.registry is defined %} +FROM {{ item.registry.url }}/{{ item.image }} +{% else %} +FROM {{ item.image }} +{% endif %} + +RUN cd /etc/yum.repos.d/ +RUN sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-* +RUN sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-* + +RUN yum update -y + +RUN yum install -y nano diff --git a/molecule/centos_8/converge.yml b/molecule/centos_8/converge.yml new file mode 100644 index 0000000..6d39e2e --- /dev/null +++ b/molecule/centos_8/converge.yml @@ -0,0 +1,7 @@ +--- +- name: Converge + hosts: all + tasks: + - name: "Include vector" + include_role: + name: "vector" diff --git a/molecule/centos_8/molecule.yml b/molecule/centos_8/molecule.yml new file mode 100644 index 0000000..6881331 --- /dev/null +++ b/molecule/centos_8/molecule.yml @@ -0,0 +1,18 @@ +--- +dependency: + name: galaxy +driver: + name: docker +platforms: + - name: centos_8 + image: docker.io/library/centos:8 + pre_build_image: false + dockerfile: Dockerfile.j2 + command: /usr/sbin/init + privileged: true + capabilities: + - SYS_ADMIN +provisioner: + name: ansible +verifier: + name: ansible diff --git a/molecule/centos_8/verify.yml b/molecule/centos_8/verify.yml new file mode 100644 index 0000000..7b2531c --- /dev/null +++ b/molecule/centos_8/verify.yml @@ -0,0 +1,48 @@ +--- +# This is an example playbook to execute Ansible tests. + +- name: Verify + hosts: all + gather_facts: false + + tasks: + - name: Find config files + ansible.builtin.find: + paths: /etc/vector + register: find_configs + + - name: Validate configs vector + ansible.builtin.command: vector validate + environment: + VECTOR_CONFIG_DIR: /etc/vector + register: validate_configs + failed_when: + - validate_configs is failed + - "'Failed to load' not in validate_configs.stdout" + when: find_configs.files | length > 0 + + - name: Check validate configs vector + ansible.builtin.debug: + var: validate_configs.stdout_lines + failed_when: '"Validated" not in validate_configs.stdout' + when: find_configs.files | length > 0 + + - name: Get state vector.service + ansible.builtin.service_facts: + + - name: Check vector.service + ansible.builtin.debug: + msg: "vector.service has state '{{ ansible_facts.services[\"vector.service\"][\"state\"] }}'" + failed_when: '"failed" in ansible_facts.services["vector.service"]["state"]' + + - name: Check enabled vector.service + ansible.builtin.debug: + msg: "vector.service has status '{{ ansible_facts.services[\"vector.service\"][\"status\"] }}'" + failed_when: '"enabled" not in ansible_facts.services["vector.service"]["status"]' + when: '"running" in ansible_facts.services["vector.service"]["state"]' + + - name: Check disabled vector.service + ansible.builtin.debug: + msg: "vector.service has status '{{ ansible_facts.services[\"vector.service\"][\"status\"] }}'" + failed_when: '"disabled" not in ansible_facts.services["vector.service"]["status"]' + when: '"running" not in ansible_facts.services["vector.service"]["state"]' diff --git a/molecule/oraclelinux_8/Dockerfile.j2 b/molecule/oraclelinux_8/Dockerfile.j2 new file mode 100644 index 0000000..ae5edc9 --- /dev/null +++ b/molecule/oraclelinux_8/Dockerfile.j2 @@ -0,0 +1,9 @@ +{% if item.registry is defined %} +FROM {{ item.registry.url }}/{{ item.image }} +{% else %} +FROM {{ item.image }} +{% endif %} + +RUN dnf update + +RUN dnf install nano diff --git a/molecule/oraclelinux_8/converge.yml b/molecule/oraclelinux_8/converge.yml new file mode 100644 index 0000000..6d39e2e --- /dev/null +++ b/molecule/oraclelinux_8/converge.yml @@ -0,0 +1,7 @@ +--- +- name: Converge + hosts: all + tasks: + - name: "Include vector" + include_role: + name: "vector" diff --git a/molecule/oraclelinux_8/molecule.yml b/molecule/oraclelinux_8/molecule.yml new file mode 100644 index 0000000..52f78a9 --- /dev/null +++ b/molecule/oraclelinux_8/molecule.yml @@ -0,0 +1,18 @@ +--- +dependency: + name: galaxy +driver: + name: docker +platforms: + - name: oraclelinux_8 + image: docker.io/library/oraclelinux:8 + pre_build_image: false + dockerfile: Dockerfile.j2 + command: /usr/sbin/init + privileged: true + capabilities: + - SYS_ADMIN +provisioner: + name: ansible +verifier: + name: ansible diff --git a/molecule/oraclelinux_8/verify.yml b/molecule/oraclelinux_8/verify.yml new file mode 100644 index 0000000..7b2531c --- /dev/null +++ b/molecule/oraclelinux_8/verify.yml @@ -0,0 +1,48 @@ +--- +# This is an example playbook to execute Ansible tests. + +- name: Verify + hosts: all + gather_facts: false + + tasks: + - name: Find config files + ansible.builtin.find: + paths: /etc/vector + register: find_configs + + - name: Validate configs vector + ansible.builtin.command: vector validate + environment: + VECTOR_CONFIG_DIR: /etc/vector + register: validate_configs + failed_when: + - validate_configs is failed + - "'Failed to load' not in validate_configs.stdout" + when: find_configs.files | length > 0 + + - name: Check validate configs vector + ansible.builtin.debug: + var: validate_configs.stdout_lines + failed_when: '"Validated" not in validate_configs.stdout' + when: find_configs.files | length > 0 + + - name: Get state vector.service + ansible.builtin.service_facts: + + - name: Check vector.service + ansible.builtin.debug: + msg: "vector.service has state '{{ ansible_facts.services[\"vector.service\"][\"state\"] }}'" + failed_when: '"failed" in ansible_facts.services["vector.service"]["state"]' + + - name: Check enabled vector.service + ansible.builtin.debug: + msg: "vector.service has status '{{ ansible_facts.services[\"vector.service\"][\"status\"] }}'" + failed_when: '"enabled" not in ansible_facts.services["vector.service"]["status"]' + when: '"running" in ansible_facts.services["vector.service"]["state"]' + + - name: Check disabled vector.service + ansible.builtin.debug: + msg: "vector.service has status '{{ ansible_facts.services[\"vector.service\"][\"status\"] }}'" + failed_when: '"disabled" not in ansible_facts.services["vector.service"]["status"]' + when: '"running" not in ansible_facts.services["vector.service"]["state"]' diff --git a/molecule/ubuntu_xenial/Dockerfile.j2 b/molecule/ubuntu_xenial/Dockerfile.j2 new file mode 100644 index 0000000..0f07233 --- /dev/null +++ b/molecule/ubuntu_xenial/Dockerfile.j2 @@ -0,0 +1,9 @@ +{% if item.registry is defined %} +FROM {{ item.registry.url }}/{{ item.image }} +{% else %} +FROM {{ item.image }} +{% endif %} + +RUN apt-get update + +RUN apt-get install -y python3 python3-apt nano diff --git a/molecule/ubuntu_xenial/converge.yml b/molecule/ubuntu_xenial/converge.yml new file mode 100644 index 0000000..6d39e2e --- /dev/null +++ b/molecule/ubuntu_xenial/converge.yml @@ -0,0 +1,7 @@ +--- +- name: Converge + hosts: all + tasks: + - name: "Include vector" + include_role: + name: "vector" diff --git a/molecule/ubuntu_xenial/molecule.yml b/molecule/ubuntu_xenial/molecule.yml new file mode 100644 index 0000000..c771681 --- /dev/null +++ b/molecule/ubuntu_xenial/molecule.yml @@ -0,0 +1,18 @@ +--- +dependency: + name: galaxy +driver: + name: docker +platforms: + - name: ubuntu_xenial + image: docker.io/library/ubuntu:xenial + pre_build_image: false + dockerfile: Dockerfile.j2 + command: /sbin/init + privileged: true + capabilities: + - SYS_ADMIN +provisioner: + name: ansible +verifier: + name: ansible diff --git a/molecule/ubuntu_xenial/verify.yml b/molecule/ubuntu_xenial/verify.yml new file mode 100644 index 0000000..7b2531c --- /dev/null +++ b/molecule/ubuntu_xenial/verify.yml @@ -0,0 +1,48 @@ +--- +# This is an example playbook to execute Ansible tests. + +- name: Verify + hosts: all + gather_facts: false + + tasks: + - name: Find config files + ansible.builtin.find: + paths: /etc/vector + register: find_configs + + - name: Validate configs vector + ansible.builtin.command: vector validate + environment: + VECTOR_CONFIG_DIR: /etc/vector + register: validate_configs + failed_when: + - validate_configs is failed + - "'Failed to load' not in validate_configs.stdout" + when: find_configs.files | length > 0 + + - name: Check validate configs vector + ansible.builtin.debug: + var: validate_configs.stdout_lines + failed_when: '"Validated" not in validate_configs.stdout' + when: find_configs.files | length > 0 + + - name: Get state vector.service + ansible.builtin.service_facts: + + - name: Check vector.service + ansible.builtin.debug: + msg: "vector.service has state '{{ ansible_facts.services[\"vector.service\"][\"state\"] }}'" + failed_when: '"failed" in ansible_facts.services["vector.service"]["state"]' + + - name: Check enabled vector.service + ansible.builtin.debug: + msg: "vector.service has status '{{ ansible_facts.services[\"vector.service\"][\"status\"] }}'" + failed_when: '"enabled" not in ansible_facts.services["vector.service"]["status"]' + when: '"running" in ansible_facts.services["vector.service"]["state"]' + + - name: Check disabled vector.service + ansible.builtin.debug: + msg: "vector.service has status '{{ ansible_facts.services[\"vector.service\"][\"status\"] }}'" + failed_when: '"disabled" not in ansible_facts.services["vector.service"]["status"]' + when: '"running" not in ansible_facts.services["vector.service"]["state"]' diff --git a/tasks/configure.yml b/tasks/configure.yml index 133bcc2..1f03b0f 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -1,8 +1,64 @@ -- name: Copy config file vector.yaml - become: true - ansible.builtin.template: - src: "vector.yaml.j2" - dest: "/etc/vector/vector.yaml" +- name: Replace vector ENV + become: "{{ true if 'sudo' in ansible_facts.packages else false }}" + ansible.builtin.lineinfile: + path: /etc/default/vector + regexp: '^VECTOR_CONFIG_DIR=' + line: VECTOR_CONFIG_DIR=/etc/vector + +- name: Delete default configs + become: "{{ true if 'sudo' in ansible_facts.packages else false }}" + ansible.builtin.file: + path: "{{ item }}" + state: absent + with_items: + - "/etc/vector/examples" + - "/etc/vector/vector.yaml" + register: vector_delete_default_configs + +- name: Copy config files + become: "{{ true if 'sudo' in ansible_facts.packages else false }}" + ansible.builtin.copy: + dest: "/etc/vector/{{ item.key }}.yaml" mode: "0644" backup: true - notify: vector_restart + content: "{{ item.value }}" + with_items: "{{ vector_configs_add | dict2items }}" + register: vector_copy_configs + +- name: Delete config files + become: "{{ true if 'sudo' in ansible_facts.packages else false }}" + ansible.builtin.file: + path: "/etc/vector/{{ item }}.yaml" + state: absent + with_items: "{{ vector_configs_del }}" + register: vector_delete_configs + +- name: Find config files + ansible.builtin.find: + paths: /etc/vector + register: find_configs + +- name: Get state vector.service + ansible.builtin.service_facts: + +- name: If need restart vector.service + debug: + msg: "vector.service will be restarted" + when: + - vector_copy_configs is changed or vector_delete_configs is changed or vector_delete_default_configs is changed + - find_configs.files | length > 0 + changed_when: true + notify: + - vector_restart + - vector_enable + +- name: If need stop vector.service + debug: + msg: "vector.service will be stopped" + when: + - find_configs.files | length == 0 + - '"running" in ansible_facts.services["vector.service"]["state"]' + changed_when: true + notify: + - vector_stop + - vector_disable diff --git a/tasks/empty.yml b/tasks/empty.yml new file mode 100644 index 0000000..ed97d53 --- /dev/null +++ b/tasks/empty.yml @@ -0,0 +1 @@ +--- diff --git a/tasks/install/apt.yml b/tasks/install/apt.yml new file mode 100644 index 0000000..16c1acf --- /dev/null +++ b/tasks/install/apt.yml @@ -0,0 +1,12 @@ +- name: Get vector distrib + ansible.builtin.get_url: + url: "https://apt.vector.dev/pool/v/ve/\ + vector_{{ vector_version }}-1_amd64.deb" + dest: "/tmp/vector_{{ vector_version }}-1_amd64.deb" + mode: "0644" + validate_certs: false + +- name: Install vector package + become: "{{ true if 'sudo' in ansible_facts.packages else false }}" + ansible.builtin.apt: + deb: /tmp/vector_{{ vector_version }}-1_amd64.deb diff --git a/tasks/install/dnf.yml b/tasks/install/dnf.yml new file mode 100644 index 0000000..be53bca --- /dev/null +++ b/tasks/install/dnf.yml @@ -0,0 +1,14 @@ +- name: Get vector distrib + ansible.builtin.get_url: + url: "https://yum.vector.dev/stable/vector-0/x86_64/\ + vector-{{ vector_version }}-1.x86_64.rpm" + dest: "/tmp/vector-{{ vector_version }}-1.x86_64.rpm" + mode: "0644" + validate_certs: false + +- name: Install vector package + become: "{{ true if 'sudo' in ansible_facts.packages else false }}" + ansible.builtin.dnf: + name: /tmp/vector-{{ vector_version }}-1.x86_64.rpm + state: present + disable_gpg_check: true diff --git a/tasks/install.yml b/tasks/install/yum.yml similarity index 65% rename from tasks/install.yml rename to tasks/install/yum.yml index 135c0a3..e8208cf 100644 --- a/tasks/install.yml +++ b/tasks/install/yum.yml @@ -4,16 +4,11 @@ vector-{{ vector_version }}-1.x86_64.rpm" dest: "/tmp/vector-{{ vector_version }}-1.x86_64.rpm" mode: "0644" + validate_certs: false -- name: Install vector packages - become: true +- name: Install vector package + become: "{{ true if 'sudo' in ansible_facts.packages else false }}" ansible.builtin.yum: name: /tmp/vector-{{ vector_version }}-1.x86_64.rpm + state: present disable_gpg_check: true - notify: vector_restart - -- name: Enable vector service - become: true - ansible.builtin.systemd: - name: vector - enabled: true diff --git a/tasks/main.yml b/tasks/main.yml index 63cc8aa..9ae54b8 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,9 +1,18 @@ --- # tasks file for vector -- include_tasks: - file: install.yml +- name: Gather the package facts + ansible.builtin.package_facts: + manager: auto + +- include_tasks: + file: "{{ lookup('first_found', params) }}" apply: tags: vector + vars: + params: + files: + - "install/{{ ansible_pkg_mgr }}.yml" + - 'empty.yml' tags: vector - include_tasks: diff --git a/templates/default.yaml.j2 b/templates/default.yaml.j2 new file mode 100644 index 0000000..2069b65 --- /dev/null +++ b/templates/default.yaml.j2 @@ -0,0 +1,21 @@ +sources: + dummy_logs: + type: "demo_logs" + format: "syslog" + interval: 1 + +transforms: + parse_logs: + type: "remap" + inputs: ["dummy_logs"] + source: | + . = parse_syslog!(string!(.message)) + +sinks: + print: + type: "console" + inputs: ["parse_logs"] + encoding: + codec: "json" + json: + pretty: true diff --git a/templates/vector.yaml.j2 b/templates/vector.yaml.j2 deleted file mode 100644 index 5afeae5..0000000 --- a/templates/vector.yaml.j2 +++ /dev/null @@ -1,20 +0,0 @@ -sources: - random_log: - type: "demo_logs" - format: "apache_common" - lines: - - "line1" - -sinks: - pull_to_clickhouse: - type: "clickhouse" - inputs: - - "random_log" - endpoint: "http://{{ vector_ch_address }}:{{ vector_ch_port }}" - database: "{{ vector_ch_db_name }}" - table: "{{ vector_ch_table_name }}" - auth: - strategy: "basic" - user: "{{ vector_ch_user }}" - password: "{{ vector_ch_password }}" - skip_unknown_fields: true