Added uninstall feature

This commit is contained in:
2026-03-13 09:14:07 -04:00
parent 619cb1a3e7
commit 70fcdbf58f
3 changed files with 48 additions and 2 deletions

11
Jenkinsfile vendored
View File

@@ -7,6 +7,11 @@ pipeline {
defaultValue: '',
description: 'Limit to specific hosts or groups (e.g., "vms", "lxcs", "zabbix_targets", or hostname) no .lan required'
)
booleanParam(
name: 'UNINSTALL',
defaultValue: false,
description: 'Remove Zabbix agent2 and purge configuration from target hosts'
)
booleanParam(
name: 'DRY_RUN',
defaultValue: false,
@@ -43,6 +48,7 @@ pipeline {
script {
def limitFlag = params.LIMIT ? "--limit '${params.LIMIT}'" : ''
def checkFlag = params.DRY_RUN ? '--check --diff' : ''
def stateVar = params.UNINSTALL ? '-e "zabbix_state=absent"' : ''
sh """
ansible-playbook \
@@ -50,6 +56,7 @@ pipeline {
playbooks/install_zabbix.yml \
${limitFlag} \
${checkFlag} \
${stateVar} \
-v
"""
}
@@ -59,10 +66,10 @@ pipeline {
post {
success {
echo "Zabbix agent installation completed successfully"
echo "Zabbix agent ${params.UNINSTALL ? 'removal' : 'installation'} completed successfully"
}
failure {
echo "Zabbix agent installation failed"
echo "Zabbix agent ${params.UNINSTALL ? 'removal' : 'installation'} failed"
}
}
}

View File

@@ -1,4 +1,5 @@
---
zabbix_state: "present"
zabbix_server: "zabbix.lan"
zabbix_version: "7.0"
zabbix_release: "7.0-2"

View File

@@ -1,43 +1,51 @@
---
# Install tasks
- name: Download Zabbix repository package
ansible.builtin.get_url:
url: "https://repo.zabbix.com/zabbix/{{ zabbix_version }}/ubuntu/pool/main/z/zabbix-release/zabbix-release_{{ zabbix_release }}+ubuntu{{ ansible_distribution_version }}_all.deb"
dest: /tmp/zabbix-release.deb
mode: '0644'
when: zabbix_state == 'present'
- name: Install Zabbix repository package
ansible.builtin.apt:
deb: /tmp/zabbix-release.deb
when: zabbix_state == 'present'
- name: Install Zabbix agent2
ansible.builtin.apt:
name: zabbix-agent2
state: present
update_cache: true
when: zabbix_state == 'present'
- name: Configure Zabbix server address
ansible.builtin.lineinfile:
path: /etc/zabbix/zabbix_agent2.conf
regexp: '^Server='
line: "Server={{ zabbix_server }}"
when: zabbix_state == 'present'
- name: Configure Zabbix active server address
ansible.builtin.lineinfile:
path: /etc/zabbix/zabbix_agent2.conf
regexp: '^ServerActive='
line: "ServerActive={{ zabbix_server }}"
when: zabbix_state == 'present'
- name: Configure Zabbix agent hostname
ansible.builtin.lineinfile:
path: /etc/zabbix/zabbix_agent2.conf
regexp: '^Hostname='
line: "Hostname={{ ansible_hostname }}"
when: zabbix_state == 'present'
- name: Configure Zabbix agent listen IP
ansible.builtin.lineinfile:
path: /etc/zabbix/zabbix_agent2.conf
regexp: '^#?ListenIP='
line: "ListenIP=127.0.0.1"
when: zabbix_state == 'present'
- name: Enable and start Zabbix agent2
ansible.builtin.systemd:
@@ -45,3 +53,33 @@
state: started
enabled: true
daemon_reload: true
when: zabbix_state == 'present'
# Uninstall tasks
- name: Stop and disable Zabbix agent2
ansible.builtin.systemd:
name: zabbix-agent2
state: stopped
enabled: false
when: zabbix_state == 'absent'
ignore_errors: true
- name: Remove Zabbix agent2 package
ansible.builtin.apt:
name: zabbix-agent2
state: absent
purge: true
when: zabbix_state == 'absent'
- name: Remove Zabbix repository package
ansible.builtin.apt:
name: zabbix-release
state: absent
purge: true
when: zabbix_state == 'absent'
- name: Remove Zabbix configuration directory
ansible.builtin.file:
path: /etc/zabbix
state: absent
when: zabbix_state == 'absent'