[TOC]
# 一:服务器规划
```
[zabbix-server]
192.168.116.133 hostname=server_zabbix ansible_ssh_user=root ansible_ssh_pass=123456
[zabbix_agent]
192.168.116.135 hostname=agent_zabbix ansible_ssh_user=root ansible_ssh_pass=123456
```
# 二.ansible目录结构
```
.
├── group_vars
│ └── all
├── hosts
├── roles
│ ├── base
│ │ ├── defaults
│ │ ├── files
│ │ │ └── hosts.j2
│ │ ├── handlers
│ │ ├── meta
│ │ ├── tasks
│ │ │ ├── main.yml
│ │ │ └── main.yml.bal
│ │ └── templates
│ │ └── hosts.j2
│ ├── zabbix_agent_install
│ │ ├── defaults
│ │ │ └── main.yml
│ │ ├── files
│ │ ├── handlers
│ │ │ └── main.yml
│ │ ├── meta
│ │ ├── tasks
│ │ │ └── main.yml
│ │ └── templates
│ │ └── zabbix_agentd.conf.j2
│ └── zabbix_server
│ ├── defaults
│ │ └── main.yml
│ ├── files
│ ├── handlers
│ │ └── main.yml
│ ├── meta
│ ├── tasks
│ │ └── main.yml
│ ├── templates
│ │ ├── zabbix.conf.j2
│ │ └── zabbix_server.conf.j2
│ └── vars
│ └── all.yml
├── site.retry
└── site.yml
```
# 三:ansible安装zabbix-server
## 1.tasks.yml
```
[root@linux-node01 zabbix_server]# cat tasks/main.yml
---
- name: Install Mysql-python
yum: name=MySQL-python state=latest
- name: install zabbix repo
shell: "rpm -Uvh http://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm --force"
- name: Install Zabbix Server
yum: name={{ item }} state=latest
with_items:
- zabbix-server-mysql
- zabbix-web-mysql
- name: Install httpd
yum: name=httpd state=latest
- name: Install Mariadb
yum: name={{ item }} state=latest
with_items:
- mariadb
- mariadb-server
- name: start mariadb
service: name=mariadb state=started
- name: change mariadb passwd
mysql_user: login_host=localhost login_user=root login_password='' user=root password=123456 config_file=/etc/my.cnf
ignore_errors: True
- name: create zabbix db
mysql_db: login_host=localhost login_user=root login_password=123456 name=zabbix state=present config_file=/etc/my.cnf
- name: import zabbix db
mysql_db: login_host=localhost login_user=root login_password=123456 config_file=/etc/my.cnf name=zabbix state=import target=/usr/share/doc/zabbix-server-mysql-4.0.1/create.sql.gz
- name: create zabbix user
mysql_user: login_host=localhost login_user=root login_password=123456 config_file=/etc/my.cnf user={{ zabbix_db_user }} password={{ zabbix_password }} priv=zabbix.*:ALL state=present
- name: copy zabbix-server conf
template: src=zabbix_server.conf.j2 dest=/etc/zabbix/zabbix_server.conf backup=yes
notify:
- restart zabbix-server
- name: copy zabbix-http
template: src=zabbix.conf.j2 dest=/etc/httpd/conf.d/zabbix.conf backup=yes
notify:
- restart http
- name: start zabbix-server
service: name=zabbix-server state=started
```
## 2. defaults/main.yml
```
[root@linux-node01 zabbix_server]# cat defaults/main.yml
---
zabbix_db_user: zabbix
zabbix_password: 123456
dbhost: localhost
dbname: zabbix
dbuser: zabbix
dbpasswd: 123456
```
## 3.cat handlers/main.yml
```
[root@linux-node01 zabbix_server]# cat handlers/main.yml
---
- name: restart zabbix-server
service: name=zabbix-server state=restarted
- name: restart http
service: name=httpd state=restarted
```
## 4.templates/zabbix.conf.j2
```
cat templates/zabbix.conf.j2
#
# Zabbix monitoring system php web frontend
#
Alias /zabbix /usr/share/zabbix
<Directory "/usr/share/zabbix">
Options FollowSymLinks
AllowOverride None
Require all granted
<IfModule mod_php5.c>
php_value max_execution_time 300
php_value memory_limit 128M
php_value post_max_size 16M
php_value upload_max_filesize 2M
php_value max_input_time 300
php_value max_input_vars 10000
php_value always_populate_raw_post_data -1
php_value date.timezone Asia/Shanghai
</IfModule>
</Directory>
<Directory "/usr/share/zabbix/conf">
Require all denied
</Directory>
<Directory "/usr/share/zabbix/app">
Require all denied
</Directory>
<Directory "/usr/share/zabbix/include">
Require all denied
</Directory>
<Directory "/usr/share/zabbix/local">
Require all denied
</Directory>
```
## 5. cat templates/zabbix_server.conf.j2
```
# DBHost=localhost
DBHost={{ dbhost }}
### Option: DBName
# Database name.
# For SQLite3 path to database file must be provided. DBUser and DBPassword are ignored.
#
# Mandatory: yes
# Default:
# DBName=
DBName={{ dbname }}
DBUser={{ dbuser }}
### Option: DBPassword
# Database password. Ignored for SQLite.
# Comment this line if no password is used.
#
# Mandatory: no
# Default:
DBPassword={{ dbpasswd }}
```
# 四.初始化服务器设置
## 1.task
```
tasks/main.yml
---
- name: set hostname
hostname: name={{ hostname }}
- name: change network files
shell: sed -i "s/HOSTNAME=.*/HOSTNAME={{ hostname }}/g" /etc/sysconfig/network
- name: stop Iptables
service: name=firewalld state=stopped enabled=no
- name: disable selinux
shell: /usr/sbin/setenforce 0 && sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
# - name: Install libselinux-python
# raw: yum install libselinux-python -y
# - name: copy epel yum source
# copy: src={{ item.src }} dest={{ item.dest }} owner=root group=root mode=644
# with_items:
# - {src: epel.repo, dest: /etc/yum.repos.d/epel.repo }
# - {src: RPM-GPG-KEY-EPEL-6, dest: /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 }
# - {src: RPM-GPG-KEY-ZABBIX, dest: /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX }
# - {src: zabbix.repo, dest: /etc/yum.repos.d/zabbix.repo }
- name: copy /etc/hosts files
template: src=hosts.j2 dest=/etc/hosts owner=root group=root mode=644
```
## 2.cat templates/hosts.j2
```
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
{% for host in groups['all'] %}
{{ hostvars[host]['inventory_hostname'] }} {{ hostvars[host]['hostname'] }}
{% endfor %}
```
# 五.安装zabbix-agent
## 1.cat tasks/main.yml
```
---
- name: install zabbix repo
shell: "rpm -Uvh http://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm --force"
- name: Install Zabbix agent
yum: name=zabbix-agent state=latest
- name: copy zabbix-agent.conf
template: src=zabbix_agentd.conf.j2 dest=/etc/zabbix/zabbix_agentd.conf backup=yes
notify:
- Restart zabbix_agent
- name: started zabbix_agentd
service: name=zabbix-agent state=started
```
## 2.cat handlers/main.yml
```
---
- name: Restart zabbix_agent
service: name=zabbix-agent state=restarted
```
## 3.cat defaults/main.yml
```
---
agent_port: 10050
server: 192.168.116.133
```
## 4.templates/zabbix_agentd.conf.j2
```
# Server=
Server={{ zabbix_server }}
### Option: ListenPort
# Agent will listen on this port for connections from the server.
#
# Mandatory: no
# Range: 1024-32767
# Default:
ListenPort={{ agent_port }}
### Option: ListenIPServerActive={{ server }}
### Option: Hostname
# Unique, case sensitive hostname.
# Required for active checks and must match hostname as configured on the server.
# Value is acquired from HostnameItem if undefined.
#
# Mandatory: no
# Default:
# Hostname=
Hostname={{ ansible_hostname }}
```
# 六. 编写入口脚本及安装
```
cat site.yml
---
- hosts: zabbix-server
remote_user: root
roles:
- base
- zabbix_server
- hosts: zabbix_agent
remote_user: root
roles:
- base
- zabbix_agent_install
```
运行 ansible-playbook -i hosts site.yml
结果:

