/home/devops/ansible/hosts
对zabbix部署所需的主机进行设置[devops@server10 ansible]$ cat hosts
[devops@server10 ansible]$ cd zabbix
[devops@server10 zabbix]$ ls
agent.yml db.yml my.cnf web.yml zabbix.conf
create.sql.gz deploy.yml server.yml zabbix_agentd.conf.j2 zabbix_server.conf
解释一下这些文件:
找一部主机安装mariadb
,将my.cnf
拷贝到当前目录下/home/devops/ansible/zabbix/
vim my.cnf
添加 character_set_server=utf8
zabbix
,将其zabbix.conf
文件拷贝过来vim zabbix.conf
将文件中的地区进行修改
[devops@server10 zabbix]$ vim zabbix_agentd.conf.j2
[devops@server10 zabbix]$ vim zabbix_server.conf
---
- hosts: server
tasks:
- name: add zabbix repo
yum_repository:
name: zabbix
description: zabbix 4.0
baseurl: https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/
gpgcheck: no
- name: add update repo
yum_repository:
name: update
description: non-supported
baseurl: https://mirrors.aliyun.com/zabbix/non-supported/rhel/7/x86_64/
gpgcheck: no
- name: install zabbix-server
yum:
name: zabbix-server-mysql,zabbix-agent
state: present
- name: config zabbix-server
copy:
src: zabbix_server.conf
dest: /etc/zabbix/zabbix_server.conf
owner: root
group: zabbix
mode: 640
notify: restart zabbix-server
- name: start zabbix-server
service:
name: "{{ item }}"
state: started
loop:
- zabbix-server
- zabbix-agent
- firewalld
- name: config firewalld
firewalld:
port: 10051/tcp
permanent: yes
immediate: yes
state: enabled
handlers:
- name: restart zabbix-server
service:
name: zabbix-server
state: restarted
db部分
在此导入数据库的时候一定要注意,确保导入成功
具体请参见
!!该数据库已经经过初始化,所以有login_user 和 login_password
---
- hosts: db
tasks:
- name: install mariadb
yum:
name: mariadb-server,MySQL-python
state: present
- name: config mariadb
copy:
src: my.cnf
dest: /etc/my.cnf
notify: restart mariadb
- name: start mariadb
service:
name: '{{ item }}'
state: started
enabled: yes
loop:
- mariadb
- firewalld
- name: create database zabbix
mysql_db:
login_user: root
login_password: westos
name: zabbix
state: present
notify: import create.sql
- name: create user
mysql_user:
login_user: root
login_password: westos
name: zabbix
password: zabbix
host: '%'
priv: 'zabbix.*:ALL'
state: present
- name: copy create.sql
copy:
src: create.sql.gz
dest: /tmp/create.sql.gz
- name: config firewalld
firewalld:
service: mysql
permanent: yes
immediate: yes
state: enabled
handlers:
- 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
agent部分
---
- hosts: agent
tasks:
- name: add zabbix repo
yum_repository:
name: zabbix
description: zabbix 4.0
baseurl: https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/
gpgcheck: no
- name: install zabbix-agent
yum:
name: zabbix-agent
state: present
- name: config zabbix-agent
template:
src: zabbix_agentd.conf.j2
dest: /etc/zabbix/zabbix_agentd.conf
owner: root
group: root
mode: 644
notify: restart zabbix-agent
- name: start zabbix-agent
service:
name: "{{ item }}"
state: started
loop:
- zabbix-agent
- firewalld
- name: config firewalld
firewalld:
port: 10050/tcp
permanent: yes
immediate: yes
state: enabled
handlers:
- name: restart zabbix-agent
service:
name: zabbix-agent
state: restarted
web部分
---
- hosts: web
tasks:
- name: add zabbix repo
yum_repository:
name: zabbix
description: zabbix 4.0
baseurl: https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/
gpgcheck: no
- name: add update repo
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
yum_repository:
name: centos
description: centos 7
baseurl: https://mirrors.aliyun.com/centos/7/os/x86_64/
gpgcheck: no
- name: install zabbix-web
yum:
name: zabbix-web-mysql
state: present
- name: config zabbix-web
copy:
src: zabbix.conf
dest: /etc/httpd/conf.d/zabbix.conf
notify: restart httpd
- name: start httpd
service:
name: "{{ item }}"
state: started
loop:
- httpd
- firewalld
- name: config firewalld
firewalld:
service: http
permanent: yes
immediate: yes
state: enabled
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
将以上部分整合到一个playbook中即deploy.yml
[devops@server10 ansible]$ ansible-playbook zabbix/deploy.yml
PLAY [db] *************************************************************************************************
TASK [Gathering Facts] ************************************************************************************
ok: [172.25.65.11]
TASK [install mariadb] ************************************************************************************
ok: [172.25.65.11]
TASK [config mariadb] *************************************************************************************
ok: [172.25.65.11]
TASK [start mariadb] **************************************************************************************
ok: [172.25.65.11] => (item=mariadb)
ok: [172.25.65.11] => (item=firewalld)
TASK [create database zabbix] *****************************************************************************
ok: [172.25.65.11]
TASK [create user] ****************************************************************************************
ok: [172.25.65.11]
TASK [copy create.sql] ************************************************************************************
ok: [172.25.65.11]
执行的时候,一定要确保这一步有做,导入的时候可能会由于电脑性能原因导入较慢
结果:
[root@server12 web]# cat zabbix.conf.php