Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup of Windows tasks in zabbix_agent #1403

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions docs/ZABBIX_AGENT_ROLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,16 @@ Otherwise it just for the Zabbix Agent or for the Zabbix Agent 2.

* `zabbix_agent_win_include`: The directory in which the Zabbix Agent specific configuration files are stored.
* `zabbix_agent_win_logfile`: The full path to the logfile for the Zabbix Agent.
* `zabbix_agent_win_controlsocket`: Windows specific value for `zabbix_agent_controlsocket`. Default: `\\.\pipe\agent.sock`
* `zabbix_agent_win_pluginsocket`: Windows specific value for `zabbix_agent_pluginsocket`. Default: `\\.\pipe\agent.plugin.sock`
* `zabbix_version_long`: The long (major.minor.patch) version of the Zabbix Agent. This will be used to generate the `zabbix_win_package` and `zabbix_win_download_link` variables. This takes precedence over `zabbix_agent_version`.
* `zabbix_win_download_link`: The download url to the `win.zip` file.
* `zabbix_win_firewall_management`: Enable Windows firewall management (add service and port to allow rules). Default: `True`
* `zabbix_win_install_dir`: The directory where Zabbix needs to be installed.
* `zabbix_win_install_dir_conf`: The directory where Zabbix configuration file needs to be installed.
* `zabbix_win_install_dir_bin`: The directory where Zabbix binary file needs to be installed.
* `zabbix_win_package`: file name pattern (zip only). This will be used to generate the `zabbix_win_download_link` variable.
* `zabbix_win_config_remove`: Remove all existing configuration on update. Default `True`

## macOS Variables

Expand Down
4 changes: 4 additions & 0 deletions roles/zabbix_agent/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,14 @@ zabbix_version_long: 5.2.4

# Windows Related
zabbix_win_download_url: https://cdn.zabbix.com/zabbix/binaries/stable
zabbix_win_download_link: "{{ zabbix_win_download_url }}/{{ zabbix_version_long | regex_search('^\\d+\\.\\d+') }}/{{ zabbix_version_long }}/{{ zabbix_win_package | default(_win_package) }}"
zabbix_win_install_dir: 'C:\Zabbix'
zabbix_win_install_dir_conf: '{{ zabbix_win_install_dir }}\conf'
zabbix_win_install_dir_bin: '{{ zabbix_win_install_dir }}\bin'
zabbix_win_firewall_management: true
zabbix_win_config_remove: true
zabbix_agent_win_controlsocket: '\\.\pipe\agent.sock'
zabbix_agent_win_pluginsocket: '\\.\pipe\agent.plugin.sock'

# macOS Related
zabbix_mac_package: zabbix_agent-{{ zabbix_version_long }}-macos-amd64-openssl.pkg
Expand Down
2 changes: 2 additions & 0 deletions roles/zabbix_agent/handlers/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
win_service:
name: "{{ zabbix_win_svc_name }}"
state: restarted
register: zabbix_win_restart_result
until: zabbix_win_restart_result is succeeded
when:
- ansible_os_family == "Windows"

Expand Down
190 changes: 92 additions & 98 deletions roles/zabbix_agent/tasks/Windows.yml
Original file line number Diff line number Diff line change
@@ -1,46 +1,26 @@
---
- name: "Windows | Set some variables"
- name: "Windows | Set base variables"
ansible.builtin.set_fact:
zabbix_agent_win_download_link: "{{ zabbix_agent_win_download_link is defined | ternary(zabbix_agent_win_download_link, zabbix_agent2_win_download_link) | default(_win_download_link) }}"
zabbix_agent_win_logfile: "{{ zabbix_agent_win_logfile is defined | ternary(zabbix_agent_win_logfile, zabbix_agent2_win_logfile) | default(_win_logfile) }}"
zabbix_agent_win_package: "{{ zabbix_agent_win_package is defined | ternary(zabbix_agent_win_package, zabbix_agent2_win_package) | default(_win_package) }}"
zabbix_agent_win_include: "{{ zabbix_agent_win_include is defined | ternary(zabbix_agent_win_include, zabbix_agent2_win_include) | default(_win_include) }}"

- name: "Windows | Set default architecture"
ansible.builtin.set_fact:
windows_arch: 32
tags:
- always

- name: "Windows | Override architecture if 64-bit"
ansible.builtin.set_fact:
windows_arch: 64
when:
- '"64" in ansible_architecture'
tags:
- always

