Ansible离线部署 之 Zabbix

Ansible介绍

Ansible 是一个自动化平台,用于 IT 自动化,如配置管理、应用部署、任务自动化等。Ansible 使用 SSH 来连接到远程机器,并执行预定义的任务。Ansible 的主要特点是其简单性、易用性和强大的功能集。

以下是 Ansible 的一些关键特性和优点:

  1. 无需代理:Ansible 不需要在远程机器上安装任何代理或守护进程。它使用 SSH 来连接和执行任务。
  2. 易于阅读和编写:Ansible 的配置文件(通常称为 Playbooks)使用 YAML 格式,这使得它们既易于人类阅读又易于编写。
  3. 幂等性:Ansible 的任务设计为幂等的,这意味着多次运行同一个任务将产生相同的结果。
  4. 模块丰富:Ansible 有大量的内置模块,可用于各种任务,如文件操作、包管理、系统服务等。此外,用户还可以编写自定义模块。
  5. 可扩展性:Ansible 可以与多种工具和平台集成,如 Docker、Kubernetes、AWS 等。
  6. 社区支持:Ansible 有一个活跃的社区,提供了大量的文档、示例和插件。

Ansible 的主要组件包括:

  • Inventory:定义了要管理的主机和组。
  • Modules:用于执行特定任务的代码片段。
  • Playbooks:定义了如何应用模块到一组主机上的任务列表。
  • Ad-hoc Commands:一次性执行的简单命令。

使用 Ansible,您可以自动化常见的 IT 任务,如部署应用程序、配置服务器、管理网络设备等,从而提高效率并减少错误。

Zabbix介绍

Zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案。它由Zabbix server与可选组件Zabbix agent两部分构成。Zabbix server可以通过SNMP、Zabbix agent、ping、端口监视等方法提供对远程服务器/网络状态的监视和数据收集等功能。它能在多种操作系统平台上运行,如Linux、Solaris、HP-UX、AIX、Free BSD、Open BSD、OS X等。

Zabbix的主要功能包括:

  1. 实时监控:能够监控网络设备、服务器和应用程序的性能指标,提供实时监控和警报功能,帮助管理员及时发现并解决问题。
  2. 数据收集和分析:可以收集、存储和分析大量的监控数据,提供图表和报告,帮助管理员了解系统的运行状况并进行性能分析。

此外,Zabbix还具有以下特点:

  1. 自动发现服务器和网络设备。
  2. 分布式的监控体系和集中式的web管理。
  3. 支持主动监控和被动监控模式。
  4. 高效的agent支持多种操作系统和环境。
  5. 无agent监控等多种监控方法。
  6. 安全的用户认证模式和灵活的用户权限设置。
  7. 基于web的管理方法。
  8. 支持自由的自定义事件和邮件发送。
  9. 高水平的业务视图监控资源。
  10. 支持日志审计。

总的来说,Zabbix的主要功能是提供全面的网络监控和管理解决方案,帮助管理员及时发现和解决问题,提高系统的稳定性和可靠性。如需更多关于Zabbix的信息,建议访问Zabbix官方网站或相关技术社区。

文件清单

