Ansible部署Zabbix监控工具

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ansible部署Zabbix监控工具相关的知识,希望对你有一定的参考价值。

[[email protected] roles]# pwd

/etc/ansible/roles

[[email protected] roles]# ls

ansible_zabbix.tar.gz  zabbix

[[email protected] roles]# ll zabbix/

总用量 16

drwxr-xr-x. 2 root root 4096 5月  25 17:15 group_vars

drwxr-xr-x. 8 root root 4096 5月  26 17:25 roles

-rw-r--r--. 1 root root   14 5月  27 11:05 zabbix.retry

-rw-r--r--. 1 root root  140 5月  27 15:29 zabbix.yaml


.角色与包含 

├── ansible_zabbix.tar.gz

└── zabbix

    ├── group_vars

    │?? └── zabbix

    ├── roles

    │?? ├── java

    │?? │?? ├── files

    │?? │?? │?? ├── java.sh

    │?? │?? │?? └── jdk-8u101-linux-x64.tar.gz

    │?? │?? ├── handlers

    │?? │?? │?? └── main.yml

    │?? │?? └── tasks

    │?? │??     └── main.yml

    │?? ├── mysql

    │?? │?? ├── files

    │?? │?? │?? ├── cmake-2.8.10.2.tar.gz

    │?? │?? │?? └── mysql-5.6.24.tar.gz

    │?? │?? ├── handlers

    │?? │?? │?? └── main.yml

    │?? │?? └── tasks

    │?? │??     └── main.yml

    │?? ├── nginx

    │?? │?? ├── files

    │?? │?? │?? ├── nginx

    │?? │?? │?? ├── nginx-1.10.2.tar.gz

    │?? │?? │?? ├── openssl-1.0.2f.tar.gz

    │?? │?? │?? ├── pcre-8.38.tar.gz

    │?? │?? │?? └── zlib-1.2.8.tar.gz

    │?? │?? ├── handlers

    │?? │?? │?? └── main.yml

    │?? │?? ├── tasks

    │?? │?? │?? └── main.yml

    │?? │?? └── templates

    │?? │??     └── nginx.conf.j2

    │?? ├── php

    │?? │?? ├── files

    │?? │?? │?? ├── index.php

    │?? │?? │?? ├── libiconv-1.13.1.tar.gz

    │?? │?? │?? ├── libmcrypt-2.5.8.tar.gz

    │?? │?? │?? ├── Makefile

    │?? │?? │?? ├── mhash-0.9.9.9.tar.gz

    │?? │?? │?? ├── php-5.6.29.tar.gz

    │?? │?? │?? ├── php-fpm

    │?? │?? │?? ├── php-fpm.conf

    │?? │?? │?? ├── php.ini

    │?? │?? │?? └── testdb.php

    │?? │?? ├── handlers

    │?? │?? │?? └── main.yml

    │?? │?? └── tasks

    │?? │??     └── main.yml

    │?? ├── yum

    │?? │?? ├── files

    │?? │?? │?? └── CentOS-Base.repo

    │?? │?? └── tasks

    │?? │??     └── main.yml

    │?? └── zabbix_server

    │??     ├── files

    │??     │?? ├── zabbix

    │      │  ├── zabbix-3.0.4.tar.gz

    │      │  ├── zabbix_agentd

    │      │  ├── zabbix_server

    │      │  └── zabbix_server.conf

    │      ├── handlers

    │      │   └── main.yml

    │      ├── tasks

    │      │   └── main.yml

    │      └── templates

    │           └── zabbix_agentd.conf.j2

    ├── zabbix.retry

    └── zabbix.yaml


定义变量

[[email protected] roles]# cd zabbix/

[[email protected] zabbix]# cat group_vars/zabbix 

java_path: /etc/ansible/roles/zabbix/roles/java/files/

nginx_path: /etc/ansible/roles/zabbix/roles/nginx/files/

mysql_path: /etc/ansible/roles/zabbix/roles/mysql/files/

php_path: /etc/ansible/roles/zabbix/roles/php/files/

zabbix_server_path: /etc/ansible/roles/zabbix/roles/zabbix_server/files/

zabbix_agent_path: /etc/ansible/roles/zabbix/roles/zabbix_agent/files/

dest_path: /usr/local/src/


主yaml文件

