Ansible自动化部署Zabbix监控系统
1. 规划节点
节点规划,见表1。
表1 节点规划
| IP | 主机名 | 节点 |
|---|---|---|
| 192.168.100.3 | ansible | Ansible |
| 192.168.100.4 | zabbix_server | zabbix_server、zabbix_agent |
| 192.168.100.5 | zabbix_agent | zabbix_agent |
2. 基础准备
将提供的软件包 Autodeployment.tar.gz 和 zabbix-7.0.11.tar.gz 上传至 ansible 节点 /root 目录下。
3. Ansible简介
Ansible 是由 Red Hat 主导维护的 开源无代理自动化运维工具,基于Python开发,通过SSH协议实现跨节点批量操作,核心覆盖配置管理、应用部署、任务编排,是DevOps落地与运维流程标准化的核心组件,无需目标节点安装客户端,简单易用且适配多环境。
(1)核心定位
- 自动化运维中枢:无代理架构统一调度多节点,解决传统运维重复劳动、配置漂移问题;
- 全场景适配:覆盖基础设施配置、应用部署、故障修复,兼容物理机、容器、云平台。
(2)核心功能
- 无代理批量操作:通过Inventory管理节点,一键执行命令、部署软件、修改配置;
- 剧本与模块生态:YAML剧本定义自动化流程,3000+内置模块覆盖全运维场景;
- 配置管理:声明式定义目标状态,自动检测并修复配置漂移;
- 任务编排:支持依赖控制、并行执行,适配集群搭建、版本升级等复杂流程。
(3)核心优势
- 简单易用:无代理架构降低部署成本,YAML语法简洁,运维人员快速上手;
- 高度兼容:支持主流OS、云平台、容器,适配异构IT环境;
- 模块化扩展:支持自定义模块与插件,可与Zabbix、Jenkins等工具无缝集成;
- DevOps友好:与CI/CD工具深度联动,实现运维流程代码化与自动化闭环。
(4)适用场景
- 中小企业基础运维自动化,减少重复劳动;
- 大型企业多环境/跨云平台编排,标准化运维流程;
- 云原生与容器集群管理,适配Kubernetes资源编排;
- DevOps流程落地,实现持续集成/持续部署自动化;
- 故障自动修复,与监控工具联动触发自动化运维任务。
4. Zabbix简介
Zabbix 是由 拉脱维亚 Zabbix SIA 公司开源的 企业级分布式IT监控系统,基于C语言和PHP开发,聚焦全栈IT基础设施可观测性,覆盖硬件、云资源、容器、应用等全层级监控对象,是运维故障预警、性能优化的核心工具,通过分布式架构适配从小型机房到混合云的各类监控需求。
(1)核心定位
- 全栈监控中枢:统一管理多层级IT资源,解决传统监控碎片化、数据分散问题;
- 分布式适配:支持单机/集群(Server+Proxy+Agent)部署,实现集中管理、分布式采集。
(2)核心功能
- 全场景监控:覆盖服务器、网络设备、云平台、容器、数据库、中间件等;
- 灵活采集与告警:支持Agent/无Agent采集,多渠道告警推送与智能阈值策略;
- 可视化与报表:自定义仪表盘、拓扑图,自动生成性能趋势与告警统计报表;
- 扩展能力:支持自定义监控项、API二次开发,适配个性化需求。
(3)核心优势
- 开源免费,无license成本,企业版提供高级支持;
- 高兼容性,适配主流OS、硬件、云平台与应用组件;
- 轻量低耗,Agent占用资源少,运维成本低;
- 灵活扩展,模块化架构支持大规模监控场景。
(4)适用场景
- 中小企业基础运维监控,快速搭建全栈监控平台;
- 大型企业混合云/跨地域监控,通过分布式架构统一管控;
- 关键业务保障,实时告警与异常检测规避故障风险;
- 云原生环境监控,适配Kubernetes与微服务架构。
案例实施
1. 基础环境准备
(1)安装ansible
修改 ansible 节点主机名为 ansible
[root@localhost ~]# hostnamectl set-hostname ansible
[root@localhost ~]# bash
Welcome to 6.6.0-28.0.0.34.oe2403.x86_64
System information as of time: Fri Nov 21 07:14:24 PM CST 2025
System load: 1.00
Memory used: 4.2%
Swap used: 0%
Usage On: 3%
IP address: 192.168.100.3
Users online: 2
[root@ansible ~]#
使用提供的软件包 Autodeployment.tar.gz 在 ansible 节点配置本地 yum 源 local.repo
# 删除网络源
[root@ansible ~]# rm -rf /etc/yum.repos.d/openEuler.repo
# 永久挂载系统源
[root@ansible ~]# vi /etc/fstab
...
# 添加以下内容
/dev/sr0 /opt/openEuler iso9660 defaults 0 0
# 生效挂载
[root@ansible ~]# mount -a
# 配置系统源
[root@ansible ~]# vi /etc/yum.repos.d/local.repo
[openEuler]
name=openEuler
baseurl=file:///opt/openEuler
gpgcheck=0
# 安装依赖工具
[root@ansible ~]# yum install -y vim tarl
配置 ansible 源并安装 ansible 工具
# 配置 ansible 源
[root@ansible ~]# tar -zxvf Autodeployment.tar.gz -C /opt/
...
[root@ansible ~]# vim /etc/yum.repos.d/local.repo
[openEuler]
name=openEuler
baseurl=file:///opt/openEuler
gpgcheck=0
# 临时关闭
enabled=0
[Autodeployment]
name=Autodeployment
baseurl=file:///opt/Autodeployment
gpgcheck=0
# 安装 ansible 工具
[root@ansible ~]# yum install -y ansible
[root@ansible ~]# ansible --version
ansible 2.9.27
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.11/site-packages/ansible
executable location = /usr/bin/ansible
python version = 3.11.6 (main, May 23 2024, 21:04:46) [GCC 12.3.1 (openEuler 12.3.1-30.oe2403)]
(2)配置免密登录
在 ansible 节点配置主机映射将 IP 地址映射为主机名,三台主机名分别为 ansible、zabbix_server、zabbix_agent
[root@ansible ~]# vim /etc/hosts
...
192.168.100.3 ansible
192.168.100.4 zabbix_server
192.168.100.5 zabbix_agent
在所有节点生成 SSH 密钥对,在 ansible 节点配置对其他节点主机名的 SSH 免密通信
[root@ansible ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): # 回车
Enter passphrase (empty for no passphrase): # 回车
Enter same passphrase again: # 回车
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:oPxuyRq2Hj+c8hWfGilDTaQEsOSJ5o+5uNRmkDhevYk root@ansible
The key's randomart image is:
+---[RSA 3072]----+
| o.... . |
| + o . o |
|..+ o . |
|+ .... + |
|o+ .o.. S |
|..B .oo + . |
| = E+=+o+ o |
|o +..*B+ o |
|oo .+=+.. |
+----[SHA256]-----+
[root@ansible ~]# ssh-copy-id ansible
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'ansible (192.168.100.3)' can't be established.
ED25519 key fingerprint is SHA256:Wlnpa1YpGmCRidCSpc5QWFLh7/0+w0gbsFDF1YvqWtw.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes # 输入 yes 回车
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
Authorized users only. All activities may be monitored and reported.
root@ansible's password: # 输入 root 密码回车
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'ansible'"
and check to make sure that only the key(s) you wanted were added.
[root@ansible ~]# ssh-copy-id zabbix_server
[root@ansible ~]# ssh-copy-id zabbix_agent
(3)配置 ansible 主机清单
在 ansible 节点创建 /root/zabbix/ 作为工作目录,在 zabbix 目录下编写 ansible.cfg 配置文件和 inventory 主机清单,在清单中分别创建 server 主机组和 agent 主机组,server 主机组主机为 zabbix_server 的主机名,agent 主机组主机为 zabbix_server 和 zabbix_agent 的主机名,添加参数 hostname=各自的主机名
[root@ansible ~]# mkdir -p /root/zabbix
[root@ansible ~]# cd /root/zabbix
[root@ansible ~]# vim /root/zabbix/ansible.cfg
[defaults]
# 关联主机清单路径
inventory = /root/zabbix/inventory
# 远程连接默认用户(运维常用root)
remote_user = root
# 禁用主机密钥检查,避免手动交互
host_key_checking = False
# 禁用重试文件,减少冗余
retry_files_enabled = False
timeout = 30
[root@ansible ~]# vim /root/zabbix/inventory
# server主机组:仅含 zabbix_server,指定 hostname 参数
[server]
zabbix_server hostname=zabbix_server
# agent主机组:含 zabbix_server 和 zabbix_agent,分别指定 hostname 参数
[agent]
zabbix_server hostname=zabbix_server
zabbix_agent hostname=zabbix_agent
2. 使用 ansible 完成系统基础设置
(1)安装 FTP 服务
在 ansible 节点配置 ftp 服务
[root@ansible zabbix]# yum install -y vsftpd
[root@ansible zabbix]# vim /etc/vsftpd/vsftpd.conf
...
# 添加 FTP 服务根目录(匿名用户访问的根路径,对应 yum 源存储目录 /opt)
anon_root=/opt
# 修改是否允许匿名 FTP 访问(必须开启,yum 源匿名可访问)
anonymous_enable=YES
[root@ansible zabbix]# systemctl restart vsftpd
[root@ansible zabbix]# systemctl enable vsftpd
(2)关闭防火墙和Selinux
[root@ansible zabbix]# systemctl disable firewalld --now
[root@ansible zabbix]# setenforce 0
[root@ansible zabbix]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
(3)编写 ansible 文件
在工作目录下创建 files 文件夹,在 files 目录下为 zabbix_server 节点和 zabbix_agent 节点编写 ftp.repo 文件,使用 ansible 节点的 ftp 提供 yum 源(注意不要出现 ip)
[root@ansible zabbix]# mkdir files
[root@ansible zabbix]# vim files/ftp.repo
[openEuler]
name=openEuler
baseurl=ftp://ansible/openEuler
gpgcheck=0
[ansible]
name=ansible
baseurl=ftp://ansible/Autodeployment
gpgcheck=0
在工作目录下创建 system.yaml 文件,文件实现的功能要求如下:
① 修改 zabbix_server 节点和 zabbix_agent 节点主机名
② 将 ansible 节点的 hosts 主机映射文件发送给其它节点
③ 删除 zabbix_server 节点和 zabbix_agent 节点自带的 repo 文件
④ 设置 zabbix_server 节点和 zabbix_agent 节点 yum.repos.d 目录的权限为 755
⑤ 把 ftp.repo 文件同时发送给 zabbix_server 节点和 zabbix_agent 节点 yum 目录下
⑥ 在 zabbix_server 节点和 zabbix_agent 节点安装 vim,bash-completion,tree,jq,tar
[root@ansible zabbix]# vim system.yaml
- name: config
hosts: agent
become: yes
tasks:
- name: hostname
hostname:
name: "{{ inventory_hostname }}"
- name: copy hosts
copy:
src: /etc/hosts
dest: /etc/hosts
- name: delete repo
file:
path: /etc/yum.repos.d/openEuler.repo
state: absent
- name: chmod yum.repos.d
file:
path: /etc/yum.repos.d/
state: directory
mode: '0755'
- name: copy ftp.repo
copy:
src: /root/zabbix/files/ftp.repo
dest: /etc/yum.repos.d/
- name: yum install
yum:
name: vim,bash-completion,tree,jq,tar
state: present
- name: disable firewalld
systemd:
name: firewalld
state: stopped
enabled: no
- name: disable selinux
selinux:
state: disabled
##############################
# - name: seteforce 0
# command: setenforce 0
# when: ansible_selinux.mode == 'enforcing'
# - name: disable selinux
# lineinfile:
# path: /etc/selinux/config
# state: present
# regexp: '^SELINUX='
# line: 'SELINUX=disabled'
##############################
查看 ansible_selinux 字段
[root@ansible zabbix]# ansible zabbix_server -m setup -a "filter=ansible_selinux"
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
zabbix_server | SUCCESS => {
"ansible_facts": {
"ansible_selinux": {
# 注:来自远程主机的 /etc/selinux/config 文件中 SELINUX= 的值(持久配置)
# 表示“重启后系统将以 enforcing 模式启动”。注意:这是文件中的配置,不一定等同于当前运行态
"config_mode": "enforcing",
# 注:当前内核运行时的 SELinux 模式(等价于 getenforce 的输出)
"mode": "enforcing",
# 注:SELinux policy 的版本号(policy 编译版本),一般用于兼容性判断,对日常操作影响小
"policyvers": 33,
# 注:表示内核层面 SELinux 功能被启用(即 SELinux 已加载到内核)
# status = enabled 并不说明是 enforcing 还是 permissive,那由 mode 指定
"status": "enabled",
# 注:使用的 SELinux 策略类型,targeted 表示只对关键服务/守护进程应用策略(常用默认策略)
"type": "targeted"
},
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false
}
[root@ansible zabbix]# ansible zabbix_server -m setup -a "filter=ansible_selinux"
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
zabbix_server | SUCCESS => {
"ansible_facts": {
"ansible_selinux": {
# 注:来自远程主机的 /etc/selinux/config 文件中 SELINUX= 的值(持久配置)
# 表示“系统下一次重启后将以 disabled 模式启动”,即永久禁用 SELinux
# 注意:修改 config_mode 不会立刻影响当前运行态,必须重启后才彻底生效
"config_mode": "disabled",
# 注:当前内核运行时的 SELinux 模式(等价于 getenforce 的输出)
# 这里为 permissive,表示 SELinux 不拦截操作,只记录日志(警告模式)
# 运行态变为 permissive 通常来自 setenforce 0 或 Ansible selinux 模块的运行时设置
"mode": "permissive",
# 注:SELinux policy 的版本号(policy 编译版本),系统策略版本未变化,因此仍为 33
# 与是否启用 SELinux 无关,仅表示策略文件的版本
"policyvers": 33,
# 注:表示内核层面 SELinux 功能仍然被启用(即 SELinux 模块仍加载到内核中)
# 即使 config_mode 已经变为 disabled,只要未重启,status 仍然是 enabled
# 只有重启后 SELINUX=disabled 才会让内核以完全禁用模式启动
"status": "enabled",
# 注:SELinux 策略类型,仍为 targeted,表示只对关键服务/守护进程应用策略
# 由于策略类型来自已加载的 policy,运行态未重启的情况下不会改变
"type": "targeted"
},
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false
}
(4)执行 ansible 文件
执行 ansible 自动化文件报错如下
[root@ansible zabbix]# ansible-playbook system.yaml
...
fatal: [zabbix_agent]: FAILED! => {"changed": false, "checksum": "db0b109aab82ff4fbe12a53763df3b7d5950d45b", "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"}
...
方法一:zabbix_server 和 zabbix_agent 安装 python3-libselinux
# zabbix_server 和 zabbix_agent 都需要修改一下文件
[root@zabbix_server ~]# vi /etc/yum.repos.d/openEuler.repo
[openEuler]
name=openEuler
baseurl=ftp://192.168.100.3/openEuler
gpgcheck=0
[ansible]
name=ansible
baseurl=ftp://192.168.100.3/Autodeployment
gpgcheck=0
[root@zabbix_agent ~]# vi /etc/yum.repos.d/ftp.repo
[openEuler]
name=openEuler
baseurl=ftp://192.168.100.3/openEuler
gpgcheck=0
[ansible]
name=ansible
baseurl=ftp://192.168.100.3/Autodeployment
gpgcheck=0
# zabbix_server 和 zabbix_agent 都需要安装 python3-libselinux
[root@zabbix_server ~]# yum install -y python3-libselinux
[root@zabbix_agent ~]# yum install -y python3-libselinux
方法二:zabbix_server 和 zabbix_agent 关闭防火墙和Selinux
# zabbix_server节点
[root@zabbix_server ~]# systemctl disable firewalld --now
[root@zabbix_server ~]# setenforce 0
[root@zabbix_server ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
[root@zabbix_server ~]# reboot
# zabbix_agent节点
[root@zabbix_agent ~]# systemctl disable firewalld --now
[root@zabbix_agent ~]# setenforce 0
[root@zabbix_agent ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
[root@zabbix_agent ~]# reboot
重新执行 ansible 自动化文件
[root@ansible zabbix]# ansible-playbook system.yaml
...
3. 使用 ansible 配置时间同步
(1)编写 ansible 文件
在 ansible 节点工作目录下创建 install_chrony.yaml 文件,文件实现的功能要求如下:
① 在 zabbix_server 节点和 zabbix_agent 节点使用 yum 安装 chrony 时间同步服务
② 修改 zabbix_server 节点 chrony 配置文件,将时间同步服务器改为 ntp.aliyun.com(使用 lineinfile 模块)
③ 修改 zabbix_agent 节点 chrony 配置文件,将时间同步服务器改为 zabbix_server(使用 lineinfile 模块)
④ 修改 zabbix_server 节点和 zabbix_agent 节点时区为 Asia/Shanghai
⑤ 重启 chrony 服务并设为开机自启动
[root@ansible zabbix]# vim install_chrony.yaml
- name: config chrony
hosts: agent
become: yes
tasks:
- name: yum install chrony
yum:
name: chrony
state: present
- name: set server ntp
lineinfile:
path: /etc/chrony.conf
state: present
regexp: '^pool pool.ntp.org iburst'
line: 'server ntp.aliyun.com iburst'
backrefs: yes
when: inventory_hostname == 'zabbix_server'
- name: set server ntp
lineinfile:
path: /etc/chrony.conf
state: present
regexp: '^#allow 192.168.0.0/16'
line: 'allow 192.168.0.0/16'
backrefs: yes
when: inventory_hostname == 'zabbix_server'
- name: set server ntp
lineinfile:
path: /etc/chrony.conf
state: present
regexp: '^#local stratum 10'
line: 'local stratum 10'
backrefs: yes
when: inventory_hostname == 'zabbix_server'
- name: set agent ntp
lineinfile:
path: /etc/chrony.conf
state: present
regexp: '^pool pool.ntp.org iburst'
line: 'server zabbix_server iburst'
backrefs: yes
when: inventory_hostname == 'zabbix_agent'
- name: timezone
timezone:
name: Asia/Shanghai
- name: restart chronyd
systemd:
name: chronyd
state: restarted
enabled: yes
(2)执行 ansible 文件
[root@ansible zabbix]# ansible-playbook install_chrony.yaml
...
4. 使用 ansible 编译安装 zabbix 服务
(1)编写 ansible 文件
在 ansible 节点将提供的 zabbix-7.0.11.tar.gz 复制到工作目录 files 文件夹下,在工作目录下创建 install_zabbix.yaml 文件并执行,文件实现的功能要求如下:
① 在 zabbix_server 节点和 zabbix_agent 节点创建 zabbix 用户和 zabbix 组
② 将 zabbix-7.0.11.tar.gz 发送给 zabbix_server 节点和 zabbix_agent 节点 /root 目录下并解压
③ 在 zabbix_server 节点和 zabbix_agent 节点使用 yum 安装必备组件 gcc,mysql-devel,libevent-devel,pcre-devel
④ 在 zabbix_server 节点编译安装 zabbix 的 server 端和 agent 端,数据库选择 mysql,安装位置为 /opt/zabbix
⑤ 在 zabbix_agent 节点编译安装 zabbix 的 agent 端,安装位置为 /opt/zabbix
[root@ansible zabbix]# cp /root/zabbix-7.0.11.tar.gz files/
[root@ansible zabbix]# vim install_zabbix.yaml
- name: config zabbix
hosts: agent
become: yes
tasks:
- name: create zabbix group
group:
name: zabbix
state: present
system: yes
- name: create zabbix user
user:
name: zabbix
state: present
group: zabbix
home: /opt/zabbix
python: /sbin/nologin
system: yes
- name: copy zabbix
copy:
src: /root/zabbix/files/zabbix-7.0.11.tar.gz
dest: /root/
- name: tar xf zabbix
unarchive:
src: /root/zabbix-7.0.11.tar.gz
dest: /root/
remote_src: yes
- name: install depend
yum:
name: gcc,mysql-devel,libevent-devel,pcre-devel
state: present
- name: configure zabbix server
command: >
bash -c "./configure --prefix=/opt/zabbix --enable-server --enable-agent --with-mysql && make -j4 && make install"
args:
chdir: /root/zabbix-7.0.11
when: inventory_hostname == 'zabbix_server'
- name: configure zabbix agent
command: >
bash -c "./configure --prefix=/opt/zabbix --enable-agent --with-mysql && make -j4 && make install"
args:
chdir: /root/zabbix-7.0.11
(2)执行 ansible 文件
[root@ansible zabbix]# ansible-playbook install_zabbix.yaml
...
5. 使用 ansible 安装 php 服务
(1)编写 ansible 文件
在 ansible 节点工作目录下创建 install_php.yaml 文件并执行,文件实现的功能要求如下:
① 在 zabbix_server 节点安装 php 服务,apache 会自动安装(另外需安装依赖 php-gd,php-mysqlnd,php-bcmath,php-xml,php-mbstring,php-fpm,httpd,php-cli)
② 将 zabbix 的前端网页文件复制到 apache 的网站根目录下
③ 使用 lineinfile 模块修改 php 配置文件,要求最大 POST 数据限制为 16M,程序执行时间限制为 300,PHP 页面接受数据所需最大时间限制为 300,把时区设为 Asia/Shanghai
④ 重启 apache 和 php 服务,并设置为开机自启动
[root@ansible zabbix]# vim install_php.yaml
- name: config php
hosts: server
become: yes
tasks:
- name: install depend
yum:
name: php-gd,php-mysqlnd,php-bcmath,php-xml,php-mbstring,php-fpm,httpd,php-cli
state: present
- name: copy zabbix ui
copy:
src: /root/zabbix-7.0.11/ui/
dest: /var/www/html/
remote_src: yes
owner: apache
group: apache
mode: '0755'
- name: post_max_size
lineinfile:
path: /etc/php.ini
state: present
regexp: '^post_max_size'
line: 'post_max_size=16M'
backrefs: yes
- name: max_input_time
lineinfile:
path: /etc/php.ini
state: present
regexp: '^max_input_time'
line: 'max_input_time=300'
backrefs: yes
- name: max_execution_time
lineinfile:
path: /etc/php.ini
state: present
regexp: '^max_execution_time'
line: 'max_execution_time=300'
backrefs: yes
- name: set timezone Asia/Shanghai
lineinfile:
path: /etc/php.ini
regexp: '^;?date.timezone'
line: 'date.timezone = Asia/Shanghai'
state: present
backrefs: yes
- name: restart httpd
systemd:
name: httpd
state: restarted
enabled: yes
- name: restart php-fpm
systemd:
name: php-fpm
state: restarted
enabled: yes
分别设置目录和文件
[root@ansible zabbix]# vim install_php.yaml
...
- name: copy zabbix ui httpd
copy:
src: /usr/local/src/zabbix-7.0.11/ui/
dest: /var/www/html
owner: apache
group: apache
mode: '0755'
remote_src: yes
# ---- 新增:将所有目录设置为 0755,所有文件设置为 0644,并确保属主属组 ----
- name: find all directories under /var/www/html
find:
paths: /var/www/html
file_type: directory
register: web_dirs
- name: set directory permissions to 0755 and owner apache
file:
path: "{{ item.path }}"
owner: apache
group: apache
mode: '0755'
loop: "{{ web_dirs.files }}"
- name: find all files under /var/www/html
find:
paths: /var/www/html
file_type: file
register: web_files
- name: set file permissions to 0644 and owner apache
file:
path: "{{ item.path }}"
owner: apache
group: apache
mode: '0644'
loop: "{{ web_files.files }}"
########################################
- name: chmod -R 755 for directories
command: find /var/www/html -type d -exec chmod 755 {} \;
- name: chmod -R 644 for files
command: find /var/www/html -type f -exec chmod 644 {} \;
(2)执行 ansible 文件
[root@ansible zabbix]# ansible-playbook install_php.yaml
...
6. 使用 ansible 安装数据库
(1)编写 ansible 文件
在 ansible 节点工作目录下创建 install_mysql.yaml 文件并执行,文件实现的功能要求如下:
① 在 zabbix_server 节点安装 mysql 服务
② 重启数据库服务并设置为开机自启动
③ 设置 mysql 登录密码,密码为 000000,默认登录账号为 root
④ 创建 mysql 用户 zabbix,密码为 000000,创建数据库 zabbix,并授权 zabbix 用户拥有 zabbix 数据库的所有权限
分别导入 zabbix 数据库架构及数据,对应的文件分别为 schema.sql、images.sql 和 data.sql(文件顺便不能乱)
[root@ansible zabbix]# tar -zxvf /root/zabbix-7.0.11.tar.gz -C /usr/local/src/
[root@ansible zabbix]# cp /usr/local/src/zabbix-7.0.11/database/mysql/*.sql files/
[root@ansible zabbix]# vim install_mysql.yaml
- name: config mysql
hosts: server
become: yes
tasks:
- name: install mysql
yum:
name: mysql-server,python3-PyMySQL
state: present
- name: restart mysql
systemd:
name: mysqld
state: restarted
enabled: yes
- name: set mysql password
mysql_user:
name: root
state: present
password: '000000'
host: localhost
priv: '*.*:ALL,GRANT'
login_unix_socket: /var/lib/mysql/mysql.sock
- name: delete zabbix database
mysql_db:
name: zabbix
state: absent
login_user: root
login_password: '000000'
login_unix_socket: /var/lib/mysql/mysql.sock
- name: create zabbix database
mysql_db:
name: zabbix
state: present
encoding: utf8
collation: utf8_bin
login_user: root
login_password: '000000'
login_unix_socket: /var/lib/mysql/mysql.sock
- name: create mysql user zabbix localhost
mysql_user:
name: zabbix
state: present
password: '000000'
host: localhost
priv: 'zabbix.*:ALL'
login_user: root
login_password: '000000'
login_unix_socket: /var/lib/mysql/mysql.sock
- name: create mysql user zabbix %
mysql_user:
name: zabbix
state: present
password: '000000'
host: '%'
priv: 'zabbix.*:ALL'
login_user: root
login_password: '000000'
login_unix_socket: /var/lib/mysql/mysql.sock
- name: copy mysql db sql
copy:
src: "/root/zabbix/files/{{ item }}"
dest: "/root/{{ item }}"
loop:
- schema.sql
- images.sql
- data.sql
- name: import zabbix sql
mysql_db:
name: zabbix
state: import
target: "/root/{{ item }}"
login_user: root
login_password: '000000'
login_unix_socket: /var/lib/mysql/mysql.sock
loop:
- schema.sql
- images.sql
- data.sql
(2)执行 ansible 文件
[root@ansible zabbix]# ansible-playbook install_mysql.yaml
...
7. 网页安装 zabbix
(1)浏览器访问
在浏览器访问 zabbix_server 节点的 ip 地址进入 zabbix 安装页面,填入 mysql信息、zabbix 主机名称为 zabbix_server,确认无报错后完成安装,如有报错自行解决,默认登录账号 Admin,密码 zabbix,可通过红色框设置文语言

(2)检查必要条件

(3)数据库连接
密码:000000

(4)设置Zabbix主机名称

(5)安装前汇总

(6)安装

(7)用户名/密码登录
用户名/密码:Admin/zabbix

查看首页

8. 使用 ansible 配置 zabbix_server
(1)配置 zabbix_server 文件
在 ansible 节点工作目录 files 文件夹下编写 zabbix_server 的配置文件 zabbix_server.conf
[root@ansible zabbix]# vim files/zabbix_server.conf
LogFile=/var/log/zabbix/zabbix_server.log
PidFile=/opt/zabbix/zabbix_server.pid
DBName=zabbix
DBUser=zabbix
DBPassword=000000
DBHost=localhost
Timeout=4
LogSlowQueries=3000
StatsAllowedIP=127.0.0.1
EnableGlobalScripts=0
(2)编写 ansible 文件
要求修改日志存放路径为 /var/log/zabbix,pid 文件存放在安装目录下,使用 zabbix 数据库,数据库用户 zabbix,数据库密码 000000,其余参数默认即可;在工作目录下新建 zabbix_server.yaml 文件并执行,文件实现的功能要求如下:
① 在 zabbix_server 节点创建 /var/log/zabbix 作为 zabbix 的日志存放路径
② 在 zabbix_server 节点给日志路径和 zabbix 安装路径的用户和组权限都设为 zabbix
③ 把 zabbix_server.conf 文件发送给 zabbix_server 节点的 zabbix 配置目录下
[root@ansible zabbix]# vim zabbix_server.yaml
- name: config zabbix server
hosts: server
become: yes
tasks:
- name: create zabbix log directory
file:
path: /var/log/zabbix
state: directory
recurse: yes
owner: zabbix
group: zabbix
mode: '0755'
- name: chmod zabbix work directory
file:
path: /opt/zabbix
state: directory
recurse: yes
owner: zabbix
group: zabbix
mode: '0755'
- name: copy zabbix server conf
copy:
src: /root/zabbix/files/zabbix_server.conf
dest: /opt/zabbix/etc/
owner: zabbix
group: zabbix
backup: yes
(3)执行 ansible 文件
[root@ansible zabbix]# ansible-playbook zabbix_server.yaml
...
9. 使用 ansible 配置 zabbix_agent
(1)配置 zabbix_server 文件
在 ansible 节点工作目录 files 文件夹下编写 zabbix_agent 的配置文件 zabbix_agentd.conf
[root@ansible zabbix]# vim files/zabbix_agentd.conf
LogFile=/var/log/zabbix/zabbix_agentd.log
PidFile=/opt/zabbix/zabbix_agentd.pid
Server=127.0.0.1,192.168.100.4
ServerActive=192.168.100.4
Hostname=zabbix_agent
(2)编写 ansible 文件
要求修改日志存放路径为 /var/log/zabbix,pid 文件存放在安装目录下,允许接受的服务器列表添加 zabbix_server 节点的 ip 地址,其余参数默认即可;在工作目录下新建 zabbix_agentd.yaml 文件并执行,文件实现的功能要求如下:
① 在 zabbix_agentd 节点创建 /var/log/zabbix 作为 zabbix 的日志存放路径
② 在zabbix_agentd 节点给日志路径和 zabbix 安装路径的用户和组权限都设为 zabbix
③ 把 zabbix_agentd.conf 文件发送给 zabbix_agent 节点的 zabbix 配置目录下
[root@ansible zabbix]# vim zabbix_agentd.yaml
- name: config zabbix agent
hosts: agent
become: yes
tasks:
- name: create zabbix log directory
file:
path: /var/log/zabbix
state: directory
recurse: yes
owner: zabbix
group: zabbix
mode: '0755'
- name: chmod zabbix work directory
file:
path: /opt/zabbix
state: directory
recurse: yes
owner: zabbix
group: zabbix
mode: '0755'
- name: copy zabbix agent conf
copy:
src: /root/zabbix/files/zabbix_agentd.conf
dest: /opt/zabbix/etc/
owner: zabbix
group: zabbix
backup: yes
(3)执行 ansible 文件
[root@ansible zabbix]# ansible-playbook zabbix_agentd.yaml
...
修改 zabbix_server 的 agent 配置文件
[root@zabbix_server ~]# vim /opt/zabbix/etc/zabbix_agentd.conf
LogFile=/var/log/zabbix/zabbix_agentd.log
PidFile=/opt/zabbix/zabbix_agentd.pid
Server=127.0.0.1,192.168.100.4
ServerActive=192.168.100.4
Hostname=Zabbix server
10. 使用 ansible 启动 zabbix 服务
(1)配置 zabbix 启动项
在 ansible 节点工作目录 files 文件夹下编写 zabbix-server.service 作为 zabbix_server 的守护文件
[root@ansible zabbix]# vim files/zabbix-server.service
[Unit]
Description=Zabbix Server
After=network.target syslog.target
Wants=network-online.target
[Service]
Type=forking
User=zabbix
Group=zabbix
PIDFile=/opt/zabbix/zabbix_server.pid
ExecStart=/opt/zabbix/sbin/zabbix_server -c /opt/zabbix/etc/zabbix_server.conf
ExecStop=/bin/kill -SIGTERM $MAINPID
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartSec=10s
TimeoutSec=30s
[Install]
WantedBy=multi-user.target
在 ansible 节点工作目录 files 文件夹下编写 zabbix-agent.service 作为 zabbix_agent 的守护文件
[root@ansible zabbix]# vim files/zabbix-agent.service
[Unit]
Description=Zabbix Agent
After=network.target syslog.target
Wants=network-online.target
[Service]
Type=forking
User=zabbix
Group=zabbix
PIDFile=/opt/zabbix/zabbix_agentd.pid
ExecStart=/opt/zabbix/sbin/zabbix_agentd -c /opt/zabbix/etc/zabbix_agentd.conf
ExecStop=/bin/kill -SIGTERM $MAINPID
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartSec=10s
TimeoutSec=30s
[Install]
WantedBy=multi-user.target
(2)编写 ansible 文件
根据实际情况编写配置文件路径和 pid 路径,用户和组使用 zabbix;在工作目录下新建 zabbix_restart.yaml 文件并执行,文件实现的功能要求如下:
① 使用复制模块把 zabbix-server.service 文件发送给 zabbix_server 节点的对应目录下
② 使用复制模块把 zabbix-agent.service 文件发送给 zabbix_server 节点和 zabbix_agent 节点的对应目录下
③ 在 zabbix_server 节点重启 zabbix-server、zabbix-agent 服务并设置开机自启
④ 在 zabbix_agent 节点重启 zabbix-agent 服务并设置开机自启
[root@ansible zabbix]# vim zabbix_restart.yaml
- name: start zabbix
hosts: agent
become: yes
tasks:
- name: copy server service
copy:
src: /root/zabbix/files/zabbix-server.service
dest: /etc/systemd/system/
when: inventory_hostname == 'zabbix_server'
- name: copy agent service
copy:
src: /root/zabbix/files/zabbix-agent.service
dest: /etc/systemd/system/
- name: restart server and agent
systemd:
name: "{{ item }}"
state: restarted
daemon_reload: yes
enabled: yes
loop:
- zabbix-server
- zabbix-agent
when: inventory_hostname == 'zabbix_server'
- name: restart agent
systemd:
name: zabbix-agent
state: restarted
daemon_reload: yes
enabled: yes
when: inventory_hostname == 'zabbix_agent'
(3)执行 ansible 文件
[root@ansible zabbix]# ansible-playbook zabbix_restart.yaml
...
11. 添加 agent 节点到监控主机
(1)查看主机列表
使用浏览器访问 zabbix 首页,此时 zabbix 服务器端应该已是运行状态且系统信息均能读取到,主机列表存在一台可用的 Zabbix server 主机;

(2)创建 zabbix_agent 主机
将 zabbix_agent 节点添加到主机列表中,主机名称为 zabbix_agent,模板选择 Linux by Zabbix agent,主机群组选择 Linux servers,使用 Agent 接口,ip 使用 ip,端口默认

(3)刷新浏览器页面查看 zabbix_agent 状态
添加完成后两台主机都应为可用状态

12. 测试监控功能
(1)zabbix_agent 安装 nginx
在 zabbix_agent 节点安装 nginx 服务并启用,nginx 端口默认为 80
[root@zabbix_agent ~]# yum install -y nginx net-tools
启动 nginx 并设置开机自启
[root@zabbix_agent ~]# systemctl enable nginx --now
查看监听端口
[root@zabbix_agent ~]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 41811/zabbix_agentd
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 42042/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1105/sshd: /usr/sbi
tcp6 0 0 :::80 :::* LISTEN 42042/nginx: master
tcp6 0 0 :::22 :::* LISTEN 1105/sshd: /usr/sbi
测试访问 nginx 首页
[root@zabbix_agent ~]# curl localhost
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Test Page for the Nginx HTTP Server on openEuler</title>
<style type="text/css">
/*<![CDATA[*/
body {
background-color: #fff;
color: #000;
font-size: 0.9em;
font-family: sans-serif,helvetica;
margin: 0;
padding: 0;
}
:link {
color: #c00;
}
:visited {
color: #c00;
}
a:hover {
color: #f50;
}
h1 {
text-align: center;
margin: 0;
padding: 0.6em 2em 0.4em;
background-color: #294172;
color: #fff;
font-weight: normal;
font-size: 1.75em;
border-bottom: 2px solid #000;
}
h1 strong {
font-weight: bold;
font-size: 1.5em;
}
h2 {
text-align: center;
background-color: #3C6EB4;
font-size: 1.1em;
font-weight: bold;
color: #fff;
margin: 0;
padding: 0.5em;
border-bottom: 2px solid #294172;
}
hr {
display: none;
}
.content {
padding: 1em 5em;
}
.alert {
border: 2px solid #000;
}
img {
border: 2px solid #fff;
padding: 2px;
margin: 2px;
}
a:hover img {
border: 2px solid #294172;
}
.logos {
margin: 1em;
text-align: center;
}
/*]]>*/
</style>
</head>
<body>
<h1>Welcome to <strong>nginx</strong> on openEuler!</h1>
<div class="content">
<p>This page is used to test the proper operation of the
<strong>nginx</strong> HTTP server after it has been
installed. If you can read this page, it means that the
web server installed at this site is working
properly.</p>
<div class="alert">
<h2>Website Administrator</h2>
<div class="content">
<p>This is the default <tt>index.html</tt> page that
is distributed with <strong>nginx</strong> on
openEuler. It is located in
<tt>/usr/share/nginx/html</tt>.</p>
<p>You should now put your content in a location of
your choice and edit the <tt>root</tt> configuration
directive in the <strong>nginx</strong>
configuration file
<tt>/etc/nginx/nginx.conf</tt>.</p>
</div>
</div>
<div class="logos">
<a href="http://nginx.net/"><img
src="nginx-logo.png"
alt="[ Powered by nginx ]"
width="121" height="32" /></a>
</div>
</div>
</body>
</html>
(2)创建模版
浏览器访问 zabbix 首页创建模板,模板名称为 listen 80 status,模板组为 Templates

(3)创建监控项
在创建的模板中添加监控项,名称为 port 80,键值选择 net.tcp.port,更新间隔 5s

(4)创建触发器
在创建的模板中添加触发器,名称为 port 80 down,监控项选择创建的 port 80,严重性选择灾难

(5)创建图表
在创建的模板中创建图表,名称为 port 80 status,使用监控项 port 80

(6)更新主机模版链接
完成后将模板更新到 zabbix_agent 主机中

(7)测试监控是否正常
在 zabbix_agent 主机关闭 nginx 测试 zabbix 触发
[root@zabbix_agent ~]# systemctl stop nginx
在 zabbix 仪表盘页面查看是否显示灾难级告警

13. 运行所有 playbooks
(1)编写 main 文件
编写一个 main.yaml,让所有 YAML 文件一次性顺序执行
[root@ansible zabbix]# vim main.yaml
- import_playbook: system.yaml
- import_playbook: install_chrony.yaml
- import_playbook: install_zabbix.yaml
- import_playbook: install_php.yaml
- import_playbook: install_mysql.yaml
- import_playbook: zabbix_server.yaml
- import_playbook: zabbix_agentd.yaml
- import_playbook: zabbix_restart.yaml
目录结构如下
[root@ansible zabbix]# tree
.
├── ansible.cfg
├── files
│ ├── data.sql
│ ├── ftp.repo
│ ├── images.sql
│ ├── schema.sql
│ └── zabbix-7.0.11.tar.gz
├── install_chrony.yaml
├── install_mysql.yaml
├── install_php.yaml
├── install_zabbix.yaml
├── inventory
├── main.yaml
├── selinux.yaml
└── system.yaml
2 directories, 14 files
(2)运行 main 文件
[root@ansible zabbix]# ansible-playbook main.yaml
...

浙公网安备 33010602011771号