1 引言
zabbix 172.16.212.129 #安装server和mariadb
ansible 172.16.212.134
2 安装ansible
- 安装
yum install epel-release -y
yum install ansible -y
- 生成ssh密钥对
ssh-keygen
注意:不断按回车键即可
ssh-copy-id root@172.16.212.129
注意:按提示输入129的密码
- 配置inventory
cat /etc/ansible/hosts | grep -v “^#” | grep -v “^$”
[zabbixsrv]
172.16.212.129
- 测试连通性
ansible -i /etc/ansible/hosts zabbixsrv -m ping
绿色即为成功!
3 配置ansible自动安装zabbix-server
- 生成roles目录
#zabbixserver
mkdir /etc/ansible/roles/zabbixsrv/{files,tasks} -pv
#Mariadb
mkdir /etc/ansible/roles/mariadb/{files,tasks} -pv
#apache
mkdir /etc/ansible/roles/ap/{files,tasks} -pv
- 创建剧本
#vim /etc/ansible/roles/zabbixsrv/tasks/main.yml
- name: copy script
copy: src=~/zabbixsrv.sh dest=~/ mode=0700
- name: run script
shell: ~/zabbixsrv.sh
- name: config zabbix
copy: src=/etc/ansible/roles/zabbixsrv/files/zabbix_server.conf dest=/etc/zabbix/
- name: start service
service: name=zabbix-server state=started
#vim /etc/ansible/roles/mariadb/tasks/main.yml
- name: install mariadb-server
yum: name=mariadb-server state=installed
- name: config mariadb
copy: src=/etc/ansible/roles/mariadb/files/my.cnf dest=/etc/my.cnf
- name: start mariadb
service: name=mariadb state=started
- name: copy script
copy: src=~/mariadb.sh dest=~/ mode=0770
- name: run script
shell: ~/mariadb.sh
#vim /etc/ansible/roles/ap/tasks/main.yml
- name: install paskages
yum: name={{ item }}
with_items:
- httpd
- php
- php-mysql
- name: config php
copy: src=/etc/ansible/roles/ap/files/zabbix.conf dest=/etc/httpd/conf.d/
- name: start service
service: name=httpd state=started
#vim /etc/ansible/all.yml
- hosts: zabbixsrv
remote_user: root
roles:
- zabbixsrv
- mariadb
- ap
- 创建要用到的脚本
#vim zabbixsrv.sh
#!/bin/bash
curl https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX-A14FE591 \
-o /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
curl https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX \
-o /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
wget -P /etc/yum.repos.d http://mirrors.aliyun.com/repo/Centos-7.repo
yum makecache -y
yum -y install zabbix-server-mysql zabbix-web-mysql zabbix-agent
#vim mariadb.sh
#!/bin/bash
mysql -e "grant all privileges on *.* to root@'%' identified by '122333';"
a=`mysql -e "show databases;" | grep "zabbix"`
if [[ $a != "zabbix" ]];then
mysql -e "create database zabbix default character set utf8 collate utf8_bin;"
fi
b=`mysql -e "select user,host from mysql.user;" | grep -o "zabbix"`
if [[ $b != "zabbix" ]];then
mysql -e "grant all privileges on zabbix.* to zabbix@'%' identified by '122333';"
fi
mysql -e "show tables from zabbix;" &> /dev/null
if [ $? -eq 0 ];then
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p122333 -h172.16.212.129 zabbix
fi
- 创建好要用到的配置文件
#cat /etc/ansible/roles/zabbixsrv/files/zabbix.repo
[zabbix]
name=Zabbix Official Repository - \$basearch
baseurl=https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/\$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
[zabbix-non-supported]
name=Zabbix Official Repository non-supported - \$basearch
baseurl=https://mirrors.aliyun.com/zabbix/non-supported/rhel/7/\$basearch/
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
gpgcheck=1
#cat /etc/ansible/roles/zabbixsrv/files/zabbix_server.conf
DBHost=172.16.212.129
DBName=zabbix
DBUser=zabbix
DBPassword=122333
LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/var/run/zabbix/zabbix_server.pid
SocketDir=/var/run/zabbix
#vim /etc/my.cnf
[mysqld]
innodb_file_per_table=on
skip_name_resolve=on
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d
#cat /etc/ansible/roles/ap/files/zabbix.conf
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>
- 最后一步
ansible-playbook /etc/ansible/all.yml
执行成功:
登录: