Linux下利用自动化运维工具Ansible自动化部署zabbix

续我的博文:https://mp.****.net/postedit/88865995。即Ansible已经部署好。

 

 

一、实验环境(rhel7.3版本)

1.selinux和firewalld状态为disabled

2.各主机信息如下:

主机 ip
server1(ansible的服务端) 172.25.83.1
server2(ansible的服务端) 172.25.83.2
物理机(用来共享zabbix镜像的) 172.25.83.83/172.25.254.83

 

二、zabbix的自动化部署

 

1、mariadb目录

[[email protected] ~]# cd /etc/ansible/roles/
[[email protected] roles]# ls
[[email protected] roles]# mkdir mariadb   #mariadb目录的名字随意给,因为搭建的是mariadb服务,所以这里创建的目录的名字为mariadb
[[email protected] roles]# cd mariadb
[[email protected] mariadb]# mkdir files tasks handlers vars templates meta defaults   #一般需要创建这几个目录,分角色存储。这里可能用不到这么多目录,但是为了规范,将这些目录都创建出来。这些目录的名字,就是这些名字,不能随意改



[[email protected] mariadb]# cd files/
[[email protected] files]# cp /etc/my.cnf .   #将本机上的/etc/my.cnf文件拷贝到当前目录下,如果本机没有,则从别的安装有mariadb-server或mysql的主机上拷贝
[[email protected] files]# vim my.cnf
  1 [mysqld]
  2 datadir=/var/lib/mysql
  3 socket=/var/lib/mysql/mysql.sock
  4 # Disabling symbolic-links is recommended to prevent assorted security     risks
  5 symbolic-links=0
  6 # Settings user and group are ignored when systemd is used.
  7 # If you need to run mysqld under a different user or group,
  8 # customize your systemd unit file for mariadb according to the
  9 # instructions in http://fedoraproject.org/wiki/Systemd
 10 character-set-server=utf8   #新增加的内容,设定字符集为utf8字符集
 11 
 12 [mysqld_safe]
 13 log-error=/var/log/mariadb/mariadb.log
 14 pid-file=/var/run/mariadb/mariadb.pid
 15 
 16 #
 17 # include all files from the config directory
 18 #
 19 !includedir /etc/my.cnf.d
 20 



[[email protected] files]# cd ../tasks/
[[email protected] tasks]# vim main.yml   #main.yml文件的名字是固定的,不要随意改
- name: install mariadb-server
  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=mariadb state=started




[[email protected] tasks]# cd ../handlers/
[[email protected] handlers]# vim main.yml
- name: restart mariadb
  service: name=mariadb state=restarted

 

2、配置所需的yum源(下载zabbix-server的软件报好)

#先在物理机上共享zabbix镜像
[[email protected] zabbix]# cp -r 4.0/ /var/www/html/
[[email protected] html]# ll -d 4.0/
drwx------ 2 root root 4096 Mar 29 01:11 4.0/
[[email protected] html]# chmod go+rx 4.0/   #给其他用户增加可读和可执行的权限,否则访问不了
[[email protected] html]# ll -d 4.0/
drwxr-xr-x 2 root root 4096 Mar 29 01:11 4.0/
[[email protected] html]# createrepo --update /var/www/html/4.0/   #生成4.0目录下需要的repodata目录
  • 如果不生成repodata目录,会报下面的错

Linux下利用自动化运维工具Ansible自动化部署zabbix

  • 查看镜像是否共享成功

Linux下利用自动化运维工具Ansible自动化部署zabbix

 

3、zabbix-server目录

[[email protected] tasks]# cd /etc/ansible/roles/
[[email protected] roles]# ls
mariadb
[[email protected] roles]# mkdir zabbix-server/{defaults,files,handlers,meta,tasks,templates,vars} -p   #zabbix-server目录的名字随意给,因为搭建的是zabbix-server服务,所以这里创建的目录的名字为zabbix-server



[[email protected] roles]# cd zabbix-server/files
[[email protected] files]# vim zabbix.repo   #zabbix.repo文件的名字随意给,但是必须以.repo结尾
[zabbix]
name=zabbix4.0
baseurl=http://172.25.83.83/4.0
gpgcheck=0
enabled=1



[[email protected] files]# cd ../tasks/
[[email protected] tasks]# vim main.yml   #main.yml文件的名字是固定的,不要随意改
- name: copy zabbix.repo
  copy: src=zabbix.repo dest=/etc/yum.repos.d/zabbix.repo

- name: install zabbix-server zabbix-agent
  yum: name=zabbix-server,zabbix-agent state=present
  notify: "init zabbix db"

- name: config zabbix server
  copy: src=zabbix_server.conf dest=/etc/zabbix/zabbix_server.conf
  notify: restart zabbix server

- name: start zabbix-server zabbix-agent
  service: name={{ item }} state=started
  with_items:
    - zabbix-server
    - zabbix-agent


[[email protected] tasks]# cd ../handlers/
[[email protected] handlers]# vim main.yml
- name: create database
  mysql_db: name=zabbix state=present
  listen: "init zabbix db"

- name: create zabbix user
  mysql_user: name=zabbix password=zabbix priv=zabbix.*:ALL state=present
  listen: "init zabbix db"

- name: import create.sql.gz
  mysql_db: name=zabbix state=import target=/usr/share/doc/zabbix-server-mysql-4.0.5/create.sql.gz
  listen: "init zabbix db"

- name: restart zabbix server
  service: name=zabbix-server state=restarted

 

[[email protected] files]# scp zabbix.repo server2:/etc/yum.repos.d/zabbix.repo   #将yum源发送给server2,以便在server2端安装zabbix-server

[[email protected] ~]# yum install zabbix-server -y   #在server2端先安装zabbix-server,以生成所需的zabbix_server.conf文件
[[email protected] ~]# scp /etc/zabbix/zabbix_server.conf server1:/etc/ansible/roles/zabbix-server/files   #将zabbix_server.conf文件发送到server1端的/etc/ansible/roles/zabbix-server/files目录下

[[email protected] ~]# cd /etc/ansible/roles/zabbix-server/files/
[[email protected] files]# ls
zabbix.repo  zabbix_server.conf
[[email protected] files]# vim zabbix_server.conf   #修改zabbix_server.conf文件中的数据库的密码
124 DBPassword=zabbix   #将密码修改为之前在文件中写的,创建的zabbix用户对应的密码

 

4、.yml文件的编写

[[email protected] handlers]# cd /etc/ansible/
[[email protected] ansible]# vim zabbix-server.yml   #该文件的名字随意给,该文件在哪个目录下创建也可以
---
#zabbix-server部署
- hosts: server2
  roles:
    - mariadb   #/etc/ansible/roles目录中创建的mariadb目录
    - zabbix-server   #/etc/ansible/roles目录中创建的zabbix-server目录

 

5、测试

[[email protected] ~]# cd /etc/ansible/
[[email protected] ansible]# ls
ansible.cfg  hosts  index.html  playbooks  roles  zabbix-server.yml
[[email protected] ansible]# ansible-playbook zabbix-server.yml --syntax-check
[[email protected] ansible]# ansible-playbook zabbix-server.yml -C
[[email protected] ansible]# ansible-playbook zabbix-server.yml 
  • 在server2端查看zabbix-server(10051),zabbix-agent(10050),mariadb(3306)的监听端口是否存在

Linux下利用自动化运维工具Ansible自动化部署zabbix

从上图中,我们发现没有zabbix-server的监听端口,表示这是错误的,那么到底哪里出错了呢?让我们一起来查看一下zabbix-server的日志

[[email protected] ~]# cat /var/log/zabbix/zabbix_server.log

Linux下利用自动化运维工具Ansible自动化部署zabbix

从图中,我们可以看到,数据库连接失败,让我们一起来排一下错。

  1. 查看数据库zabbix是否创建成功
  2. 查看zabbix用户是否创建成功并授权成功
  3. 查看/etc/zabbix/zabbix-server.conf中的DBPassword是否已经更改

Linux下利用自动化运维工具Ansible自动化部署zabbix

Linux下利用自动化运维工具Ansible自动化部署zabbix

Linux下利用自动化运维工具Ansible自动化部署zabbix

从图中分析,我们可以知道数据库zabbix没有创建成功,那到底是为什么?查看一下文件的编写。我们发现,安装完zabbix-server和zabbix-agent之后才会触发创建zabbix数据库的触发器。所以可能是因为,并没有触发触发器。那为什么没有触发触发器呢?原因是可能server2之前安装过zabbix-server和zabbix-agent服务,所以导致没有再重复安装zabbis-server和zabbix-agent服务,进而导致没有触发触发器。那么解决办法就是,删除server2端的zabbix-server和zabbix-agent服务(或者只删除一个,因为只要能触发触发器就可以)。删除之后,再次进行测试。

#在server2端删除zabbix-server和zabbix-agent
[[email protected] ~]# yum remove zabbix-server -y
[[email protected] ~]# yum remove zabbix-agent -y
  • 在server2端查看zabbix-server(10051),zabbix-agent(10050),mariadb(3306)的监听端口是否存在

Linux下利用自动化运维工具Ansible自动化部署zabbix

从上图我们可以看到zabbix-server的监听端口10051,zabbix-agent的监听端口10050和mysql的监听端口3306存在。表示配置成功。

  • 我们再次查看zabbix-server的日志,发现已经没有错误
[[email protected] ~]# cat /var/log/zabbix/zabbix_server.log

Linux下利用自动化运维工具Ansible自动化部署zabbix