- name: "Windows | Set path to zabbix.exe"
ansible.builtin.set_fact:
zabbix_win_exe_path: '{{ zabbix_win_install_dir }}\bin\win{{ windows_arch }}\zabbix_agentd.exe'
tags:
- always

- name: "Windows | Set variables specific to Zabbix"
ansible.builtin.set_fact:
zabbix_win_install_dir_setup: '{{ zabbix_win_install_dir }}\setup'
zabbix_win_package: "{{ zabbix_win_package | default(_win_package) }}"
zabbix_win_svc_name: Zabbix Agent
zabbix_win_exe_path: '{{ zabbix_win_install_dir }}\bin\zabbix_agentd.exe'
zabbix_win_exe_name: 'zabbix_agentd.exe'
zabbix_win_exe_path: '{{ zabbix_win_install_dir_bin }}\zabbix_agentd.exe'
zabbix_win_config_name: "zabbix_agentd.conf"
zabbix2_win_svc_name: Zabbix Agent 2
zabbix2_win_exe_path: '{{ zabbix_win_install_dir }}\bin\zabbix_agent2.exe'
zabbix2_win_exe_name: 'zabbix_agent2.exe'
zabbix2_win_exe_path: '{{ zabbix_win_install_dir_bin }}\zabbix_agent2.exe'
zabbix2_win_config_name: "zabbix_agent2.conf"
tags:
- always

- name: "Windows | Check if Zabbix agent is present"
ansible.windows.win_stat:
path: "{{ item }}"
with_items:
loop:
- "{{ zabbix_win_exe_path }}"
- "{{ zabbix2_win_exe_path }}"
register: agent_file_info
Expand All @@ -53,14 +33,16 @@
register: zabbix_win_exe_info
when:
- item.stat.exists | bool
with_items: "{{ agent_file_info.results }}"
loop: "{{ agent_file_info.results }}"
loop_control:
label: "{{ item.item }}"
tags:
- always

- name: "Windows | Set facts current zabbix agent installation"
ansible.builtin.set_fact:
zabbix_agent_1_binary_exist: true
zabbix_agent_1_version: zabbix_win_exe_info.results[0].win_file_version.product_version
zabbix_agent_1_version: "{{ zabbix_win_exe_info.results[0].win_file_version.product_version }}"
when:
- zabbix_win_exe_info.results[0] is defined
- zabbix_win_exe_info.results[0].item.stat.exists
Expand All @@ -72,7 +54,7 @@
- name: "Windows | Set facts current zabbix agent installation (agent 2)"
ansible.builtin.set_fact:
zabbix_agent_2_binary_exist: true
zabbix_agent_2_version: zabbix_win_exe_info.results[1].win_file_version.product_version
zabbix_agent_2_version: "{{ zabbix_win_exe_info.results[1].win_file_version.product_version }}"
when:
- zabbix_win_exe_info.results[1] is defined
- zabbix_win_exe_info.results[1].item.stat.exists
Expand All @@ -83,10 +65,12 @@

- name: "Windows | Check Zabbix service"
ansible.windows.win_service:
name: "{{ (item.item.stat.path == zabbix_win_exe_path ) | ternary(zabbix_win_svc_name,zabbix2_win_svc_name) }}"
name: "{{ (item.item.stat.path == zabbix_win_exe_path) | ternary(zabbix_win_svc_name, zabbix2_win_svc_name) }}"
register: zabbix_service_info
when: item.item.stat.exists
with_items: "{{ zabbix_win_exe_info.results }}"
loop: "{{ zabbix_win_exe_info.results }}"
loop_control:
label: "{{ item.item.item }}"
tags:
- always

Expand Down Expand Up @@ -131,7 +115,6 @@
- name: "Windows | Stop Zabbix agent v1"
ansible.windows.win_service:
name: "{{ zabbix_win_svc_name }}"
start_mode: auto
state: stopped
when:
- zabbix_agent_version_change | default(false) or zabbix_agent2
Expand All @@ -140,7 +123,6 @@
- name: "Windows | Stop Zabbix agent v2"
ansible.windows.win_service:
name: "{{ zabbix2_win_svc_name }}"
start_mode: auto
state: stopped
when:
- zabbix_agent_version_change | default(false) or not zabbix_agent2
Expand All @@ -158,67 +140,88 @@
- zabbix_agent_version_change | default(false) or not zabbix_agent2
- zabbix_agent_2_service_exist | default(false)

