ansible(二)例子:playbook部署zabbix

 src: my.cnf	##mysql主机
 src: create.sql.gz		##mysql主机
 src: zabbix_server.conf	##server主机
 src: zabbix.conf		##web主机
 src: zabbix_agentd.conf.j2	##server主机
playbook: zabbix/deploy.yml
play #1 (db): db      TAGS: []
tasks:
  install mariadb   TAGS: []
  config mariadb    TAGS: []
  start mariadb     TAGS: []
  create database zabbix    TAGS: []
  create user       TAGS: []
  copy create.sql   TAGS: []
  config firewalld  TAGS: []

 play #2 (server): server      TAGS: []
 tasks:
  add zabbix repo   TAGS: []
  add update repo   TAGS: []
  install zabbix-server     TAGS: []
  config zabbix-server      TAGS: []
  start zabbix-server       TAGS: []
  config firewalld  TAGS: []

 play #3 (web): web    TAGS: []
 tasks:
  add zabbix repo   TAGS: []
  add update repo   TAGS: []
  add centos repo   TAGS: []
  install zabbix-web        TAGS: []
  config zabbix-web TAGS: []
  start httpd       TAGS: []
  config firewalld  TAGS: []

play #4 (agent): agent        TAGS: []
tasks:
  add zabbix repo   TAGS: []
  install zabbix-agent      TAGS: []
  config zabbix-agent       TAGS: []
  start zabbix-agent        TAGS: []
  config firewalld  TAGS: []

做法

需要的准备

[devops@server1 ansible]$ ls 
ansible.cfg  hosts  mysql  ssh.yml  zabbix
[devops@server1 ansible]$ ls zabbix/
agent.yml      db.yml      my.cnf      web.yml                zabbix.conf
create.sql.gz  deploy.yml  server.yml  zabbix_agentd.conf.j2  	zabbix_server.conf

[devops@server1 ansible]$ cat hosts 
[db]
172.25.70.11

[server]
172.25.70.12

[web]
172.25.70.13

[agent]
172.25.70.12
172.25.70.13


[zz:children]
db
server
web

各主机的部署

db

---
- 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: create.sql.gz

sever

---
- 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

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

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

结果

[root@server3 ~]# cat /etc/zabbix/web/zabbix.conf.php
<?php
// Zabbix GUI configuration file.
global $DB;

$DB['TYPE']     = 'MYSQL';
$DB['SERVER']   = '172.25.70.11';
$DB['PORT']     = '3306';
$DB['DATABASE'] = 'zabbix';
$DB['USER']     = 'zabbix';
$DB['PASSWORD'] = 'zabbix';

// Schema name. Used for IBM DB2 and PostgreSQL.
$DB['SCHEMA'] = '';

$ZBX_SERVER      = '172.25.70.12';
$ZBX_SERVER_PORT = '10051';
$ZBX_SERVER_NAME = '';

$IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_PNG;

出现的问题

  • ansible主机的文件都需要从服务机拷贝
  • yml文件结构不清楚可以ansible-doc xxx
  • 倒入结构那步可能没写好

各文件具体配置

my.conf
在这里插入图片描述
zabbix.conf
在这里插入图片描述

zabbix_agent.conf
在这里插入图片描述
zabbix_server.conf
zabbix_server.conf
现象
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值