Ansible automated operation and maintenance - write Playbook achieve Zabbix automatically install deployment

First, a brief introduction zabbix

Zabbix is ​​a highly integrated network monitoring solution from a foreign team continued maintenance updates, the software is free to download. It includes a common business software includes monitoring functions (performance monitoring host network equipment performance monitoring, performance monitoring database, FTP, general protocol monitor, a variety of alarm mode, detailed reports diagramming) .Zabbix by C / S mode acquisitions data, and display web arranged at the end by B / S mode:

  • Client (monitored) end: data collected by the host agent installation mode, the network device collecting data through SNMP
  • Server (service) side: by collecting data sent from the agent and SNMP, and then written to the database through the web front end php + apache display.

Zabbix function

  • Supports automatic discovery of network devices and servers (server automatically discovered by the rules configured to implement)
  • Supports automatic discovery (low discovery) key to achieve mass monitoring dynamic monitoring items (need to write script)
  • Support distributed, to showcase, management of distributed monitoring points
  • Scalability, server provides a common interface (api function), can be improved and developed its own monitoring (realized in accordance with the relevant programming interfaces)

Second, write Playbook - Zabbix automatically install deployment

Hostname (ip) service
server1(172.25.2.1) ansible、mariadb
server2(172.25.2.2) The host control hosts, zabbix-server, zabbix-agent
server3(172.25.2.3) The host control hosts, web front end

1. Define Inventory file:vim /home/devops/ansible/hosts
Here Insert Picture Description

2, create zabbix directory, configuration files need to be stored: mkdir zabbix

3, write YAML file: vim deploy.yml

---
- hosts: mariadb 
		##对mariadb主机进行操作
  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  #创建数据库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: '%'   #%是匹配所有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   #名称必须与notify一致
      service:
        name: mariadb
        state: restarted

    - name: import create.sql  #将zabbix数据库初始化包导入数据库
      mysql_db:
        login_user: root
        login_password: westos
        name: zabbix
        state: import
        target: /tmp/create.sql.gz

- hosts: zabbix-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源,满足zabbix版本安装需求
      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  #添加zabbix服务端口
        permanent: yes
        immediate: yes
        state: enabled

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

- hosts: zabbix-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


- hosts: zabbix-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

Wrote the above configuration file /home/devops/ansible/zabbix/deploy.yml File Source:

  • Database Security initialization file my.cnf
    Mounted on server1 database mariadb-server, security initialization, then the generated files copied to /eth/my.cnf zabbix directory.
    Here Insert Picture Description

  • Zabbix database initialization packet:create.sql.gz
    Here Insert Picture Description
    In the /usr/share/doc/zabbix-server-mysql-4.0.5/, can be obtained create.sql.gz, before sending it to the server1 under devops / home / devops / ansible / zabbix /.

Here Insert Picture Description

  • zabbix_server.conf profile
    On server2, will be sent to server1 /etc/zabbix/zabbix_server.conf under devops, and modify the contents of the file i
 91 BHost=172.25.2.1
 116 DBUser=zabbix
 124 DBPassword= zabbix
 137 DBPort=3306
  • zabbix.conf file, Copy it to the next zabbix directory, and change the time zone
php_value date.timezone Asia/Shanghai
  • zabbix_agentd.conf file, Copy it to the next zabbix directory and change the file suffix instead zabbix_agentd.conf.j2, and modify files
Server=172.25.2.1
ServerActive=172.25.2.1
Hostname= {{ ansible_hostname }}

4, run the script

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

5, open the web interface search web host: http://172.25.2.3/zabbix
Here Insert Picture Description
Here Insert Picture Description

Published 102 original articles · won praise 21 · views 5323

Guess you like

Origin blog.csdn.net/ranrancc_/article/details/103263609