Merge pull request 'added var to define config file path for zabbix scripts; linted the damn thing' (#12) from staging into main

Reviewed-on: #12
Reviewed-by: meaz <meaz@no-reply@disroot.org>
This commit is contained in:
muppeth 2024-12-30 08:43:47 +01:00
commit de01805965
12 changed files with 196 additions and 147 deletions

6
.ansible-lint Normal file
View file

@ -0,0 +1,6 @@
skip_list:
- fqcn-builtins
- fqcn[action]
- fqcn[action-core]
- fqcn[canonical]
- name[casing]

View file

@ -1,6 +1,7 @@
---
- hosts: zabbix
- name: 'Zabbix example playbook'
hosts: zabbix
roles:
- mariadb
- zabbix

View file

@ -1,25 +1,25 @@
---
#main
# Main
zabbix_version: '7.0'
zabbix_agent: 'true' # set to true if you want to deploy it.
zabbix_server: 'true' # set to true if you want to deploy it.
zabbix_proxy: 'false' # set to true if you want to deploy it.
#Zabbix Server
# Zabbix Server
zabbix_server_pkg:
- zabbix-server-mysql
- zabbix-frontend-php
- zabbix-nginx-conf
- zabbix-sql-scripts
- gzip
- python3-pymysql
- 'zabbix-server-mysql'
- 'zabbix-frontend-php'
- 'zabbix-nginx-conf'
- 'zabbix-sql-scripts'
- 'gzip'
- 'python3-pymysql'
zabbix_server_hostname: 'localhost'
zabbix_server_name: 'Monitoring'
zabbix_server_listen_port: '10051'
zabbix_server_log_path: '/var/log/zabbix/zabbix_server.log'
zabbix_server_log_size: '1' #in MB
zabbix_server_log_size: '1' # in MB
zabbix_debug_level: '3'
# 0 - basic information about starting and stopping of Zabbix processes
# 1 - critical information
@ -43,7 +43,7 @@ zabbix_server_fping6: '/usr/bin/fping6'
zabbix_server_queries_slow: '3000'
zabbix_cachesize: '8M'
#Zabbix Client
# Zabbix Client
zabbix_web_listen: '8080'
zabbix_web_servername: 'example.com'
@ -53,12 +53,13 @@ zabbix_agent_log_size: '1'
zabbix_agent_debug_level: '3'
zabbix_agent_server_address: "{{ zabbix_server_hostname }}"
zabbix_agent_listen_port: '10050'
zabbix_agent_scripts_config_path: '/etc/container-scripts/zabbix-scripts'
zabbix_agent_pkg:
- zabbix-agent
- gzip
- zabbix-sender
- 'zabbix-agent'
- 'gzip'
- 'zabbix-sender'
#Zabbix Scripts
# Zabbix Scripts
zabbix_agent_params: # this deploys userparameters
- name: "container" # this one is useful for LXC containers
@ -66,17 +67,17 @@ zabbix_scripts_deploy: 'false'
zabbix_scripts_repo: '' # add your script repo address here
zabbix_scripts_dir: '/usr/local/bin/zabbix-scripts'
zabbix_scripts_version: 'main'
#zabbix_scripts_config_items: # Uncomment those if you want to add more config items from your scripts
# - key: ''
# value: ''
# zabbix_scripts_config_items: # Uncomment those if you want to add more config items from your scripts
# - key: ''
# value: ''
#Zabbix Proxy
# Zabbix Proxy
zabbix_proxy_pkg:
- zabbix-proxy-mysql
- gzip
- 'zabbix-proxy-mysql'
- 'gzip'
zabbix_apache:
- apache2
- libapache2-mod-php5
- 'apache2'
- 'libapache2-mod-php5'
zabbix_proxy_mode: '0'
zabbix_proxy_master_server: 'monitoring.example.com'
zabbix_proxy_hostname: 'proxy.example.lan'
@ -102,7 +103,7 @@ zabbix_pm_min_spare_servers: '5'
zabbix_pm_max_spare_servers: '35'
zabbix_pm_max_requests: '200'
#MariaDB
# MariaDB
mariadb_databases:
- name: '{{ zabbix_server_db_name }}'
collation: 'utf8mb4_bin'

View file

@ -1,21 +1,21 @@
---
- name: restart zabbix_server
- name: 'Restart zabbix_server'
systemd:
name: zabbix-server
state: restarted
name: 'zabbix-server'
state: 'restarted'
- name: restart zabbix_agent
- name: 'Restart zabbix_agent'
systemd:
name: zabbix-agent
state: restarted
name: 'zabbix-agent'
state: 'restarted'
- name: restart zabbix_proxy
- name: 'Restart zabbix_proxy'
systemd:
name: zabbix-proxy
state: restarted
name: 'zabbix-proxy'
state: 'restarted'
- name: restart nginx
- name: 'Restart nginx'
systemd:
name: nginx
state: restarted
name: 'nginx'
state: 'restarted'

View file

@ -1,12 +1,12 @@
---
- name: "[APT] - Install gpg key"
- name: '[APT] - Install gpg key'
apt_key:
id: 79EA5ED4
url: http://repo.zabbix.com/zabbix-official-repo.key
id: '79EA5ED4'
url: 'http://repo.zabbix.com/zabbix-official-repo.key'
- name: "[APT] - Add repository Debian"
- name: '[APT] - Add repository Debian'
apt_repository:
repo: "deb http://repo.zabbix.com/zabbix/{{ zabbix_version }}/debian/ {{ ansible_distribution_release }} main"
state: present
filename: zabbix
repo: 'deb http://repo.zabbix.com/zabbix/{{ zabbix_version }}/debian/ {{ ansible_distribution_release }} main'
state: 'present'
filename: 'zabbix'

View file

@ -8,7 +8,8 @@
group: 'root'
mode: '0644'
when: zabbix_server == 'true'
notify: restart zabbix_server
notify:
- 'Restart zabbix_server'
- name: '[Configure] - Configure Zabbix Proxy'
template:
@ -18,7 +19,8 @@
group: 'root'
mode: '0644'
when: zabbix_proxy == 'true'
notify: restart zabbix_proxy
notify:
- 'Restart zabbix_proxy'
- name: '[Configure] - Configure Zabbix frontend'
template:
@ -26,8 +28,10 @@
dest: '/etc/zabbix/web/zabbix.conf.php'
owner: 'www-data'
group: 'www-data'
mode: '0644'
when: zabbix_server == 'true'
notify: restart zabbix_server
notify:
- 'Restart zabbix_server'
- name: '[Configure] - Configure nginx'
template:
@ -37,7 +41,8 @@
group: 'root'
mode: '0644'
when: zabbix_server == 'true'
notify: restart zabbix_server
notify:
- 'Restart zabbix_server'
- name: '[Configure] - Configure Zabbix php'
template:
@ -47,7 +52,8 @@
group: 'root'
mode: '0644'
when: zabbix_server == 'true'
notify: restart zabbix_server
notify:
- 'Restart zabbix_server'
- name: '[Configure] - Configure Zabbix Agent'
template:
@ -57,4 +63,5 @@
group: 'root'
mode: '0644'
when: zabbix_agent == 'true'
notify: restart zabbix_agent
notify:
- 'Restart zabbix_agent'

View file

@ -6,5 +6,6 @@
dest: '/var/lib/zabbix/'
owner: 'zabbix'
group: 'zabbix'
mode: '0755'
loop: '{{ zabbix_agent_params }}'
when: zabbix_agent_params is defined and item.name == 'postgres'

View file

@ -1,25 +1,29 @@
---
- name: "[Agent] - Install Zabbix from repository"
- name: '[Agent] - Install Zabbix from repository'
apt:
name: "{{ zabbix_agent_pkg }}"
state: latest
update_cache: yes
install_recommends: no
cache_valid_time: 3600
name: '{{ zabbix_agent_pkg }}'
update_cache: true
install_recommends: false
cache_valid_time: '3600'
notify:
- restart zabbix_agent
tags: agent
- 'Restart zabbix_agent'
tags:
- 'agent'
- name: "[Agent] - Deploy Zabbix custom userparameters"
- name: '[Agent] - Deploy Zabbix custom userparameters'
template:
src: 'templates/etc/zabbix/zabbix_agent.d/userparameter_container.conf.j2'
dest: '/etc/zabbix/zabbix_agentd.d/userparameter_container.conf'
owner: 'root'
group: 'root'
mode: '0755'
notify:
- restart zabbix_agent
tags: agent
- 'Restart zabbix_agent'
tags:
- 'agent'
- name: "[Agent] - Deploy Zabbix scripts"
- name: '[Agent] - Deploy Zabbix scripts'
git:
repo: '{{ zabbix_scripts_repo }}'
dest: '{{ zabbix_scripts_dir }}'
@ -27,27 +31,36 @@
version: '{{ zabbix_scripts_version }}'
when: zabbix_scripts_deploy == 'true'
notify:
- restart zabbix_agent
tags: agent
- 'Restart zabbix_agent'
tags:
- 'agent'
- name: '[Agent] - Ensure config dir path exists'
file:
path: '/var/local/container-scripts'
path: '{{ zabbix_agent_scripts_config_path }}'
state: 'directory'
owner: 'root'
group: 'root'
mode: '0755'
tags: agent
tags:
- 'agent'
- name: "[Agent] - Configure scripts"
- name: '[Agent] - Configure scripts'
template:
src: 'zabbix-scripts.cfg.j2'
dest: '/var/local/container-scripts/zabbix-scripts.cfg'
dest: '{{ zabbix_agent_scripts_config_path }}/zabbix-scripts.cfg'
owner: 'root'
group: 'root'
mode: '0755'
when: zabbix_scripts_deploy == 'true'
notify:
- restart zabbix_agent
tags: agent
- 'Restart zabbix_agent'
tags:
- 'agent'
- name: "[Agent] - Make sure the service is enabled"
- name: '[Agent] - Make sure the service is enabled'
systemd:
name: zabbix-agent
enabled: yes
tags: agent
name: 'zabbix-agent'
enabled: true
tags:
- 'agent'

View file

@ -2,45 +2,49 @@
- name: '[Proxy] - Install Zabbix Proxy from repository'
apt:
name: "{{ zabbix_proxy_pkg }}"
state: latest
update_cache: yes
install_recommends: no
cache_valid_time: 3600
name: '{{ zabbix_proxy_pkg }}'
update_cache: true
install_recommends: false
cache_valid_time: '3600'
notify:
- restart zabbix_proxy
tags: proxy
- 'Restart zabbix_proxy'
tags:
- 'proxy'
- name: '[Proxy] - Remove apache2'
apt:
name: "{{ zabbix_apache }}"
state: absent
tags: proxy
name: '{{ zabbix_apache }}'
state: 'absent'
tags:
- 'proxy'
- name: "[Proxy] - Check if Zabbix proxy is already installed"
- name: '[Proxy] - Check if Zabbix proxy is already installed'
shell:
cmd: "{{ zabbix_server_db_plugin }} -h {{ zabbix_proxy_db_host }} -u {{ zabbix_proxy_db_user }} -p{{ zabbix_proxy_db_passwd }} {{ zabbix_proxy_db_name }} -se 'SELECT * from dbversion;'"
register: zabbixproxy_installed
ignore_errors: true
tags: proxy
tags:
- 'proxy'
- name: '[Proxy] - Importing Zabbix Proxy schema. Onetime only shot'
mysql_db:
login_host: "{{ zabbix_proxy_db_host }}"
login_password: "{{ zabbix_proxy_db_passwd }}"
login_host: '{{ zabbix_proxy_db_host }}'
login_password: '{{ zabbix_proxy_db_passwd }}'
login_user: '{{ zabbix_proxy_db_user }}'
name: "{{ zabbix_proxy_db_name }}"
state: import
target: /usr/share/zabbix-sql-scripts/mysql/proxy.sql
name: '{{ zabbix_proxy_db_name }}'
state: 'import'
target: '/usr/share/zabbix-sql-scripts/mysql/proxy.sql'
when: "'ERROR' in zabbixproxy_installed.stderr"
# ERROR 1146 means the table doesn't exist
notify:
- restart zabbix_server
tags: proxy
- 'Restart zabbix_server'
tags:
- 'proxy'
- name: "[Proxy] - Make sure the service is enabled"
- name: '[Proxy] - Make sure the service is enabled'
systemd:
name: zabbix-proxy
enabled: yes
state: restarted
tags: proxy
name: 'zabbix-proxy'
enabled: true
state: 'restarted'
tags:
- 'proxy'

View file

@ -2,44 +2,47 @@
- name: '[Server] - Install Zabbix from repository'
apt:
pkg: "{{ zabbix_server_pkg }}"
state: latest
update_cache: yes
install_recommends: no
cache_valid_time: 3600
pkg: '{{ zabbix_server_pkg }}'
update_cache: true
install_recommends: false
cache_valid_time: '3600'
notify:
- restart zabbix_server
tags: server
- 'Restart zabbix_server'
tags:
- 'server'
- name: '[Server] - Remove apache2'
apt:
name: "{{ zabbix_apache }}"
state: absent
name: '{{ zabbix_apache }}'
state: 'absent'
- name: "[Server] - Check if Zabbix is already installed"
- name: '[Server] - Check if Zabbix is already installed'
shell:
cmd: "{{ zabbix_server_db_plugin }} -h {{ zabbix_server_db_host }} -u {{ zabbix_server_db_user }} -p{{ zabbix_server_db_passwd }} {{ zabbix_server_db_name }} -se 'SELECT instanceid from config;'"
register: zabbix_installed
register: 'zabbix_installed'
ignore_errors: true
tags: server
tags:
- 'server'
- name: '[Server] - Importing Zabbix database. Onetime only shot'
mysql_db:
login_host: "{{ zabbix_server_db_host }}"
login_password: "{{ zabbix_server_db_passwd }}"
login_host: '{{ zabbix_server_db_host }}'
login_password: '{{ zabbix_server_db_passwd }}'
login_user: '{{ zabbix_server_db_user }}'
name: "{{ zabbix_server_db_name }}"
state: import
target: /usr/share/zabbix-sql-scripts/mysql/server.sql.gz
name: '{{ zabbix_server_db_name }}'
state: 'import'
target: '/usr/share/zabbix-sql-scripts/mysql/server.sql.gz'
when: "'ERROR 1146' in zabbix_installed.stderr"
# ERROR 1146 means the table doesn't exist
notify:
- restart zabbix_server
- restart nginx
tags: server
- 'Restart zabbix_server'
- 'Restart nginx'
tags:
- 'server'
- name: "[Server] - Make sure the service is enabled"
- name: '[Server] - Make sure the service is enabled'
systemd:
name: zabbix-server
enabled: yes
tags: server
name: 'zabbix-server'
enabled: true
tags:
- 'server'

View file

@ -1,23 +1,34 @@
---
- include_tasks: update.yml
- name: '[Main] - Include update tasks'
include_tasks: update.yml
- include_tasks: apt.yml
- name: '[Main] - Include apt tasks'
include_tasks: apt.yml
- include_tasks: install_server.yml
- name: '[Main] - Include server install tasks'
include_tasks: install_server.yml
when: zabbix_server == 'true'
tags: server
tags:
- 'server'
- include_tasks: install_agent.yml
- name: '[Main] - Include agent install tasks'
include_tasks: install_agent.yml
when: zabbix_agent == 'true'
tags: agent
tags:
- 'agent'
- include_tasks: custom_agent.yml
- name: '[Main] - Include agent custom tasks'
include_tasks: custom_agent.yml
when: zabbix_agent == 'true'
tags: agent
tags:
- 'agent'
- include_tasks: install_proxy.yml
- name: '[Main] - Include proxy install tasks'
include_tasks: install_proxy.yml
when: zabbix_proxy == 'true'
tags: proxy
tags:
- 'proxy'
- include_tasks: configure.yml
- name: '[Main] - Include configure tasks'
include_tasks: configure.yml

View file

@ -1,37 +1,39 @@
---
- name: "[Update] - Check installed version"
- name: '[Update] - Check installed version'
shell:
cmd: "apt-cache policy zabbix-server-mysql | grep Installed | cut -f 3 -d':' | cut -f1,2 -d'.'"
register: installed_version
register: 'installed_version'
- name: "[Update] - Make sure the server is stopped"
- name: '[Update] - Make sure the server is stopped'
systemd:
name: zabbix-server
state: stopped
name: 'zabbix-server'
state: 'stopped'
when: installed_version.stdout != zabbix_version
ignore_errors: true # needed otherwise there is an error on first installation as it doesn't exist
notify: restart zabbix_server
notify:
- 'Restart zabbix_server'
- name: "[Update] - Make sure the agent is stopped"
- name: '[Update] - Make sure the agent is stopped'
systemd:
name: zabbix-agent
state: stopped
name: 'zabbix-agent'
state: 'stopped'
when: installed_version != zabbix_version
ignore_errors: true # needed otherwise there is an error on first installation as it doesn't exist
notify: restart zabbix_agent
notify:
- 'Restart zabbix_agent'
- name: "[Update] - Make sure the proxy is stopped"
- name: '[Update] - Make sure the proxy is stopped'
systemd:
name: zabbix-proxy
state: stopped
name: 'zabbix-proxy'
state: 'stopped'
when: installed_version != zabbix_version
ignore_errors: true # needed otherwise there is an error on first installation as it doesn't exist
notify: restart zabbix_proxy
notify:
- 'Restart zabbix_proxy'
- name: '[Update] - Remove current repository'
file:
path: '/etc/apt/sources.list.d/zabbix.list'
state: 'absent'
when: installed_version != zabbix_version