1. 程式人生 > 其它 >使用ansible的playbook指令碼批量搭建部署zabbix6.0

使用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: yes
zabbix的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] = 10000
zabbix-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 [control root 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={{ hostvars.node2.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.1
zabbix-server的j2模板配置檔案

 最後一步,執行playbook指令碼

執行成功後,效果圖如下:

 指令碼執行成功後,訪問兩臺node節點對應IP的web網頁

 到此,ansible批量部署zabbix完成。(如果條件允許,可多新增幾臺node節點進行實驗)