Ansible部署zabbix
# Ansible部署zabbix
节点 | IP | 主机名 |
---|---|---|
ansible | 172.25.253.5 | ansible |
server | 172.25.253.11 | zabbix-server |
agent | 172.25.253.20 | zabbix-agent |
# 查看当前的文件树
[root@ansible ~]# tree ansible
ansible
├── hosts
├── install_zabbix.yaml
├── local.repo
└── templates
├── zabbix_agentd.conf.j2
├── zabbix.conf.j2
└── zabbix_server.conf.j2
# 查看ansible的主机清单
[root@ansible ~]# cat /etc/ansible/hosts
[zabbix_server]
172.25.253.11
[zabbix_agent]
172.25.253.20 serverip=172.25.253.11
# ansible部署zabbix集群
# 1.基础操作:
# 配置hosts文件和repo源
[root@ansible ansible]# cat hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.253.11 zabbix-server
172.25.253.20 zabbix-agent
[root@ansible ansible]# cat local.repo
[centos]
name=centos
baseurl=http://172.25.253.144/file/repo/centos/
gpgcheck=0
[zabbix]
name=zabbix
baseurl=http://172.25.253.144/file/repo/zabbix/
gpgcheck=0
# 2.配置templates
[root@ansible templates]# cat zabbix.conf.j2
···
<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>
[root@ansible templates]# grep -n '^'[a-Z] zabbix_server.conf.j2
38:LogFile=/var/log/zabbix/zabbix_server.log
49:LogFileSize=0
72:PidFile=/var/run/zabbix/zabbix_server.pid
82:SocketDir=/var/run/zabbix
92:DBHost=localhost #取消改行注释
100:DBName=zabbix
116:DBUser=zabbix
125:DBPassword=zabbix #取消改行注释
131:DBSocket=/var/lib/mysql/mysql.sock #修改mysql的路径
329:SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
446:Timeout=4
488:AlertScriptsPath=/usr/lib/zabbix/alertscripts
498:ExternalScripts=/usr/lib/zabbix/externalscripts
534:LogSlowQueries=3000
[root@ansible templates]# grep -n '^'[a-Z] zabbix_agentd.conf.j2
13:PidFile=/var/run/zabbix/zabbix_agentd.pid
32:LogFile=/var/log/zabbix/zabbix_agentd.log
43:LogFileSize=0
97:Server={{ serverip }} #添加server的IP
138:ServerActive={{ serverip }} #添加server的IP
149:Hostname=Zabbix-agent #添加Server的主机名
267:Include=/etc/zabbix/zabbix_agentd.d/*.conf
# 3.编写playbook
[root@ansible ansible]# cat install_zabbix.yaml
- name: First tasks
hosts: zabbix_server
remote_user: root
tasks:
- name: cp hosts
copy: src=hosts dest=/etc/hosts
- name: shell
shell: rm -rf /etc/yum.repos.d/*
- name: cp local.repo
copy: src=local.repo dest=/etc/yum.repos.d/local.repo
- name: install zabbix
yum:
name:
- httpd
- mariadb
- mariadb-server
- zabbix-server-mysql
- zabbix-web-mysql
- zabbix-agent
- zabbix-server
- name: start httpd service
service: name=httpd state=started
- name: start mariadb service
service: name=mariadb state=started
- name: create database
shell: mysql -uroot -e 'create database zabbix character set utf8 collate utf8_bin;'
- name: grant all %
shell: mysql -uroot -e "grant all privileges on zabbix.* to zabbix@'%' identified by 'zabbix';"
- name: grant all localhost
shell: mysql -uroot -e "grant all privileges on zabbix.* to zabbix@'localhost' identified by 'zabbix';"
- name: add sql
shell: zcat /usr/share/doc/zabbix-server-mysql-3.4.15/create.sql.gz | mysql -uroot zabbix
- name: change PRC
shell: sed -i '/date.timezone =/a\date.timezone = PRC' /etc/php.ini
- name: change date
template: src=templates/zabbix.conf.j2 dest=/etc/httpd/conf.d/zabbix.conf
- name: restart httpd
shell: systemctl restart httpd
- name: copy
template: src=templates/zabbix_server.conf.j2 dest=/etc/zabbix/zabbix_server.conf
- name: start zabbix
service: name=zabbix-server state=started
- name: Two First
hosts: zabbix_agent
remote_user: root
tasks:
- name: cp hosts
copy: src=hosts dest=/etc/hosts
- name: shell
shell: rm -rf /etc/yum.repos.d/*
- name: cp local.repo
copy: src=local.repo dest=/etc/yum.repos.d/local.repo
- name: install zabbix
yum: name=zabbix-agent state=latest
- name: change config
template: src=templates/zabbix_agentd.conf.j2 dest=/etc/zabbix/zabbix_agentd.conf
- name: restart service
service: name=zabbix-agent state=started
# 4.执行playbook
[root@ansible ansible]# ansible-playbook install_zabbix.yaml
PLAY [First tasks] *****************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************
ok: [172.25.253.11]
TASK [cp hosts] *********************************************************************************************************************************************
ok: [172.25.253.11]
·
·
·
PLAY RECAP **************************************************************************************************************************************
172.25.253.138 : ok=10 changed=5 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
172.25.253.139 : ok=10 changed=5 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
172.25.253.140 : ok=10 changed=5 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
# 5.检查zabbix的端口
[root@ansible ansible]# ansible all -m shell -a "ss -ntpl | grep 10051 "
172.25.253.20 | FAILED | rc=1 >>
non-zero return code
172.25.253.11 | CHANGED | rc=0 >>
LISTEN 0 128 *:10051 *:* users:(("zabbix_server",pid=3254,fd=5),("zabbix_server",pid=3253,fd=5),("zabbix_server",pid=3252,fd=5),("zabbix_server",pid=3249,fd=5),("zabbix_server",pid=3248,fd=5),("zabbix_server",pid=3246,fd=5),("zabbix_server",pid=3244,fd=5),("zabbix_server",pid=3242,fd=5),("zabbix_server",pid=3241,fd=5),("zabbix_server",pid=3240,fd=5),("zabbix_server",pid=3239,fd=5),("zabbix_server",pid=3238,fd=5),("zabbix_server",pid=3237,fd=5),("zabbix_server",pid=3236,fd=5),("zabbix_server",pid=3235,fd=5),("zabbix_server",pid=3234,fd=5),("zabbix_server",pid=3233,fd=5),("zabbix_server",pid=3232,fd=5),("zabbix_server",pid=3231,fd=5),("zabbix_server",pid=3230,fd=5),("zabbix_server",pid=3229,fd=5),("zabbix_server",pid=3228,fd=5),("zabbix_server",pid=3227,fd=5),("zabbix_server",pid=3226,fd=5),("zabbix_server",pid=3225,fd=5),("zabbix_server",pid=3223,fd=5),("zabbix_server",pid=3222,fd=5),("zabbix_server",pid=3219,fd=5),("zabbix_server",pid=3218,fd=5),("zabbix_server",pid=3217,fd=5),("zabbix_server",pid=3216,fd=5),("zabbix_server",pid=3215,fd=5),("zabbix_server",pid=3214,fd=5),("zabbix_server",pid=3209,fd=5))
LISTEN 0 128 :::10051 :::* users:(("zabbix_server",pid=3254,fd=6),("zabbix_server",pid=3253,fd=6),("zabbix_server",pid=3252,fd=6),("zabbix_server",pid=3249,fd=6),("zabbix_server",pid=3248,fd=6),("zabbix_server",pid=3246,fd=6),("zabbix_server",pid=3244,fd=6),("zabbix_server",pid=3242,fd=6),("zabbix_server",pid=3241,fd=6),("zabbix_server",pid=3240,fd=6),("zabbix_server",pid=3239,fd=6),("zabbix_server",pid=3238,fd=6),("zabbix_server",pid=3237,fd=6),("zabbix_server",pid=3236,fd=6),("zabbix_server",pid=3235,fd=6),("zabbix_server",pid=3234,fd=6),("zabbix_server",pid=3233,fd=6),("zabbix_server",pid=3232,fd=6),("zabbix_server",pid=3231,fd=6),("zabbix_server",pid=3230,fd=6),("zabbix_server",pid=3229,fd=6),("zabbix_server",pid=3228,fd=6),("zabbix_server",pid=3227,fd=6),("zabbix_server",pid=3226,fd=6),("zabbix_server",pid=3225,fd=6),("zabbix_server",pid=3223,fd=6),("zabbix_server",pid=3222,fd=6),("zabbix_server",pid=3219,fd=6),("zabbix_server",pid=3218,fd=6),("zabbix_server",pid=3217,fd=6),("zabbix_server",pid=3216,fd=6),("zabbix_server",pid=3215,fd=6),("zabbix_server",pid=3214,fd=6),("zabbix_server",pid=3209,fd=6))
[root@ansible ansible]# ansible all -m shell -a "ss -ntpl | grep 10050 "
172.25.253.20 | CHANGED | rc=0 >>
LISTEN 0 128 *:10050 *:* users:(("zabbix_agentd",pid=2035,fd=4),("zabbix_agentd",pid=2034,fd=4),("zabbix_agentd",pid=2033,fd=4),("zabbix_agentd",pid=2032,fd=4),("zabbix_agentd",pid=2031,fd=4),("zabbix_agentd",pid=2030,fd=4))
LISTEN 0 128 :::10050 :::* users:(("zabbix_agentd",pid=2035,fd=5),("zabbix_agentd",pid=2034,fd=5),("zabbix_agentd",pid=2033,fd=5),("zabbix_agentd",pid=2032,fd=5),("zabbix_agentd",pid=2031,fd=5),("zabbix_agentd",pid=2030,fd=5))
172.25.253.11 | FAILED | rc=1 >>
non-zero return code
上次更新: 2023/11/28, 22:03:59