[root@localhost deploy-zabbix5.0]# tree
.
├── ansible
│   ├── ansible-2.9.27-1.el7.noarch.rpm
│   ├── libyaml-0.1.4-11.el7_0.x86_64.rpm
│   ├── python2-cryptography-1.7.2-2.el7.x86_64.rpm
│   ├── python2-httplib2-0.18.1-3.el7.noarch.rpm
│   ├── python2-jmespath-0.9.4-2.el7.noarch.rpm
│   ├── python2-pyasn1-0.1.9-7.el7.noarch.rpm
│   ├── python-babel-0.9.6-8.el7.noarch.rpm
│   ├── python-backports-1.0-8.el7.x86_64.rpm
│   ├── python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch.rpm
│   ├── python-cffi-1.6.0-5.el7.x86_64.rpm
│   ├── python-enum34-1.0.4-1.el7.noarch.rpm
│   ├── python-idna-2.4-1.el7.noarch.rpm
│   ├── python-ipaddress-1.0.16-2.el7.noarch.rpm
│   ├── python-jinja2-2.7.2-4.el7.noarch.rpm
│   ├── python-markupsafe-0.11-10.el7.x86_64.rpm
│   ├── python-paramiko-2.1.1-9.el7.noarch.rpm
│   ├── python-ply-3.4-11.el7.noarch.rpm
│   ├── python-pycparser-2.14-1.el7.noarch.rpm
│   ├── python-setuptools-0.9.8-7.el7.noarch.rpm
│   ├── python-six-1.9.0-2.el7.noarch.rpm
│   ├── PyYAML-3.10-11.el7.x86_64.rpm
│   └── sshpass-1.06-2.el7.x86_64.rpm
├── conf
│   ├── ansible.cfg
│   ├── hosts
│   ├── install_zabbix_agent.yml
│   ├── remove_zabbix-agent.yml
│   ├── simkai.ttf
│   ├── update_agent_yml_ip.sh
│   ├── zabbix-agent-5.0.33-1.el7.x86_64.rpm
│   ├── zabbix_agentd.conf
│   ├── zabbix_agentd.conf.j2
│   ├── zabbix_server.conf
│   └── zabbix.sql
├── deploy.sh
├── down
│   └── zabbix-agent-5.0.33-windows-amd64-openssl.msi
├── http
│   ├── apr-1.4.8-7.el7.x86_64.rpm
│   ├── apr-util-1.5.2-6.el7.x86_64.rpm
│   ├── httpd-2.4.6-98.el7.centos.7.x86_64.rpm
│   ├── httpd-tools-2.4.6-98.el7.centos.7.x86_64.rpm
│   └── mailcap-2.1.41-2.el7.noarch.rpm
├── mariadb
│   ├── boost-program-options-1.53.0-28.el7.x86_64.rpm
│   ├── galera-4-26.4.14-1.el7.centos.x86_64.rpm
│   ├── libpmem-1.5.1-2.1.el7.x86_64.rpm
│   ├── lsof-4.87-6.el7.x86_64.rpm
│   ├── MariaDB-client-10.11.2-1.el7.centos.x86_64.rpm
│   ├── MariaDB-common-10.11.2-1.el7.centos.x86_64.rpm
│   ├── MariaDB-compat-10.11.2-1.el7.centos.x86_64.rpm
│   ├── MariaDB-server-10.11.2-1.el7.centos.x86_64.rpm
│   ├── pcre2-10.23-2.el7.x86_64.rpm
│   ├── perl-5.16.3-299.el7_9.x86_64.rpm
│   ├── perl-Carp-1.26-244.el7.noarch.rpm
│   ├── perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64.rpm
│   ├── perl-Compress-Raw-Zlib-2.061-4.el7.x86_64.rpm
│   ├── perl-constant-1.27-2.el7.noarch.rpm
│   ├── perl-Data-Dumper-2.145-3.el7.x86_64.rpm
│   ├── perl-DBI-1.627-4.el7.x86_64.rpm
│   ├── perl-Encode-2.51-7.el7.x86_64.rpm
│   ├── perl-Exporter-5.68-3.el7.noarch.rpm
│   ├── perl-File-Path-2.09-2.el7.noarch.rpm
│   ├── perl-File-Temp-0.23.01-3.el7.noarch.rpm
│   ├── perl-Filter-1.49-3.el7.x86_64.rpm
│   ├── perl-Getopt-Long-2.40-3.el7.noarch.rpm
│   ├── perl-HTTP-Tiny-0.033-3.el7.noarch.rpm
│   ├── perl-IO-Compress-2.061-2.el7.noarch.rpm
│   ├── perl-libs-5.16.3-299.el7_9.x86_64.rpm
│   ├── perl-macros-5.16.3-299.el7_9.x86_64.rpm
│   ├── perl-Net-Daemon-0.48-5.el7.noarch.rpm
│   ├── perl-parent-0.225-244.el7.noarch.rpm
│   ├── perl-PathTools-3.40-5.el7.x86_64.rpm
│   ├── perl-PlRPC-0.2020-14.el7.noarch.rpm
│   ├── perl-Pod-Escapes-1.04-299.el7_9.noarch.rpm
│   ├── perl-podlators-2.5.1-3.el7.noarch.rpm
│   ├── perl-Pod-Perldoc-3.20-4.el7.noarch.rpm
│   ├── perl-Pod-Simple-3.28-4.el7.noarch.rpm
│   ├── perl-Pod-Usage-1.63-3.el7.noarch.rpm
│   ├── perl-Scalar-List-Utils-1.27-248.el7.x86_64.rpm
│   ├── perl-Socket-2.010-5.el7.x86_64.rpm
│   ├── perl-Storable-2.45-3.el7.x86_64.rpm
│   ├── perl-Text-ParseWords-3.29-4.el7.noarch.rpm
│   ├── perl-threads-1.87-4.el7.x86_64.rpm
│   ├── perl-threads-shared-1.43-6.el7.x86_64.rpm
│   ├── perl-Time-HiRes-1.9725-3.el7.x86_64.rpm
│   ├── perl-Time-Local-1.2300-2.el7.noarch.rpm
│   ├── pv-1.4.6-1.el7.x86_64.rpm
│   ├── rsync-3.1.2-12.el7_9.x86_64.rpm
│   └── socat-1.7.3.2-2.el7.x86_64.rpm
├── tools
│   ├── curl-7.29.0-59.el7_9.1.x86_64.rpm
│   ├── gpm-libs-1.20.7-6.el7.x86_64.rpm
│   ├── libcurl-7.29.0-59.el7_9.1.x86_64.rpm
│   ├── lrzsz-0.12.20-36.el7.x86_64.rpm
│   ├── perl-5.16.3-299.el7_9.x86_64.rpm
│   ├── perl-Carp-1.26-244.el7.noarch.rpm
│   ├── perl-constant-1.27-2.el7.noarch.rpm
│   ├── perl-Encode-2.51-7.el7.x86_64.rpm
│   ├── perl-Exporter-5.68-3.el7.noarch.rpm
│   ├── perl-File-Path-2.09-2.el7.noarch.rpm
│   ├── perl-File-Temp-0.23.01-3.el7.noarch.rpm
│   ├── perl-Filter-1.49-3.el7.x86_64.rpm
│   ├── perl-Getopt-Long-2.40-3.el7.noarch.rpm
│   ├── perl-HTTP-Tiny-0.033-3.el7.noarch.rpm
│   ├── perl-libs-5.16.3-299.el7_9.x86_64.rpm
│   ├── perl-macros-5.16.3-299.el7_9.x86_64.rpm
│   ├── perl-parent-0.225-244.el7.noarch.rpm
│   ├── perl-PathTools-3.40-5.el7.x86_64.rpm
│   ├── perl-Pod-Escapes-1.04-299.el7_9.noarch.rpm
│   ├── perl-podlators-2.5.1-3.el7.noarch.rpm
│   ├── perl-Pod-Perldoc-3.20-4.el7.noarch.rpm
│   ├── perl-Pod-Simple-3.28-4.el7.noarch.rpm
│   ├── perl-Pod-Usage-1.63-3.el7.noarch.rpm
│   ├── perl-Scalar-List-Utils-1.27-248.el7.x86_64.rpm
│   ├── perl-Socket-2.010-5.el7.x86_64.rpm
│   ├── perl-Storable-2.45-3.el7.x86_64.rpm
│   ├── perl-Text-ParseWords-3.29-4.el7.noarch.rpm
│   ├── perl-threads-1.87-4.el7.x86_64.rpm
│   ├── perl-threads-shared-1.43-6.el7.x86_64.rpm
│   ├── perl-Time-HiRes-1.9725-3.el7.x86_64.rpm
│   ├── perl-Time-Local-1.2300-2.el7.noarch.rpm
│   ├── tree-1.6.0-10.el7.x86_64.rpm
│   ├── vim-common-7.4.629-8.el7_9.x86_64.rpm
│   ├── vim-enhanced-7.4.629-8.el7_9.x86_64.rpm
│   ├── vim-filesystem-7.4.629-8.el7_9.x86_64.rpm
│   └── wget-1.14-18.el7_6.1.x86_64.rpm
└── zabbix
    ├── apr-1.4.8-7.el7.x86_64.rpm
    ├── apr-util-1.5.2-6.el7.x86_64.rpm
    ├── audit-libs-python-2.8.5-4.el7.x86_64.rpm
    ├── checkpolicy-2.5-8.el7.x86_64.rpm
    ├── dejavu-fonts-common-2.33-6.el7.noarch.rpm
    ├── dejavu-sans-fonts-2.33-6.el7.noarch.rpm
    ├── fontpackages-filesystem-1.44-8.el7.noarch.rpm
    ├── fping-3.16-1.el7.x86_64.rpm
    ├── httpd-2.4.6-98.el7.centos.7.x86_64.rpm
    ├── httpd-tools-2.4.6-98.el7.centos.7.x86_64.rpm
    ├── libcgroup-0.41-21.el7.x86_64.rpm
    ├── libevent-2.0.21-4.el7.x86_64.rpm
    ├── libjpeg-turbo-1.2.90-8.el7.x86_64.rpm
    ├── libsemanage-python-2.5-14.el7.x86_64.rpm
    ├── libtool-ltdl-2.4.2-22.el7_3.x86_64.rpm
    ├── libwebp-0.3.0-10.el7_9.x86_64.rpm
    ├── libX11-1.6.7-4.el7_9.x86_64.rpm
    ├── libX11-common-1.6.7-4.el7_9.noarch.rpm
    ├── libXau-1.0.8-2.1.el7.x86_64.rpm
    ├── libxcb-1.13-1.el7.x86_64.rpm
    ├── libXpm-3.5.12-2.el7_9.x86_64.rpm
    ├── libxslt-1.1.28-6.el7.x86_64.rpm
    ├── mailcap-2.1.41-2.el7.noarch.rpm
    ├── net-snmp-libs-5.7.2-49.el7_9.2.x86_64.rpm
    ├── OpenIPMI-2.0.27-1.el7.x86_64.rpm
    ├── OpenIPMI-libs-2.0.27-1.el7.x86_64.rpm
    ├── OpenIPMI-modalias-2.0.27-1.el7.x86_64.rpm
    ├── policycoreutils-python-2.5-34.el7.x86_64.rpm
    ├── python-IPy-0.75-6.el7.noarch.rpm
    ├── rh-php72-1-2.el7.x86_64.rpm
    ├── rh-php72-php-bcmath-7.2.24-1.el7.x86_64.rpm
    ├── rh-php72-php-cli-7.2.24-1.el7.x86_64.rpm
    ├── rh-php72-php-common-7.2.24-1.el7.x86_64.rpm
    ├── rh-php72-php-fpm-7.2.24-1.el7.x86_64.rpm
    ├── rh-php72-php-gd-7.2.24-1.el7.x86_64.rpm
    ├── rh-php72-php-json-7.2.24-1.el7.x86_64.rpm
    ├── rh-php72-php-ldap-7.2.24-1.el7.x86_64.rpm
    ├── rh-php72-php-mbstring-7.2.24-1.el7.x86_64.rpm
    ├── rh-php72-php-mysqlnd-7.2.24-1.el7.x86_64.rpm
    ├── rh-php72-php-pdo-7.2.24-1.el7.x86_64.rpm
    ├── rh-php72-php-pear-1.10.5-1.el7.noarch.rpm
    ├── rh-php72-php-process-7.2.24-1.el7.x86_64.rpm
    ├── rh-php72-php-xml-7.2.24-1.el7.x86_64.rpm
    ├── rh-php72-php-zip-7.2.24-1.el7.x86_64.rpm
    ├── rh-php72-runtime-1-2.el7.x86_64.rpm
    ├── scl-utils-20130529-19.el7.x86_64.rpm
    ├── setools-libs-3.3.8-4.el7.x86_64.rpm
    ├── unixODBC-2.3.1-14.el7.x86_64.rpm
    ├── zabbix-agent-5.0.33-1.el7.x86_64.rpm
    ├── zabbix-apache-conf-scl-5.0.33-1.el7.noarch.rpm
    ├── zabbix-get-5.0.33-1.el7.x86_64.rpm
    ├── zabbix-server-mysql-5.0.33-1.el7.x86_64.rpm
    ├── zabbix-web-5.0.33-1.el7.noarch.rpm
    ├── zabbix-web-deps-scl-5.0.33-1.el7.noarch.rpm
    └── zabbix-web-mysql-scl-5.0.33-1.el7.noarch.rpm


