From c229ac6c2db47586745e6695b01ef7b8eb925684 Mon Sep 17 00:00:00 2001 From: Jeffrey van Pelt Date: Fri, 19 Jul 2024 15:07:04 +0200 Subject: [PATCH] Reworked include logic in agent role(#1335) --- .../fragments/1335-rework-include-logic.yml | 3 +++ roles/zabbix_agent/defaults/main.yml | 2 +- roles/zabbix_agent/tasks/Docker.yml | 1 + roles/zabbix_agent/tasks/Linux.yml | 16 ++++++++++++++++ roles/zabbix_agent/tasks/main.yml | 4 ---- roles/zabbix_agent/templates/agent.conf.j2 | 12 +++++++++++- roles/zabbix_agent/vars/agent2_vars.yml | 4 +++- 7 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 changelogs/fragments/1335-rework-include-logic.yml diff --git a/changelogs/fragments/1335-rework-include-logic.yml b/changelogs/fragments/1335-rework-include-logic.yml new file mode 100644 index 000000000..f7b3c0f2c --- /dev/null +++ b/changelogs/fragments/1335-rework-include-logic.yml @@ -0,0 +1,3 @@ +--- +minor_changes: + - zabbix_agent Role - Reworked Include logic based on Alias logic diff --git a/roles/zabbix_agent/defaults/main.yml b/roles/zabbix_agent/defaults/main.yml index 448f7cd16..9f090a12c 100644 --- a/roles/zabbix_agent/defaults/main.yml +++ b/roles/zabbix_agent/defaults/main.yml @@ -136,7 +136,7 @@ zabbix_agent_docker_ports: zabbix_agent_docker_security_opts: - apparmor:unconfined zabbix_agent_docker_volumes: - - /etc/zabbix/zabbix_agentd.d:{{ zabbix_agent_include_dir }} + - /etc/zabbix/zabbix_agentd.d:{{ zabbix_agent_docker_include_dir }} - /:/hostfs:ro - /etc:/hostfs/etc:ro - /proc:/hostfs/proc:ro diff --git a/roles/zabbix_agent/tasks/Docker.yml b/roles/zabbix_agent/tasks/Docker.yml index 031a5fe61..51fa0d048 100644 --- a/roles/zabbix_agent/tasks/Docker.yml +++ b/roles/zabbix_agent/tasks/Docker.yml @@ -1,6 +1,7 @@ --- - name: "Create volume mount string" ansible.builtin.set_fact: + zabbix_agent_docker_include_dir: "{{ (zabbix_agent_include_dir is iterable) | ternary(zabbix_agent_include_dir[0], zabbix_agent_include_dir) }}" volume_mount: "{{ zabbix_agent_tlspskfile }}:/var/lib/zabbix/enc/tlspskfile" tls_key: ZBX_TLSPSKFILE: tlspskfile diff --git a/roles/zabbix_agent/tasks/Linux.yml b/roles/zabbix_agent/tasks/Linux.yml index d11324b70..9aeb3c236 100644 --- a/roles/zabbix_agent/tasks/Linux.yml +++ b/roles/zabbix_agent/tasks/Linux.yml @@ -138,6 +138,22 @@ become: true tags: - config + when: zabbix_agent_include_dir is string + +- name: "Create include dirs zabbix-agent" + ansible.builtin.file: + path: "{{ include_dir }}" + owner: root + group: zabbix + mode: "{{ zabbix_agent_include_mode }}" + state: directory + loop: "{{ zabbix_agent_include_dir | list }}" # To prevent errors, filter a string value as a list + loop_control: + loop_var: 'include_dir' + when: zabbix_agent_include_dir is iterable + become: true + tags: + - config - name: "Install the Docker container" ansible.builtin.include_tasks: Docker.yml diff --git a/roles/zabbix_agent/tasks/main.yml b/roles/zabbix_agent/tasks/main.yml index a809829ae..d6b9ca4fd 100644 --- a/roles/zabbix_agent/tasks/main.yml +++ b/roles/zabbix_agent/tasks/main.yml @@ -35,10 +35,6 @@ zabbix_agent_tlsaccept: "{{ zabbix_agent_tlsaccept is defined | ternary(zabbix_agent_tlsaccept, 'unencrypted')}}" zabbix_agent_tlsconnect: "{{ zabbix_agent_tlsconnect is defined | ternary(zabbix_agent_tlsconnect, 'unencrypted')}}" -- name: Set Include Files - ansible.builtin.set_fact: - zabbix_agent_include: "{{ zabbix_agent_include is defined | ternary(zabbix_agent_include, zabbix_agent_include_dir + '/*.conf') }}" - - name: Setting Zabbix API Server Port ansible.builtin.set_fact: zabbix_api_server_port: "{{ '443' if zabbix_api_use_ssl|bool else '80' }}" diff --git a/roles/zabbix_agent/templates/agent.conf.j2 b/roles/zabbix_agent/templates/agent.conf.j2 index d8324002c..003adaee9 100644 --- a/roles/zabbix_agent/templates/agent.conf.j2 +++ b/roles/zabbix_agent/templates/agent.conf.j2 @@ -66,7 +66,17 @@ DenyKey={{ item }} {{ (zabbix_agent_hostmetadataitem is defined and zabbix_agent_hostmetadataitem is not none) | ternary('', '# ') }}HostMetadataItem={{ zabbix_agent_hostmetadataitem | default('') }} {{ (zabbix_agent_hostname is defined and zabbix_agent_hostname is not none) | ternary('', '# ') }}Hostname={{ zabbix_agent_hostname | default('') }} {{ (zabbix_agent_hostnameitem is defined and zabbix_agent_hostnameitem is not none) | ternary('', '# ') }}HostnameItem={{ zabbix_agent_hostnameitem | default('') }} -{{ (zabbix_agent_include is defined and zabbix_agent_include is not none) | ternary('', '# ') }}Include={{ zabbix_agent_include | default('') }} +{% if zabbix_agent_include_dir is defined and zabbix_agent_include_dir %} +{% if zabbix_agent_include_dir is string %} +Include={{ zabbix_agent_include_dir }}/*.conf +{% else %} +{% for item in zabbix_agent_include_dir %} +Include={{ item }}/*.conf +{% endfor %} +{% endif %} +{% else %} +# Include= +{% endif %} {% if not zabbix_agent2 %} {{ (zabbix_agent_listenbacklog is defined and zabbix_agent_listenbacklog is not none) | ternary('', '# ') }}ListenBacklog={{ zabbix_agent_listenbacklog | default('') }} {% endif %} diff --git a/roles/zabbix_agent/vars/agent2_vars.yml b/roles/zabbix_agent/vars/agent2_vars.yml index ebd79716e..119aa3a93 100644 --- a/roles/zabbix_agent/vars/agent2_vars.yml +++ b/roles/zabbix_agent/vars/agent2_vars.yml @@ -1,6 +1,8 @@ _pidfile: /var/run/zabbix/zabbix_agent2.pid _logfile: /var/log/zabbix/zabbix_agent2.log -_include: /etc/zabbix/zabbix_agent2.d +_include: + - /etc/zabbix/zabbix_agent2.d + - /etc/zabbix/zabbix_agent2.d/plugins.d _tls_subject: "{{ zabbix_agent_tlsservercertsubject | default(omit) }}" # FIXME this is not correct and should be removed with 2.0.0, here only to prevent regression _win_package: zabbix_agent2-{{ zabbix_version_long }}-windows-amd64-openssl-static.zip _win_download_link: "{{ zabbix_win_download_url }}/{{ zabbix_version_long | regex_search('^\\d+\\.\\d+') }}/{{ zabbix_version_long }}/{{ zabbix_win_package | default(_win_package) }}"