[[email protected] zabbix]# cat zabbix.yaml 

---

- hosts: zabbix

  roles:

    - role: yum

    - role: java

    - role: nginx

    - role: mysql

    - role: php

    - role: zabbix_server


role里面的文件内容

[[email protected] roles]# pwd

/etc/ansible/roles/zabbix/roles

[[email protected] roles]# ll

总用量 24

drwxr-xr-x. 5 root root 4096 5月  23 18:14 java

drwxr-xr-x. 5 root root 4096 5月  23 11:46 mysql

drwxr-xr-x. 6 root root 4096 5月  25 21:39 nginx

drwxr-xr-x. 5 root root 4096 5月  24 10:25 php

drwxr-xr-x. 4 root root 4096 5月  19 05:32 yum

drwxr-xr-x. 6 root root 4096 5月  27 16:06 zabbix_server 


Yum部署

[[email protected] yum]# pwd

/etc/ansible/roles/zabbix/roles/yum

[[email protected] yum]# tree

.

├── files

│   └── CentOS-Base.repo

└── tasks

    └── main.yml


2 directories, 2 files


[[email protected] yum]# cat tasks/main.yml 

---

# tasks file for yum

- name: copy the repo 

  copy: src=CentOS-Base.repo dest=/etc/yum.repos.d/CentOS-Base.repo


- name: clean cache

  shell: yum clean all;yum makecache


- name: install software

  yum: name=gcc,gcc-c++,make,zlib-devel,ncurses-devel,libxml2,libxml2-devel,libjpeg-devel,libpng-devel,freetype,openldap-devel,openldap,openssl,openssl-devel,pcre,pcre-devel,curl-devel,freetype-devel,net-snmp-devel,mysql-devel state=latest


Java部署

[[email protected] java]# tree

.

├── files

│   ├── java.sh

│   └── jdk-8u101-linux-x64.tar.gz

├── handlers

│   └── main.yml

└── tasks

    └── main.yml


3 directories, 4 files


[email protected] java]# cat tasks/main.yml 

---

# tasks file for java

- name: jie ya jdk-8u101-linux-x64.tar.gz

  unarchive: src={{ java_path }}jdk-8u101-linux-x64.tar.gz dest={{ dest_path }} copy=yes


- name: move jdk

  shell: mv /usr/local/src/jdk1.8.0_101 /usr/local/jdk


- name: add bianliang

  copy: src=java.sh dest=/etc/profile.d/


  #lineinfile: dest=/etc/profile line={{ item }}

  #with_items:

  #  - export JRE_HOME=/usr/local/jdk

  #  - export JAVA_BIN=/usr/local/jdk/bin

  #  - export PATH=$JRE_HOME/bin:$PATH

  #  - export CLASSPATH=.:$JRE_HOME/lib/dt.jar:$JRE_HOME/lib/tools.jar

  #  - export JRE_HOME JAVA_BIN PATH CLASSPATH


- name: source profile

  shell: source /etc/profile.d/java.sh


#  notify:

#    - add bianliang

#    - source profile



[[email protected] java]# cat handlers/main.yml 

---

# handlers file for java

- name: add bianliang

  lineinfile: dest=/etc/profile line={{ item }}

  with_items:

    - export JRE_HOME=/usr/local/jdk

    - export JAVA_BIN=/usr/local/jdk/bin

    - export PATH=$JRE_HOME/bin:$PATH

    - export CLASSPATH=.:$JRE_HOME/lib/dt.jar:$JRE_HOME/lib/tools.jar

    - export JRE_HOME JAVA_BIN PATH CLASSPATH


- name: source profile

  shell: source /etc/profile



Nginx部署

[[email protected] nginx]# tree

.

├── files

│   ├── nginx

│   ├── nginx-1.10.2.tar.gz

│   ├── openssl-1.0.2f.tar.gz

│   ├── pcre-8.38.tar.gz

│   └── zlib-1.2.8.tar.gz

├── handlers

│   └── main.yml

├── tasks

│   └── main.yml

└── templates

    └── nginx.conf.j2


4 directories, 8 files

[[email protected] nginx]#

[[email protected] tasks]# pwd

/etc/ansible/roles/zabbix/roles/nginx/tasks

[[email protected] tasks]# cat main.yml 

---