7 directories, 177 files

deploy.sh

#!/bin/bash
#********************************************************************
#Author:               Linux技术宅
#Date:                 2023-04-23
#FileName:Delopy zabbix5.0 for offline script
#URL:                  www.linuxjsz.top
#Copyright (C):        2023 All rights reserved
#********************************************************************


DIR=`pwd`
##color
#black="\033[30m"
#white="\033[37m"
red="\033[31m"
green="\033[32m"
yellow="\033[33m"
blue="\033[34m"
purple="\033[35m"
cyan="\033[36m"
normal="\033[0m"


# 关闭防火墙并禁用开机启动
systemctl disable firewalld --now &> /dev/null
systemctl status firewalld | grep dead &> /dev/null
if [ $? -eq 0 ];then echo -e "${blue}----------------防火墙 关闭完成.---------------${normal}"
else echo -e "${red}----------------防火墙 关闭失败.---------------${normal}"
fi
# 关闭seLinux
sed -i 's/=enforcing/=disabled/g' /etc/selinux/config
setenforce 0 &> /dev/null
echo -e "${purple}---------------SeLinux 关闭完成.---------------${normal}"


install_ansible() {
cd $DIR/ansible
rpm -ivh *.rpm --force --nodeps &> /dev/null


cp -r $DIR/conf /etc/ansible/
mv /etc/ansible/hosts /etc/ansible/conf/hosts.bak
mv /etc/ansible/ansible.cfg /etc/ansible/conf/ansible.cfg.bak
cp /etc/ansible/conf/hosts /etc/ansible/
cp /etc/ansible/conf/ansible.cfg /etc/ansible/
cp /etc/ansible/conf/*.yml /etc/ansible/


ansible --version &> /dev/null
if [ $? -eq 0 ];then echo -e "${cyan}---------------Ansible 安装完成.---------------${normal}"
else echo -e "${red}---------------Ansible 安装失败.---------------${normal}"
fi
}
install_ansible


install_http() {
cd $DIR/http
rpm -ivh *.rpm --force --nodeps &> /dev/null


cp -r $DIR/down /var/www/html/


systemctl enable httpd --now &> /dev/null


systemctl status httpd | grep running &> /dev/null
if [ $? -eq 0 ];then echo -e "${blue}----------------httpd 启动成功.----------------${normal}"
else echo -e "${red}----------------httpd 启动失败.----------------${normal}"
fi
}
install_http


install_tools() {
cd $DIR/tools
rpm -ivh *.rpm --force --nodeps &> /dev/null


echo -e "${green}-------------other tools 安装完成.-------------${normal}"
}
install_tools


install_mariadb() {
cd $DIR/mariadb
rpm -ivh *.rpm --force --nodeps &> /dev/null


# mariadb启动并配置开机启动
systemctl enable mariadb --now &> /dev/null


systemctl status mariadb | grep running &> /dev/null
if [ $? -eq 0 ];then echo -e "${purple}---------------Mariadb 启动成功.---------------${normal}"
else echo -e "${red}---------------Mariadb 启动失败.---------------${normal}"
fi


# 更新mariadb's root passwd
mysql -uroot -popenstack -e "alter user 'root'@'localhost' IDENTIFIED BY 'openstack';"
# 授权root访问权限:任何IP外部访问
mysql -uroot -popenstack -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'openstack' WITH GRANT OPTION;"
# 创建zabbix数据库用户
mysql -uroot -popenstack -e "create user zabbix@localhost identified by 'zabbix';"
# 创建zabbix数据库
mysql -uroot -popenstack -e "create database zabbix character set utf8 collate utf8_bin;"
# 更新zabbix数据库访问权限
mysql -uroot -popenstack -e "grant all privileges on zabbix.* to zabbix@localhost;"
# 配置全局log_bin_trust_function_creators
mysql -uroot -popenstack -e "set global log_bin_trust_function_creators = 0;"
# 导入初始化数据到zabbix数据库
#zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -pzabbix zabbix
mysql -uzabbix -pzabbix zabbix < /etc/ansible/conf/zabbix.sql
}
install_mariadb


install_zabbix() {
cd $DIR/zabbix
rpm -ivh *.rpm --force --nodeps &> /dev/null


# mariadb启动并配置开机启动
systemctl enable zabbix-server zabbix-agent rh-php72-php-fpm --now &> /dev/null


systemctl status rh-php72-php-fpm | grep running &> /dev/null
if [ $? -eq 0 ];then echo -e "${green}-----------rh-php72-php-fpm 启动成功.----------${normal}"
else echo -e "${red}-----------rh-php72-php-fpm 启动失败.----------${normal}"
fi
systemctl status zabbix-agent | grep running &> /dev/null
if [ $? -eq 0 ];then echo -e "${cyan}------------zabbix-agent 启动成功.-------------${normal}"
else echo -e "${red}------------zabbix-agent 启动失败.-------------${normal}"
fi
systemctl status zabbix-server | grep running &> /dev/null
if [ $? -eq 0 ];then echo -e "${yellow}------------zabbix-server 启动成功.------------${normal}"
else echo -e "${red}------------zabbix-server 启动失败.------------${normal}"
fi


# 配置zabbix页面时区
echo "php_value[date.timezone] = Asia/Shanghai" >> /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf


# 备份原来zabbix相关配置文件
mv /etc/zabbix/zabbix_agentd.conf /etc/zabbix/zabbix_agentd.conf.bak
mv /etc/zabbix/zabbix_server.conf /etc/zabbix/zabbix_server.conf.bak
mkdir /etc/zabbix/alertscripts && mkdir /etc/zabbix/externalscripts
mv /etc/ansible/conf/zabbix*.conf /etc/zabbix/


# 配置页面字体乱码
mv /etc/alternatives/zabbix-web-font /etc/alternatives/zabbix-web-font.bak
mv /etc/ansible/conf/simkai.ttf /usr/share/fonts/dejavu/
ln -s /usr/share/fonts/dejavu/simkai.ttf /etc/alternatives/zabbix-web-font
}
install_zabbix


restart_service() {
# 重启服务:zabbix-server zabbix-agent httpd rh-php72-php-fpm
systemctl restart zabbix-server zabbix-agent httpd rh-php72-php-fpm
}
restart_service

conf/update_agent_yml_ip.sh

#!/bin/bash
#********************************************************************
#Author:               Linux技术宅
#Date:                 2023-04-23
#FileName:update_yml_ip.sh
#URL:                  www.linuxjsz.top
#Copyright (C):        2023 All rights reserved
#********************************************************************


#color
red="\033[31m"
green="\033[32m"
yellow="\033[33m"
normal="\033[0m"


repo_ip=$1


#if [ ! -n "$repo_ip" ]; then
#echo "请输入Yum源的ip地址"
#exit
#fi
if [ -z "$2" ];then IP=`ifconfig|grep -Po '(?<=inet )[\d.]+'|head -1`
else IP=$2
fi


if [ -z "$1" ];then Repo_IP=`echo $IP |awk -F'.' '{print $1"."$2"."$3".254"}'`
else Repo_IP=$1
fi


echo -e "${red}Repo's IP${normal}: $Repo_IP"


Raw_repo_ip=`grep echo /etc/ansible/install_zabbix_agent.yml|awk '{print $8}'|sed 's/\"//'`
echo -e "${yellow}Raw Repo's IP${normal}: $Raw_repo_ip"


sed -ri "s/$Raw_repo_ip www/$Repo_IP www/g" /etc/ansible/install_zabbix_agent.yml


Updated_repo_ip=`grep echo /etc/ansible/install_zabbix_agent.yml|awk '{print $8}'|sed 's/\"//'`
echo -e "${green}Updated Repo's IP${normal}: $Updated_repo_ip"


echo -e ""
echo -e "${red}Current IP${normal}: $IP"


Raw_zabbix_server_ip=`grep zabbix_server_ip /etc/ansible/install_zabbix_agent.yml | awk '{print $2}'`
echo -e "${yellow}Raw Zabbix Server IP${normal}: $Raw_zabbix_server_ip"


sed -ri "s/: $Raw_zabbix_server_ip/: $IP/g" /etc/ansible/install_zabbix_agent.yml


Updated_zabbix_server_ip=`grep zabbix_server_ip /etc/ansible/install_zabbix_agent.yml | awk '{print $2}'`
echo -e "${green}Updated Zabbix Server IP${normal}: $Updated_zabbix_server_ip"

conf/install_zabbix_agent.yml

---
- name: install zabbix-agent conf
  hosts: all
  vars:
    zabbix_server_ip: 10.14.115.254
    zabbix_agent_ip: "{{ ansible_default_ipv4.address }}"
  gather_facts: true
  ignore_errors: yes
  tasks:
    - name: copy zabbix-agent rpm
      copy: src=/etc/ansible/conf/zabbix-agent-5.0.33-1.el7.x86_64.rpm dest=/usr/local/src/ owner=root group=root mode=0644
    - name: rpm -ivh zabbix-agent
      shell: rpm -ivh /usr/local/src/zabbix-agent-5.0.33-1.el7.x86_64.rpm
    - name: yum install zabbix-agent
      yum: name=zabbix-agent state=present
    - name: Copy zabbix-agent configuration file
      template: src=/etc/ansible/conf/zabbix_agentd.conf.j2 dest=/etc/zabbix/zabbix_agentd.conf
    - name: copy port_discovery.sh
      copy: src=/etc/zabbix/alertscripts/port_discovery.sh dest=/etc/zabbix/zabbix_agentd.d/ owner=root group=root mode=0755
    - name: +x port_discovery.sh
      shell: chmod +x /etc/zabbix/zabbix_agentd.d/port_discovery.sh
    - name: Start zabbix-agent
      service: name=zabbix-agent state=started enabled=true
    - name: add repo's dns to hosts
      shell: sed -i '/centos-repo/'d /etc/hosts && echo "10.14.115.249 www.centos-repo.cecgw.cn" >> /etc/hosts
    - name: sync Yum repo
      shell: curl http://www.centos-repo.cecgw.cn/scripts/mkrepo.sh|bash

conf/remove_zabbix-agent.yml

---
- name: install zabbix-agent conf
  hosts: all
  tasks:
    - name: stop zabbix-agent
      shell: systemctl disable zabbix-agent --now
    - name: remove zabbix-agent
      shell: rpm -e zabbix-agent
    - name: del zabbix-agent rpm
      shell: rm -f /usr/local/src/zabbix-agent-5.0.33-1.el7.x86_64.rpm
    - name:  del zabbix-agent configuration file
      shell: rm -rf /etc/zabbix
    - name: sync Yum repo
      shell: curl http://www.centos-repo.cecgw.cn/scripts/remkrepo.sh|bash

conf/zabbix_agentd.conf.j2

PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server={{ zabbix_server_ip }}
ServerActive={{ zabbix_server_ip }}
Hostname={{ zabbix_agent_ip }}
Timeout=30
UnsafeUserParameters=1
HostMetadataItem=system.uname
Include=/etc/zabbix/zabbix_agentd.d/*.conf
UserParameter=check_port,/etc/zabbix/zabbix_agentd.d/port_discovery.sh

conf/zabbix_agentd.conf

LogFile=/var/log/zabbix/zabbix_agent.log
PidFile=/var/run/zabbix/zabbix_agentd.pid
Server=127.0.0.1
ServerActive=127.0.0.1
Hostname=127.0.0.1
Timeout=30
UnsafeUserParameters=1
HostMetadataItem=system.uname
UserParameter=check_port,/etc/zabbix/alertscripts/port_discovery.sh

conf/zabbix_server.conf

LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/var/run/zabbix/zabbix_server.pid
DBSocket=/var/lib/mysql/mysql.sock
DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=zabbix
StartPollers=10
StartTrappers=10
StartPingers=10
StartDiscoverers=10
MaxHousekeeperDelete=5000
CacheSize=256M
StartDBSyncers=40
HistoryCacheSize=128M
TrendCacheSize=128M
Timeout=30
AlertScriptsPath=/etc/zabbix/alertscripts
ExternalScripts=/etc/zabbix/externalscripts
FpingLocation=/usr/sbin/fping
LogSlowQueries=1000

/etc/zabbix/alertscripts/port_discovery.sh

#!/bin/bash
#因为netstat -p需要管理员权限,脚本是zabbix用户调用的,所以去掉了,但是效果是一样的
portarray=(`netstat -tnl|awk {'print $4'}|awk -F':' '{if ($NF~/^[0-9]*$/) print $NF}'|sort|uniq|grep -v 22|grep -v 111|grep -v 25|grep -v 601*|grep -v 100*|grep -v 59*`)
length=${#portarray[@]}
printf "{\n"
printf '\t'"\"data\":["
for ((i=0;i<$length;i++))
do
        printf '\n\t\t{'
        printf "\"{#TCP_PORT}\":\"${portarray[$i]}\"}"
        if [ $i -lt $[$length-1] ];then
                printf ','
        fi
done
printf "\n\t]\n"
printf "}\n"

check_port模板

在包根目录。

update_agent.yml

---
- name: update agent conf
  hosts: all
  tasks:
    - name: copy port_discovery.sh
      copy: src=/etc/zabbix/alertscripts/port_discover.sh dest=/etc/zabbix/zabbix_agentd.d/ owner=root group=root mode=0755
    - name: echo config
      shell: echo "UserParameter=check_port,/etc/zabbix/zabbix_agentd.d/port_discover.sh" >> /etc/zabbix/zabbix_agentd.conf
    - name: Restart zabbix-agent
      shell: systemctl restart zabbix-agent

Linux-agent安装

  • 更新yum repo IP,zabbix-server IP

    cd /etc/ansible
    ./conf/update_agent_yml_ip.sh $1 $2

    $1 内网yum环境IP,不填写,默认为同网段.254

    $2 zabbix-server IP, 不填写,默认为当前IP

  • 修改/etc/ansible/hosts文件,填写IP

    vim /etc/ansible/hosts

  • 执行安装

    ansible-playbook /etc/ansible/install_zabbix_agent.yml

Windows-agent安装,启用自动注册

  • 安装完成后,修改zabbix_agentd.conf文件,默认安装路径:C:\Program Files\Zabbix Agent

    修改或添加如下参数,默认未启用

    HostMetadataItem=

    HostMetadataItem=system.uname

  • 打开任务管理器→服务→重启zabbix-agent服务即可

文件清单压缩包

请查看原文,微信公众号回复"zabbix5.0"获取。

来自: Ansible离线部署 之 Zabbixhttps://mp.weixin.qq.com/s?__biz=Mzk0NTQ3OTk3MQ==&mid=2247487434&idx=1&sn=3128800a0219c5ebc5a3f89d2c8ccf50&chksm=c3158786f4620e90afe440bb32fe68541191cebbabc2d2ef196f7300e84cde1e1b57383c521a&token=113329682&lang=zh_CN#rd

相关推荐
vvw&39 分钟前
如何在 Ubuntu 22.04 上安装 Ansible 教程
linux·运维·服务器·ubuntu·开源·ansible·devops
☆凡尘清心☆1 天前
CentOS-stream-9安装ansible
linux·centos·ansible
墨水\\1 天前
Ansible---playbook剧本
ansible
墨水\\2 天前
Ansible部署及基础模块
服务器·网络·ansible
七七powerful2 天前
ansible play-book玩法
linux·服务器·ansible
Karoku0664 天前
【自动化部署】Ansible循环
linux·运维·数据库·docker·容器·自动化·ansible
W u 小杰4 天前
Ansible基本用法
服务器·网络·ansible
运维小文4 天前
ansible剧本快速上手
linux·运维·python·自动化·ansible·幂等性·剧本
记得多喝水o4 天前
Ansible自动化运维 技术与最佳实践
运维·自动化·ansible
Karoku0664 天前
【自动化部署】Ansible Playbook 基础应用
运维·网络·docker·容器·自动化·ansible·consul