- name: "Windows | Removing Zabbix Directory"
- name: "Windows | Removing Zabbix bin Directory"
ansible.windows.win_file:
path: "{{ zabbix_win_install_dir }}"
path: "{{ zabbix_win_install_dir_bin }}"
state: absent
register: zabbix_agent_dir_remove_result
until: zabbix_agent_dir_remove_result is succeeded
retries: 5
delay: 15
when:
((zabbix_agent_version_change | default(false) or zabbix_agent2) and zabbix_agent_1_binary_exist | default(false)) or
((zabbix_agent_version_change | default(false) or not zabbix_agent2) and zabbix_agent_2_binary_exist | default(false))

- name: "Windows | Removing Zabbix conf Directory"
ansible.windows.win_file:
path: "{{ zabbix_win_install_dir_conf }}"
state: absent
register: zabbix_agent_dir_remove_result
until: zabbix_agent_dir_remove_result is succeeded
retries: 5
delay: 15
when:
- zabbix_win_config_remove
- >
((zabbix_agent_version_change | default(false) or zabbix_agent2) and zabbix_agent_1_binary_exist | default(false)) or
((zabbix_agent_version_change | default(false) or not zabbix_agent2) and zabbix_agent_2_binary_exist | default(false))

###################
# install section #
###################

- name: "Windows | Set installation settings (agent 2)"
ansible.builtin.set_fact:
zabbix_win_exe_path: "{{ zabbix2_win_exe_path }}"
zabbix_win_exe_name: "{{ zabbix2_win_exe_name }}"
zabbix_win_config_name: "{{ zabbix2_win_config_name }}"
zabbix_win_svc_name: "{{ zabbix2_win_svc_name }}"
when: zabbix_agent2 | bool
tags:
- install

- name: "Windows | Check if agent binaries in place"
ansible.windows.win_stat:
path: "{{ zabbix_win_exe_path }}"
register: zabbix_windows_binaries
tags:
- install

- name: "Windows | Create directory structure"
ansible.windows.win_file:
path: "{{ item }}"
state: directory
with_items:
loop:
- "{{ zabbix_win_install_dir }}"
- "{{ zabbix_win_install_dir_conf }}"
- "{{ zabbix_win_install_dir_bin }}"
- "{{ zabbix_win_install_dir_setup }}"
when: zabbix_agent_version_change | default(false) or not zabbix_windows_binaries.stat.exists
tags:
- install

- name: "Windows | Create directory structure, includes"
ansible.windows.win_file:
path: "{{ item }}"
path: "{{ zabbix_agent_win_include }}"
state: directory
with_items:
- "{{ zabbix_agent_win_include }}"
when:
- ('.conf' not in zabbix_agent_win_include)
tags:
- install

- name: "Windows | Set installation settings (agent 2)"
ansible.builtin.set_fact:
zabbix_win_package: "{{ zabbix_win_package | default(_win_package) }}"
zabbix_win_download_link: "{{ zabbix_win_download_link | default(_win_download_link) }}"
zabbix_win_exe_path: "{{ zabbix2_win_exe_path }}"
zabbix_win_config_name: "{{ zabbix2_win_config_name }}"
zabbix_win_svc_name: "{{ zabbix2_win_svc_name }}"
when: zabbix_agent2 | bool
tags:
- install

- name: "Windows | Check if agent file is already downloaded"
ansible.windows.win_stat:
path: '{{ zabbix_win_install_dir }}\{{ zabbix_win_package }}'
path: '{{ zabbix_win_install_dir_setup }}\{{ zabbix_win_package }}'
when:
- zabbix_agent_version_change | default(false) or not zabbix_windows_binaries.stat.exists
register: file_info
tags:
- install

- name: "Windows | Check if agent binaries in place"
ansible.windows.win_stat:
path: "{{ zabbix_win_exe_path }}"
register: zabbix_windows_binaries
tags:
- install