- name: useradd www

  user: name=www shell=/sbin/nologin createhome=no


- name: openssl-1.0.2f.tar.gz package

  unarchive: src={{ nginx_path }}openssl-1.0.2f.tar.gz dest={{ dest_path }} copy=yes


- name: pcre-8.38.tar.gz package

  unarchive: src={{ nginx_path }}pcre-8.38.tar.gz dest={{ dest_path }} copy=yes


- name: zlib-1.2.8.tar.gz

  unarchive: src={{ nginx_path }}zlib-1.2.8.tar.gz dest={{ dest_path }} copy=yes


- name: nginx-1.10.2.tar.gz package

  unarchive: src={{ nginx_path }}nginx-1.10.2.tar.gz dest={{ dest_path }} copy=yes


- name: nginx config

  shell: cd {{ dest_path }}nginx-1.10.2;./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v2_module --with-ipv6 --with-pcre={{ dest_path }}pcre-8.38 --with-zlib={{ dest_path }}zlib-1.2.8 --with-openssl={{ dest_path }}openssl-1.0.2f


- name: nginx make and install

  shell: cd {{ dest_path }}nginx-1.10.2;make;make install


- name: create cache nginx

  file: path=/var/cache/nginx state=directory owner=root group=root mode=0644


- name: copy config file

  template: src=nginx.conf.j2 dest=/usr/local/nginx/nginx.conf owner=root group=root mode=0644


- name: add nginx grant

  shell: cd /usr/local/nginx/;chown -R www:www *

 

- name: copy start file

  copy: src=nginx dest=/etc/init.d/ owner=root group=root mode=0755 backup=yes

  notify: 

    - start nginx service 

  tags: nginx


[[email protected] handlers]# pwd

/etc/ansible/roles/zabbix/roles/nginx/handlers

[[email protected] handlers]# cat main.yml 

---

- name: start nginx service 

  service: name=nginx state=restarted enabled=yes



[[email protected] templates]# pwd

/etc/ansible/roles/zabbix/roles/nginx/templates

[[email protected] templates]# cat nginx.conf.j2 

user www;

worker_processes {{ ansible_processor_cores }};

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {

        listen       80;

        server_name  localhost;

        location / {

            root   html;

            index  index.php index.html index.htm;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

location ~ \.php$ {

            root           html;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

            include        fastcgi_params;

        }

    }

}


MySQL部署

[[email protected] mysql]# pwd

/etc/ansible/roles/zabbix/roles/mysql

[[email protected] mysql]# tree

.

├── files

│   ├── cmake-2.8.10.2.tar.gz

│   └── mysql-5.6.24.tar.gz

├── handlers

│   └── main.yml

└── tasks

    └── main.yml


3 directories, 4 files


[[email protected] mysql]# cat tasks/main.yml 

---

# tasks file for mysql

- name: mysql-5.6.24.tar.gz package

  unarchive: src={{ mysql_path }}mysql-5.6.24.tar.gz dest={{ dest_path }} copy=yes


- name: cmake-2.8.10.2.tar.gz package

  unarchive: src={{ mysql_path }}cmake-2.8.10.2.tar.gz dest={{ dest_path }} copy=yes


- name: install camke

  shell: cd {{ dest_path }}cmake-2.8.10.2;./bootstrap --prefix=/usr/local/cmake;make;make install


- name: add user

  shell: id mysql || useradd -M -s /sbin/nologin mysql


- name: add quanxian

  file: path=/tmp mode=777


- name: bianyi

  shell: cd {{ dest_path }}mysql-5.6.24;/usr/local/cmake/bin/cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DMYSQL_DATADIR=/usr/local/mysql/data -DMYSQL_TCP_PORT=3306 -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysqld.sock -DMYSQL_USER=mysql -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_EMBEDDED_SERVER=1 -DENABLED_LOCAL_INFILE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_DEBUG=0


- name: make and make install

  shell: cd {{ dest_path }}mysql-5.6.24;make;make install


- name: shou quna 

  shell: chown -R mysql:mysql /usr/local/mysql