- 0.空白目录
- 1.ansible
- 1.1 ansible playbook 搭建haproxy+lamp
- 1.2 ansibleplaybook 安装zabbix
- 1.0 安装最新版ansible
- 2.docker
- 2.2.安装docker私有仓库harbor
- 2.1 安装docker并启动一个容器操作
- 2.3 基于Docker-WEB自动化管理Docker容器实战
- 2.4 通过实例演示dockerfile的语法
- 2.5 通过docker部署mysql5.7
- 2.6 通过docker部署rabbitmq
- 2.7 docker国内加速镜像
- 3.k8s
- 4.mysql
- 1.Xtrabackup实现数据的备份与恢复
- 2.mysql主从复制
- mysql报错修复
- 1.ERROR 1146 (42S02): Table 'performance_schema.session_variables' doesn't exist
- 5.lvs+keepalive
- 1.haproxy.conf
- 5.1 haproxy
- haproxy配置详解
- 5.2 ab压测haproxy架构和nginx架构对比
- 5.3 基于ve的压测
- 6.prometheus
- 7.python学习
- 7.1 python基础部分
- 8.elk
- 8.1 es数据备份及导出导入快照
- 9.jenkins-git-ansible-CI/CD
- 10.nosql
- 10.1 mongo
- 10.2 redis
- 10.2.1 redis集群搭建
- 10.3 kafka
- 10.4 rabbitmq
- 11.granfa
- 12.nginx
- 13.shell
- 14.zabbix
- 14.1 install zabbix4.0
- 14.2 zabbix监控日志报错
- 14.3 install zabbix for docker