- name: "Windows | Download Zabbix Agent Zip file"
ansible.windows.win_get_url:
url: "{{ zabbix_win_download_link }}"
dest: '{{ zabbix_win_install_dir }}\{{ zabbix_win_package }}'
dest: '{{ zabbix_win_install_dir_setup }}\{{ zabbix_win_package }}'
url_username: "{{ zabbix_download_user | default(omit) }}"
url_password: "{{ zabbix_download_pass | default(omit) }}"
force: false
Expand All @@ -227,8 +230,8 @@
validate_certs: "{{ zabbix_download_validate_certs | default(False) | bool }}"
timeout: "{{ zabbix_download_timeout | default(120) | int }}"
when:
- zabbix_agent_version_change | default(false) or not zabbix_windows_binaries.stat.exists
- not file_info.stat.exists
- not zabbix_windows_binaries.stat.exists
register: zabbix_agent_win_download_zip
until: zabbix_agent_win_download_zip is succeeded
throttle: "{{ zabbix_download_throttle | default(5) | int }}"
Expand All @@ -237,61 +240,52 @@

- name: "Windows | Unzip file"
community.windows.win_unzip:
src: '{{ zabbix_win_install_dir }}\{{ zabbix_win_package }}'
dest: "{{ zabbix_win_install_dir }}"
creates: "{{ zabbix_win_exe_path }}"
tags:
- install

- name: "Windows | Cleanup downloaded Zabbix Agent Zip file"
ansible.windows.win_file:
path: '{{ zabbix_win_install_dir }}\{{ zabbix_win_package }}'
state: absent
src: '{{ zabbix_win_install_dir_setup }}\{{ zabbix_win_package }}'
dest: "{{ zabbix_win_install_dir_setup }}"
when:
- zabbix_agent_win_download_zip.changed
- zabbix_agent_version_change | default(false) or not zabbix_windows_binaries.stat.exists
- zabbix_agent_win_download_zip is changed or file_info.stat.exists
tags:
- install

- name: "Windows | Copy binary files to expected location"
ansible.windows.win_copy:
src: "{{ zabbix_win_install_dir }}\\bin\\{{ item }}"
dest: "{{ zabbix_win_install_dir_bin }}\\{{ item }}"
remote_src: yes
loop:
- zabbix_agentd.exe
- zabbix_sender.exe
src: "{{ zabbix_win_install_dir_setup }}\\bin\\"
dest: "{{ zabbix_win_install_dir_bin }}\\"
remote_src: true
when:
- zabbix_win_install_dir_bin is defined
- not (zabbix_agent2 | bool)
- zabbix_agent_version_change | default(false) or not zabbix_windows_binaries.stat.exists
- zabbix_agent_win_download_zip is changed or file_info.stat.exists
tags:
- install

- name: "Windows | Copy binary files to expected location (zabbix-agent2)"
- name: "Windows | Copy initial configuration to expected location"
ansible.windows.win_copy:
src: "{{ zabbix_win_install_dir }}\\bin\\{{ item }}"
dest: "{{ zabbix_win_install_dir_bin }}\\{{ item }}"
remote_src: yes
loop:
- zabbix_agent2.exe
src: "{{ zabbix_win_install_dir_setup }}\\conf\\"
dest: "{{ zabbix_win_install_dir_conf }}\\"
force: false
remote_src: true
when:
- zabbix_win_install_dir_bin is defined
- zabbix_agent2 | bool
- zabbix_agent_win_download_zip is changed or file_info.stat.exists | default(false)
tags:
- install

- set_fact:
zabbix_win_exe_path: "{{ zabbix_win_install_dir_bin }}\\zabbix_agentd.exe"
- name: "Windows | Copy plugin configuration to expected location on Agent 2"
ansible.windows.win_copy:
src: "{{ zabbix_win_install_dir_setup }}\\conf\\zabbix_agent2.d\\plugins.d"
dest: "{{ zabbix_win_install_dir_conf }}\\zabbix_agent2.d\\plugins.d"
force: false
remote_src: true
when:
- zabbix_win_install_dir_bin is defined
- not (zabbix_agent2 | bool)
- zabbix_agent2
- zabbix_agent_win_download_zip is changed or file_info.stat.exists | default(false)
tags:
- install

- set_fact:
zabbix_win_exe_path: "{{ zabbix_win_install_dir_bin }}\\zabbix_agent2.exe"
when:
- zabbix_win_install_dir_bin is defined
- zabbix_agent2 | bool
- name: "Windows | Cleanup temporary setup directory"
ansible.windows.win_file:
path: '{{ zabbix_win_install_dir_setup }}'
state: absent
tags:
- install

Expand Down
Loading