- name: line

  shell: ln -s /usr/local/mysql/bin/{{ item }} /usr/sbin/

  with_items:

    - innochecksum

    - msql2mysql

    - myisamchk

    - myisam_ftdump

    - myisamlog

    - myisampack

    - my_print_defaults

    - mysql

    - mysqlaccess

    - mysqlaccess.conf

    - mysqladmin

    - mysqlbinlog

    - mysqlbug

    - mysqlcheck

    - mysql_client_test

    - mysql_client_test_embedded

    - mysql_config

    - mysql_config_editor

    - mysql_convert_table_format

    - mysqld

    - mysqld_multi

    - mysqld_safe

    - mysqldump

    - mysqldumpslow

    - mysql_embedded

    - mysql_find_rows

    - mysql_fix_extensions

    - mysqlhotcopy

    - mysqlimport

    - mysql_plugin

    - mysql_secure_installation

    - mysql_setpermission

    - mysqlshow

    - mysqlslap

    - mysqltest

    - mysqltest_embedded

    - mysql_tzinfo_to_sql

    - mysql_upgrade

    - mysql_waitpid

    - mysql_zap

    - perror

    - replace

    - resolveip

    - resolve_stack_dump

  tags:

    - link


- name: copy mysql start script

  shell: cp {{ dest_path }}mysql-5.6.24/support-files/mysql.server /etc/init.d/mysqld


- name: insert ld.so.conf

  lineinfile: dest=/etc/ld.so.conf line=/usr/local/mysql/lib/


- name: ldconfig

  shell: ldconfig


- name: chu shi hua 

  shell: /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data


- name: copy my.cnf

  shell: rm -rf /etc/my.cnf;mv /usr/local/mysql/my.cnf /etc/


- name: add max_connections

  lineinfile: dest=/etc/my.cnf line="max_connections = 1000"


#- name: insert /var/lib/mysql/mysql.sock

#  lineinfile:  dest=/etc/my.cnf line="socket = /var/lib/mysql/mysql.sock"

#  tags: 

#    - socket


- name: start mysql service

  service: name=mysqld state=restarted enabled=yes

  tags:

    - mysqlstart


- name: set mysql passwd

  shell: /usr/local/mysql/bin/mysqladmin -u root password ‘mysql_dbpass‘

  tags:

    - mysqlpasswd


[[email protected] mysql]# cat handlers/main.yml 

---

# handlers file for mysql

- name: start mysql service

  service: name=mysqld state=restarted enabled=yes


- name: set mysql passwd

  shell: /usr/local/mysql/bin/mysqladmin -u root password ‘mysql_dbpass‘


PHP部署

[[email protected] php]# pwd

/etc/ansible/roles/zabbix/roles/php

[[email protected] php]# tree

.

├── files

│   ├── index.php

│   ├── libiconv-1.13.1.tar.gz

│   ├── libmcrypt-2.5.8.tar.gz

│   ├── Makefile

│   ├── mhash-0.9.9.9.tar.gz

│   ├── php-5.6.29.tar.gz

│   ├── php-fpm

│   ├── php-fpm.conf

│   ├── php.ini

│   └── testdb.php

├── handlers

│   └── main.yml

└── tasks

    └── main.yml


3 directories, 12 files

[[email protected] php]# cat tasks/main.yml 

---

# tasks file for php

- name: mhash-0.9.9.9.tar.gz

  unarchive: src={{ php_path }}mhash-0.9.9.9.tar.gz dest={{ dest_path }} copy=yes


- name: libiconv-1.13.1.tar.gz

  unarchive: src={{ php_path }}libiconv-1.13.1.tar.gz dest={{ dest_path }} copy=yes


- name: libmcrypt-2.5.8.tar.gz

  unarchive: src={{ php_path }}libmcrypt-2.5.8.tar.gz dest={{ dest_path }} copy=yes


- name: php-5.6.29.tar.gz

  unarchive: src={{ php_path }}php-5.6.29.tar.gz dest={{ dest_path }} copy=yes


- name: config mhash

  shell: cd {{ dest_path }}mhash-0.9.9.9;./configure;make;make install


- name: config libiconv

  shell: cd {{ dest_path }}libiconv-1.13.1;./configure;make;make install


- name: config libmcrypt

  shell: cd {{ dest_path }}libmcrypt-2.5.8;./configure;make;make install


- name: config libmcrypt-2.5.8/libltdl

  shell: cd {{ dest_path }}libmcrypt-2.5.8/libltdl/;./configure --with-gmetad --enable-gexec --enable-ltdl-install


