使用ansible的playbook脚本批量搭建部署zabbix6.0
部署环境:
一台control节点,两台node节点(均为stream8)
第一步,控制节点安装ansible(将之前的源删掉,全部替换成清华源,epel源里有ansible的安装包)
第二步,编辑ansible的配置文件和IP主机名解析(ansible_ssh_pass为远程被管理节点的root密码,在这里指定的话就不需要做免密登录)
第三步,查看当前ansible配置文件和测试control节点与node节点间的主机连通性
ping失败的原因是需要确认远程主机的指纹,因此我们修改ssh客户端配置文件并重启sshd服务即可
第四步,编写playbook文件
1 [control root zabbix]# cat zabbix.yml 2 --- 3 - name: zabbix basic config 4 hosts: all 5 tasks: 6 - name: close firewall 7 service: 8 name: firewalld 9 state: stopped 10 enabled: no 11 - name: close selinux 12 selinux: 13 policy: targeted 14 state: permissive 15 - name: remove old yum repo 16 file: 17 path: /etc/yum.repos.d/ 18 state: absent 19 - name: copy new yum repo to managed host 20 copy: 21 src: files/repo.repo 22 dest: /etc/yum.repos.d/ 23 - name: config zabbix db 24 hosts: all 25 tasks: 26 - name: install zabbix db 27 yum: 28 name: 29 - mariadb-server 30 - zabbix-server-mysql 31 - python3-PyMySQL 32 state: present 33 - name: start mariadb service 34 service: 35 name: mariadb 36 state: started 37 enabled: yes 38 - name: create zabbix db and set demo 39 mysql_db: 40 name: "{{ zabbix_db_name }}" 41 encoding: utf8 42 collation: utf8_bin 43 state: present 44 register: db 45 - name: create zabbix db user 46 mysql_user: 47 name: "{{ zabbix_db_name }}" 48 password: "{{ zabbix_db_password }}" 49 priv: "{{ zabbix_db_name }}.*:ALL" 50 state: present 51 host: '%' 52 - name: download sql scripts 53 yum: 54 name: zabbix-sql-scripts 55 state: present 56 - name: import db 57 mysql_db: 58 name: "{{ zabbix_db_name }}" 59 state: import 60 target: /usr/share/doc/zabbix-sql-scripts/mysql/server.sql.gz 61 when: db.changed == true 62 - name: config zabbix-server 63 hosts: all 64 tasks: 65 - name: install zabbix-server 66 yum: 67 name: zabbix-server-mysql 68 state: present 69 - name: set config file 70 template: 71 src: files/zabbix_server.conf.j2 72 dest: /etc/zabbix/zabbix_server.conf 73 - name: start zabbix-server 74 service: 75 name: zabbix-server 76 state: restarted 77 enabled: yes 78 - name: config zabbix-web 79 hosts: all 80 tasks: 81 - name: install zabbix-web 82 yum: 83 name: 84 - zabbix-web-mysql 85 - zabbix-nginx-conf 86 state: present 87 - name: config nginx file 88 template: 89 src: files/zabbix-nginx.conf.j2 90 dest: /etc/nginx/conf.d/zabbix.conf 91 - name: config php file 92 template: 93 src: files/zabbix-php.conf.j2 94 dest: /etc/php-fpm.d/zabbix.conf 95 - name: restart nginx service 96 service: 97 name: nginx 98 state: restarted 99 enabled: yes 100 - name: restart php service 101 service: 102 name: php-fpm 103 state: restarted 104 enabled: yeszabbix的playbook脚本
第五步,创建一个目录,在创建的目录里编辑模板文件,j2文件,变量文件
1 [control root zabbix]# cat files/zabbix-php.conf.j2 2 [zabbix] 3 user = apache 4 group = apache 5 6 listen = /run/php-fpm/zabbix.sock 7 listen.acl_users = apache,nginx 8 listen.allowed_clients = 127.0.0.1 9 10 pm = dynamic 11 pm.max_children = 50 12 pm.start_servers = 5 13 pm.min_spare_servers = 5 14 pm.max_spare_servers = 35 15 pm.max_requests = 200 16 17 php_value[session.save_handler] = files 18 php_value[session.save_path] = /var/lib/php/session 19 20 php_value[max_execution_time] = 300 21 php_value[memory_limit] = 128M 22 php_value[post_max_size] = 16M 23 php_value[upload_max_filesize] = 2M 24 php_value[max_input_time] = 300 25 php_value[max_input_vars] = 10000zabbix-php的j2模板配置文件
1 [control root zabbix]# cat files/zabbix-nginx.conf.j2 2 server { 3 listen 80; 4 server_name {{ ansible_default_ipv4.address }}; 5 6 root /usr/share/zabbix; 7 8 index index.php; 9 10 location = /favicon.ico { 11 log_not_found off; 12 } 13 14 location / { 15 try_files $uri $uri/ =404; 16 } 17 18 location /assets { 19 access_log off; 20 expires 10d; 21 } 22 23 location ~ /\.ht { 24 deny all; 25 } 26 27 location ~ /(api\/|conf[^\.]|include|locale) { 28 deny all; 29 return 404; 30 } 31 32 location /vendor { 33 deny all; 34 return 404; 35 } 36 37 location ~ [^/]\.php(/|$) { 38 fastcgi_pass unix:/run/php-fpm/zabbix.sock; 39 fastcgi_split_path_info ^(.+\.php)(/.+)$; 40 fastcgi_index index.php; 41 42 fastcgi_param DOCUMENT_ROOT /usr/share/zabbix; 43 fastcgi_param SCRIPT_FILENAME /usr/share/zabbix$fastcgi_script_name; 44 fastcgi_param PATH_TRANSLATED /usr/share/zabbix$fastcgi_script_name; 45 46 include fastcgi_params; 47 fastcgi_param QUERY_STRING $query_string; 48 fastcgi_param REQUEST_METHOD $request_method; 49 fastcgi_param CONTENT_TYPE $content_type; 50 fastcgi_param CONTENT_LENGTH $content_length; 51 52 fastcgi_intercept_errors on; 53 fastcgi_ignore_client_abort off; 54 fastcgi_connect_timeout 60; 55 fastcgi_send_timeout 180; 56 fastcgi_read_timeout 180; 57 fastcgi_buffer_size 128k; 58 fastcgi_buffers 4 256k; 59 fastcgi_busy_buffers_size 256k; 60 fastcgi_temp_file_write_size 256k; 61 } 62 }zabbix-nginx的j2模板配置文件
1 [root@control zabbix]# cat files/zabbix_server.conf.j2 2 LogFile=/var/log/zabbix/zabbix_server.log 3 LogFileSize=0 4 PidFile=/var/run/zabbix/zabbix_server.pid 5 SocketDir=/var/run/zabbix 6 DBHost={{ ansible_default_ipv4.address }} 7 DBName={{ zabbix_db_name }} 8 DBUser={{ zabbix_db_user }} 9 DBPassword={{ zabbix_db_password }} 10 SNMPTrapperFile=/var/log/snmptrap/snmptrap.log 11 Timeout=4 12 AlertScriptsPath=/usr/lib/zabbix/alertscripts 13 ExternalScripts=/usr/lib/zabbix/externalscripts 14 LogSlowQueries=3000 15 StatsAllowedIP=127.0.0.1zabbix-server的配置文件
最后一步,执行playbook脚本
执行成功后,效果图如下:
脚本执行成功后,访问两台node节点对应IP的web网页
到此,ansible批量部署zabbix完成。(如果条件允许,可多添加几台node节点进行实验)