LINUX企业级——Ansible-playbook自动安装部署Zabbix
前期准备:
1.三台可上网虚拟机:
server1 172.25.66.1
server2 172.25.66.2
server3 172.25.66.3
2.创建devops用户:
在三台虚拟机上均创建devops用户,并且在server1上对三台虚拟机均做免密操作。
3.配置好相关yum源(rhel7.5.repo、zabbix.repo、update.repo、centos.repo等)
4.在server1上部署ansible
5.大致思路:
分别配置mariadb、zabbix-server、zabbix-agent、zabbix-web角色,之后统一运行。
进行部署:
注:以下server1中的操作用户均为devops
1.mariadb角色
1.1 添加mariadb角色
cd /ansible/roles #进入角色目录
ansible-galaxy init mariadb #添加mariadb角色
cd mariadb #进入mariadb角色
rm -fr tests #删除测试目录
1.2 tasks(任务)配置
cd tasks #进入tasks目录
vim main.yml #编辑yml文件
- name: install mariadb #安装mariadb
yum:
name: mariadb-server,MySQL-python
state: present
- name: config mariadb #配置mariadb
copy:
src: my.cnf
dest: /erc/my.cnf
notify: restart mariadb #触发器
- name: start mariadb #启动mariadb
service:
name: mariadb
state: started
enabled: yes
- name: create database zabbix #添加zabbix库
mysql_db:
login_user: root
login_password: westos
name: zabbix #添加zabbix库
state: present
notify: import create.sql #触发器
- name: create user #添加zabbix用户
mysql_user:
login_user: root
login_password: westos #数据库初始化密码
name: zabbix #添加zabbix用户
password: zabbix
host: '%'
priv: 'zabbix.*:ALL'
state: present
- name: copy create.sql #拷贝相关表文件
copy:
src: create.sql.gz
dest: /tmp/create.sql.gz
1.3 handlers(触发器)配置
cd handlers #进入handlers目录
vim main.yml #编辑yml文件
- name: restart mariadb #重启数据库
service:
name: mariadb
state: restarted
- name: import create.sql #导入相关表文件
mysql_db:
login_user: root
login_password: westos
name: zabbix
state: import
target: /tmp/create.sql.gz
1.4 数据库配置文件修改
1.4.1 my.cnf的修改
cp /etc/my.cnf /home/devops/ansible/mariadb/files #将文件拷贝至角色的相关文件目录下
1.4.2 添加mariadb编码格式
vim /etc/my.cnf中 #编辑配置文件
添加:
charactet_set_server=utf8
1.4.3 create.sql.gz的配置
yum install zabbix-server-mysql -y #安装zabbix-server-mysql
cd /usr/share/doc/zabbix-server-mysql-4.0.5/ #进入相关目录
scp create.sql.gz devops@server1:/home/devops/ansible/roles/mariadb/files #将文件拷贝至角色的相关文件目录下
2.zabbix-server角色
2.1 添加zabbix-server角色
cd /ansible/roles #进入角色目录
ansible-galaxy init zabbix-server #添加zabbix-server角色
cd zabbix-server #进入zabbix-server角色
rm -fr tests #删除测试目录
2.2 tasks(任务)配置
cd tasks #进入tasks目录
vim main.yml #编辑yml文件
- name: ADD zabbix repo #添加yum源
yum_repository:
name: zabbix
description: zabbix 4.0
baseurl: https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/
gpgcheck: no
- name: Update repo #添加更新yum源
yum_repository:
name: update
description: non-supported
baseurl: https://mirrors.aliyun.com/zabbix/non-supported/rhel/7/x86_64/
gpgcheck: no
- name: insatll zabbix-server #安装zabbix-server
yum:
name: zabbix-server-mysql,zabbix-agent
state: present
- name: config zabbix-server #配置zabbix-server
template:
src: zabbix_server.conf.j2
dest: /etc/zabbix/zabbix_server.conf
owner: root
group: zabbix
mode: 640
notify: restart zabbix-server #触发器
- name: start zabbix-server #启动zabbix-server
service:
name: "{{ item }}"
state: started
loop: #两者均启动
- zabbix-server
- zabbix-agent
2.3 handlers(触发器)配置
cd handlers #进入handlers目录
vim main.yml #编辑yml文件
- name: restart zabbix-server #重启zabbix-server
service:
name: zabbix-server
state: restarted
2.4 templates(模板)配置
在server2中:
yum install zabbix-server-mysql zabbix-agent -y #安装zabbix-server-mysql和zabbix-agent
cd /etc/zabbix/ #进入zabbix目录
scp zabbix_server.conf devops@server1:/home/devops/ansible/roles/zabbix-server/templates #将文件拷贝至server1角色的模板目录下
在server1中:
cd templates #进入模板目录
mv zabbix_server.conf zabbix_server.conf.j2 #更改文件名
vim zabbix_server.conf.j2 #编辑模板文件
3.zabbix-agent角色
3.1 添加zabbix-agent角色
cd /ansible/roles #进入角色目录
ansible-galaxy init zabbix-agent #添加zabbix-agent角色
cd zabbix-agent #进入zabbix-agent角色
rm -fr tests #删除测试目录
3.2 tasks(任务)配置
cd tasks #进入tasks目录
vim main.yml #编辑yml文件
- name: add zabbix repo #添加yum源
yum_repository:
name: zabbix
description: zabbix
baseurl: https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/
gpgcheck: no
- name: add update #添加更新yum源
yum_repository:
name: update
description: non-supported
baseurl: https://mirrors.aliyun.com/zabbix/non-supported/rhel/7/x86_64/
gpgcheck: no
- name: install zabbix-agent #安装zabbix-agent
yum:
name: zabbix-agent
state: present
- name: config zabbix-agent #配置zabbix-agent
template:
src: zabbix_agentd.conf.j2
dest: /etc/zabbix/zabbix.agent.conf
owner: root
group: root
mode: 644
notify: restart zabbix-agent #触发器
- name: start zabbix-agent #启动zabbix-agent
service:
name: zabbix-agent
state: started
3.3 handlers(触发器)配置
cd handlers #进入handlers目录
vim main.yml #编辑yml文件
- name: restart zabbix-agent
service:
name: zabbix-agent
state: restarted
3.4 templates(模板)配置
在server2中:
cd /etc/zabbix/ #进入zabbix目录
scp zabbix_agentd.conf devops@server1:/home/devops/ansible/roles/zabbix-agent/templates #将文件拷贝至server1角色的模板目录下
在server1中:
cd templates #进入模板目录
mv zabbix_agentd.conf zabbix_agentd.conf.j2 #更改文件名
vim zabbix_agentd.conf.j2 #编辑模板文件
4.zabbix-web角色
4.1 添加zabbix-web角色
cd /ansible/roles #进入角色目录
ansible-galaxy init zabbix-web #添加zabbix-web角色
cd zabbix-web #进入zabbix-web角色
rm -fr tests #删除测试目录
4.2 tasks(任务)配置
cd tasks #进入tasks目录
vim main.yml #编辑yml文件
- name: add zabbix repo #添加yum源
yum_repository:
name: zabbix
description: zabbix
baseurl: https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/
gpgcheck: no
- name: add update #添加更新yum源
yum_repository:
name: update
description: non-supported
baseurl: https://mirrors.aliyun.com/zabbix/non-supported/rhel/7/x86_64/
gpgcheck: no
- name: add centos repo #添加centos的yum源
yum_repository:
name: centos
description: centos 7
baseurl: https://mirrors.aliyun.com/centos/7/os/x86_64/
gpgcheck: no
- name: install zabbix-web #安装zabbix-web
yum:
name: zabbix-web-mysql
state: present
- name: config zabbix-web #配置zabbix-web
template:
src: zabbix.conf.j2
dest: /etc/httpd/conf.d/zabbix.conf
notify: restart httpd #触发器
- name: start httpd #开启zabbix-web
service:
name: httpd
state: started
4.3 handlers(触发器)配置
cd handlers #进入handlers目录
vim main.yml #编辑yml文件
- name: restart httpd #重启httpd服务
service:
name: httpd
state: restarted
4.4 templates(模板)配置
在server3中:
yum install zabbix-web-mysql -y #安装zabbix-web-mysql
cd /etc/httpd/conf.d/ #进入相关目录
scp zabbix.conf devops@server1:/home/devops/ansible/roles/zabbix-web/templates #将文件拷贝至server1角色的模板目录下
在server1中:
cd templates #进入模板目录
mv zabbix.conf zabbix.conf.j2 #更改文件名
vim zabbix.conf.j2 #编辑模板文件
5.zibbix.yml文件
cd /home/devops/ansible #进入ansible目录
vim zibbix.yml #编辑yml文件
6.运行
ansible-playbook zibbix.yml
7.网页访问
http://172.25.66.3/zabbix