- name: libmcrypt make and make install

  shell: cd {{ dest_path }}libmcrypt-2.5.8/libltdl/;make;make install


- name: line libmcrypt*

  file: src=/usr/local/lib/{{ item.src }} dest=/usr/lib/{{ item.dest }} state=link

  with_items:

    - { src: ‘libmcrypt.so.4.4.8‘, dest: ‘libmcrypt.so.4.4.8‘ }

    - { src: ‘libmcrypt.so.4‘, dest: ‘libmcrypt.so.4‘ }

    - { src: ‘libmcrypt.so‘, dest: ‘libmcrypt.so‘ }

    - { src: ‘libmcrypt.la‘, dest: ‘libmcrypt.la‘ }

    - { src: ‘libmcrypt‘, dest: ‘libmcrypt‘ }

    - { src: ‘libmhash.a‘, dest: ‘libmhash.a‘ }

    - { src: ‘libmhash.la‘, dest: ‘libmhash.la‘ }

    - { src: ‘libmhash.so‘, dest: ‘libmhash.so‘ }

    - { src: ‘libmhash.so.2‘, dest: ‘libmhash.so.2‘ }

    - { src: ‘libmhash.so.2.0.1‘, dest: ‘libmhash.so.2.0.1‘ }


- name: ldconfig

  shell: ldconfig


- name: cp libladp*

  file: src=/usr/lib64/{{ item.src }} dest=/usr/lib/{{ item.dest }} state=link

  with_items:

    - { src: ‘libldap-2.4.so.2‘, dest: ‘libldap-2.4.so.2‘ }

    - { src: ‘libldap_r-2.4.so.2‘, dest: ‘libldap_r-2.4.so.2‘ }

    - { src: ‘libldap_r.so‘, dest: ‘libldap_r.so‘ }

    - { src: ‘libldap.so‘, dest: ‘libldap.so‘ }


- name: config php

  shell: cd {{ dest_path }}php-5.6.29;./configure  --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --with-freetype-dir --with-gettext --with-fpm-user=www --with-fpm-group=www


- name: copy Makefile 

  copy: src=Makefile dest={{ dest_path }}php-5.6.29 owner=root group=root mode=0644 backup=yes


#- name: config Makefile

#  shell: sed -i ‘s#EXTRA_LIBS = -lcrypt -lz -lcrypt -lrt -lmysqlclient -lmcrypt -lltdl -lldap -lpng -lz -ljpeg -lcurl -lz -lrt -lm -ldl -lnsl -lrt -lxml2 -lz -lm -lssl -lcrypto -lcurl -lxml2 -lz -lm -lfreetype -lmysqlclient -lm -lrt -lssl -lcrypto -ldl -lxml2 -lz -lm -lxml2 -lz -lm -lcrypt -lxml2 -lz -lm -lxml2 -lz -lm -lxml2 -lz -lm -lxml2 -lz -lm -lcrypt#EXTRA_LIBS = -lcrypt -lz -lcrypt -lrt -lmysqlclient -lmcrypt -lltdl -lldap -lpng -lz -ljpeg -lcurl -lz -lrt -lm -ldl -lnsl -lrt -lxml2 -lz -lm -lssl -lcrypto -lcurl -lxml2 -lz -lm -lfreetype -lmysqlclient -lm -lrt -lssl -lcrypto -ldl -lxml2 -lz -lm -lxml2 -lz -lm -lcrypt -lxml2 -lz -lm -lxml2 -lz -lm -lxml2 -lz -lm -lxml2 -lz -lm -lcrypt -liconv#g‘ {{ dest_path }}php-5.6.29/Makefile


- name: make ZEND_EXTRA_LIBS=‘-liconv‘

  shell: cd {{ dest_path }}php-5.6.29;make ZEND_EXTRA_LIBS=‘-liconv‘


- name: make install

  shell: cd {{ dest_path }}php-5.6.29;make install


- name: copy file php.ini

  copy: src=php.ini dest=/usr/local/php/etc/ owner=root group=root mode=0644 backup=yes


- name: copy file php-fpm.conf

  copy: src=php-fpm.conf dest=/usr/local/php/etc/ owner=root group=root mode=0644 backup=yes


- name: copy php-fpm 

  copy: src=php-fpm dest=/etc/init.d/ owner=root group=root mode=0755 backup=yes


- name: copy file index.php testdb.php 

  copy: src={{ item }} dest=/usr/local/nginx/html/ owner=root group=root mode=0644 backup=yes

  with_items:

    - index.php

    - testdb.php

  notify:

    - start php  


[[email protected] php]# cat handlers/main.yml 

---

# handlers file for php

- name: start php

  service: name=php-fpm state=restarted enabled=yes


Zabbix部署

[[email protected] zabbix_server]# pwd

/etc/ansible/roles/zabbix/roles/zabbix_server

[[email protected] zabbix_server]# ls

handlers  tasks  templates

[[email protected] zabbix_server]# tree

├── files

│   ├── zabbix

│   ├── zabbix-3.0.4.tar.gz

│   ├── zabbix_agentd

│   ├── zabbix_server

│   └── zabbix_server.conf

├── handlers

│   └── main.yml

├── tasks

│   └── main.yml

└── templates

    └── zabbix_agentd.conf.j2




[[email protected] zabbix_server]# cat tasks/main.yml 

---

# tasks file for zabbix_server

- name: add usergroup

  group: name=zabbix gid=201 system=yes

  tags:

    - zabbix_1  


- name: add user 

  user: name=zabbix uid=201 group=zabbix

  tags:

    - zabbix_2


- name: unarchive zabbix-3.0.4.tar.gz

  unarchive: src={{ zabbix_server_path }}zabbix-3.0.4.tar.gz dest={{ dest_path }} copy=yes

  tags:

    - zabbix_3


- name: config and make install

  shell: cd {{ dest_path }}zabbix-3.0.4;./configure --prefix=/usr/local/zabbix --enable-java --enable-server --enable-proxy --enable-agent --with-mysql --with-net-snmp --with-libcurl --with-libxml2;make install

  tags:

    - zabbix_4


- name: add zabbix to services

  lineinfile: dest=/etc/services line={{ item }}

  with_items:

    - "zabbix-agent    10050/tcp               #Zabbix Agent"

    - "zabbix-agent    10050/udp               #Zabbix Agent"

    - "zabbix-server   10051/tcp               #zabbix Trapper"

    - "zabbix-server   10051/udp               #zabbix Trapper"

  tags: 

    - zabbix_4_1


- name: create database user

  shell: mysql -uroot -pmysql_dbpass -e "create database zabbix default character set utf8;"

  tags:

    - zabbix_5


- name: databases shou quan

  shell: mysql -uroot -pmysql_dbpass -e "grant all on zabbix.* to ‘zabbix‘@‘localhost‘ identified by ‘zabbix‘;"

  tags:

    - zabbix_6


- name: flush privileges

  shell: mysql -uroot -pmysql_dbpass -e "flush privileges;"

  tags:

    - zabbix_7


- name: import databases

  shell: cd {{dest_path }}zabbix-3.0.4/database/mysql;mysql -uzabbix -pzabbix zabbix < schema.sql;mysql -uzabbix -pzabbix zabbix < images.sql;mysql -uzabbix -pzabbix zabbix < data.sql

  tags:

    - zabbix_8


- name: create zabbix log directory 

  file: path=/data/logs/zabbix/ state=directory owner=zabbix group=zabbix

  tags:

    - zabbix_9


- name: create /etc/ zabbix directory

  file: path=/etc/zabbix/ state=directory

  tags:

    - zabbix_10


- name: create /usr/local/zabbix/etc link

  file: src=/usr/local/zabbix/etc/{{ item.src }} dest=/etc/zabbix/{{ item.dest }} state=link

  with_items:

    - { src: ‘zabbix_agentd.conf‘, dest: ‘zabbix_agentd.conf‘ }

    - { src: ‘zabbix_proxy.conf‘, dest: ‘zabbix_proxy.conf‘ }

    - { src: ‘zabbix_server.conf‘, dest: ‘zabbix_server.conf‘ }

    - { src: ‘zabbix_agentd.conf.d‘, dest: ‘zabbix_agentd.conf.d‘ }

    - { src: ‘zabbix_proxy.conf.d‘, dest: ‘zabbix_proxy.conf.d‘ }

    - { src: ‘zabbix_server.conf.d‘, dest: ‘zabbix_server.conf.d‘ }

  tags:

    - zabbix_11


- name: create /usr/local/zabbix/bin link

  file: src=/usr/local/zabbix/bin/{{ item.src }} dest=/etc/zabbix/{{ item.dest }} state=link

  with_items:

    - { src: ‘zabbix_get‘, dest: ‘zabbix_get‘ }

    - { src: ‘zabbix_sender‘, dest: ‘zabbix_sender‘ }

  tags:

    - zabbix_12


- name: create /usr/local/zabbix/sbin link

  file: src=/usr/local/zabbix/sbin/{{ item.src }} dest=/usr/sbin/{{ item.dest }} state=link

  with_items:

    - { src: ‘zabbix_agentd‘, dest: ‘zabbix_agentd‘ }

    - { src: ‘zabbix_java‘, dest: ‘zabbix_java‘ }

    - { src: ‘zabbix_proxy‘, dest: ‘zabbix_proxy‘ }

    - { src: ‘zabbix_server‘, dest: ‘zabbix_server‘ }

  tags:

    - zabbix_13


- name: copy zabbix start stop script

  copy: src={{ item }} dest=/etc/init.d owner=root group=root mode=0755 backup=yes

  with_items:

    - zabbix_server

    - zabbix_agentd

  tags:

    - zabbix_14


- name: copy zabbix_server.conf

  #file: src={{ item }} dest=/usr/local/zabbix/etc/ owner=root group=root mode=0644 backup=yes

  synchronize: src={{ item }} dest=/usr/local/zabbix/etc/  mode=push

  with_items:

    - zabbix_server.conf

  tags:

    - zabbix_15


- name: copy zabbix_agentd.conf

  template: src=zabbix_agentd.conf.j2 dest=/usr/local/zabbix/etc/zabbix_agentd.conf owner=root group=root mode=0644

  tags:

    - zabbix_16


- name: copy zabbix wangzhan directory

  synchronize: src=zabbix dest=/usr/local/nginx/html/ mode=push

  tags:

    - zabbix_17


#- name: change user

#  file: dest=/usr/local/nginx/html/zabbix owner=www group=www backup=yes


- name: add /usr/local/lib

  lineinfile: dest=/etc/ld.so.conf line=/usr/local/lib

  tags:

    - zabbix_19


#- name: add max_connections

#  lineinfile: dest=/etc/my.cnf line="max_connections = 1000"


- name: ldconfig

  shell: ldconfig;

  tags:

    - zabbix_20


  notify:

    - start zabbix_server

    - start zabbix_agentd

    - stop iptables

    - stop selinux


[[email protected] zabbix_server]# cat handlers/main.yml 

---

# handlers file for zabbix_server

- name: start zabbix_server

  service: name=zabbix_server state=restarted enabled=yes


- name: start zabbix_agentd

  service: name=zabbix_agentd state=restarted enabled=yes


- name: stop iptables

  service: name=iptables state=stopped enabled=no


- name: stop selinux

  shell: setenforce 0


[[email protected] zabbix_server]# cat templates/zabbix_agentd.conf.j2 

EnableRemoteCommands=1

UnsafeUserParameters=1

Server=127.0.0.1,{{ ansible_eth0.ipv4.address }}

ServerActive={{ ansible_eth0.ipv4.address }}:10051

Hostname={{ ansible_hostname }}

LogFile=/data/logs/zabbix/zabbix_agentd.log

Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/

Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/*.conf



执行Yaml主文件

[[email protected] zabbix]# ls

group_vars  roles  zabbix.retry  zabbix.yaml

[[email protected] zabbix]# pwd

/etc/ansible/roles/zabbix

[[email protected] zabbix]# ansible-playbook zabbix.yaml 



本文出自 “YHT的运维笔记” 博客,请务必保留此出处http://yht1990.blog.51cto.com/9014030/1930310

以上是关于Ansible部署Zabbix监控工具的主要内容,如果未能解决你的问题,请参考以下文章

通过ansible自动化部署zabbix应用

Ubuntu下Zabbix服务器监控工具部署

监控工具之zabbix server3.4 部署配置

CentOS 7.x下部署和配置zabbix 3.2.x监控工具

Ansible-playbook批量添加zabbix监控项目同步配置信息

Zabbix实战之部署篇Zabbix使用SNMP监控Linux系统