linux系统ansible工具剧本编写布置zabbix

zabbix剧本编写

      • 布置zabbix
        • zabbix.yaml
        • agent
        • server
        • mysql

布置zabbix

ansible-galaxy init agent
ansible-galaxy init mysql
ansible-galaxy init server
touch zabbix.yaml
zabbix.yaml
---
- hosts: web
  remote_user: root
  roles:
    - mysql 
    - server
- hosts: db
  remote_user: root
  roles:
    - agent
agent
vim vars/main.yml 

---
# vars file for agent
serverip: 10.12.153.118
vim tasks/main.yml 

---
# tasks file for agent
#- name: 安装源
#  shell: rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm  
# 
- name: 安装agent端
  yum: name=zabbix-agent state=present

- name: 拷贝配置文件
  template: src=zabbix_agentd.conf dest=/etc/zabbix/zabbix_agentd.conf 
vim templates/zabbix_agentd.conf
 
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server={{ serverip }}
ServerActive={{ serverip }}
Hostname={{ ansible_hostname }}
Include=/etc/zabbix/zabbix_agentd.d/*.conf
server
vim tasks/main.yml 

---
# tasks file for server

#- name: 安装server源
#  shell: rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
#
#- name: 安装工具
#  yum: name=zabbix-server-mysql,zabbix-agent,centos-release-scl state=present
#
#- name: 拷贝repo文件
#  copy: src=zabbix.repo dest=/etc/yum.repos.d/zabbix.repo backup=yes
#
#- name: 安装工具
#  yum: name=zabbix-web-mysql-scl,zabbix-nginx-conf-scl state=present
- name: 导入初始数据
  shell: zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz |mysql -uzabbix -p0 zabbix

- name: 修改数据库
  shell:  mysql -e "set global log_bin_trust_function_creators = 0;"
#- name: 拷贝server配置文件
#  copy: src=zabbix_server.conf dest=/etc/zabbix/zabbix_server.conf backup=yes
#
#- name: 拷贝php文件
#  copy: src=zabbix.conf dest=/etc/opt/rh/rh-nginx116/nginx/conf.d/zabbix.conf backup=yes
#
#- name: 拷贝php配置文件
#  copy: src=zabbix.con dest=/etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf backup=yes
#
#- name: 拷贝nginx配置文件
#  copy: src=nginx.conf dest=/etc/opt/rh/rh-nginx116/nginx/nginx.conf backup=yes

- name: 启动zabbix-server
  shell: systemctl start zabbix-server zabbix-agent rh-nginx116-nginx rh-php72-php-fpm
vim files/nginx.conf 

user nginx;
worker_processes auto;
error_log /var/opt/rh/rh-nginx116/log/nginx/error.log;
pid /var/opt/rh/rh-nginx116/run/nginx/nginx.pid;
include /opt/rh/rh-nginx116/root/usr/share/nginx/modules/*.conf;
events {
    worker_connections  1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/opt/rh/rh-nginx116/log/nginx/access.log  main;

    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;
    keepalive_timeout  65;
    types_hash_max_size 2048;
    include       /etc/opt/rh/rh-nginx116/nginx/mime.types;
    default_type  application/octet-stream;
    include /etc/opt/rh/rh-nginx116/nginx/conf.d/*.conf;
}
vim files/zabbix.con

[zabbix]
user = apache
group = apache

listen = /var/opt/rh/rh-php72/run/php-fpm/zabbix.sock
listen.acl_users = apache,nginx
listen.allowed_clients = 127.0.0.1

pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 200

php_value[session.save_handler] = files
php_value[session.save_path]    = /var/opt/rh/rh-php72/lib/php/session/

php_value[max_execution_time] = 300
php_value[memory_limit] = 128M
php_value[post_max_size] = 16M
php_value[upload_max_filesize] = 2M
php_value[max_input_time] = 300
php_value[max_input_vars] = 10000
php_value[date.timezone] = Asia/Shanghai
vim files/zabbix.conf 

server {
         listen          80;
         server_name     example.com;

        root    /usr/share/zabbix;

        index   index.php;

        location = /favicon.ico {
                log_not_found   off;
        }

        location / {
                try_files       $uri $uri/ =404;
        }

        location /assets {
                access_log      off;
                expires         10d;
        }

        location ~ /\.ht {
                deny            all;
        }

        location ~ /(api\/|conf[^\.]|include|locale|vendor) {
                deny            all;
                return          404;
        }

        location ~ [^/]\.php(/|$) {
                fastcgi_pass    unix:/var/opt/rh/rh-php72/run/php-fpm/zabbix.sock;
#                fastcgi_pass    unix:/var/opt/rh/rh-php73/run/php-fpm/zabbix.sock;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_index   index.php;

                fastcgi_param   DOCUMENT_ROOT   /usr/share/zabbix;
                fastcgi_param   SCRIPT_FILENAME /usr/share/zabbix$fastcgi_script_name;
                fastcgi_param   PATH_TRANSLATED /usr/share/zabbix$fastcgi_script_name;

                include fastcgi_params;
                fastcgi_param   QUERY_STRING    $query_string;
                fastcgi_param   REQUEST_METHOD  $request_method;
                fastcgi_param   CONTENT_TYPE    $content_type;
                fastcgi_param   CONTENT_LENGTH  $content_length;

                fastcgi_intercept_errors        on;
                fastcgi_ignore_client_abort     off;
                fastcgi_connect_timeout         60;
                fastcgi_send_timeout            180;
                fastcgi_read_timeout            180;
                fastcgi_buffer_size             128k;
                fastcgi_buffers                 4 256k;
                fastcgi_busy_buffers_size       256k;
                fastcgi_temp_file_write_size    256k;
        }
}
vim files/zabbix.repo 

[zabbix]
name=Zabbix Official Repository - $basearch
baseurl=http://repo.zabbix.com/zabbix/5.0/rhel/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591

[zabbix-frontend]
name=Zabbix Official Repository frontend - $basearch
baseurl=http://repo.zabbix.com/zabbix/5.0/rhel/7/$basearch/frontend
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591

[zabbix-debuginfo]
name=Zabbix Official Repository debuginfo - $basearch
baseurl=http://repo.zabbix.com/zabbix/5.0/rhel/7/$basearch/debuginfo/
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
gpgcheck=1

[zabbix-non-supported]
name=Zabbix Official Repository non-supported - $basearch
baseurl=http://repo.zabbix.com/non-supported/rhel/7/$basearch/
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
gpgcheck=1
vim files/zabbix_server.conf 

LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/var/run/zabbix/zabbix_server.pid
SocketDir=/var/run/zabbix
DBName=zabbix
DBUser=zabbix
DBPassword=0
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
Timeout=4
AlertScriptsPath=/usr/lib/zabbix/alertscripts
ExternalScripts=/usr/lib/zabbix/externalscripts
LogSlowQueries=3000
StatsAllowedIP=127.0.0.1
mysql
vim tasks/main.yml

---
 tasks file for mysql
- name: 清理环境
  yum: name=mysql*,mariadb* state=absent

- name: 清理环境
  file: path=/var/lib/mysql,/etc/my.cnf,/var/log/mysql*  state=absent
- name: 安装数据库
  yum: name=mariadb-server,mariadb state=present
- name: 启动数据库
  service: name=mariadb state=started enabled=yes
- name: 等待mariadb是否启动
  wait_for:
    host: localhost
    port: 3306
- name: 删除数据库
  shell:  mysql -e "drop database zabbix;"
- name: 创建数据库
  shell:  mysql -e "create database zabbix character set utf8 collate utf8_bin;set global log_bin_trust_function_creators = 1;grant all on *.* to 'zabbix'@'localhost' identified by '0';flush privileges"


#- name: 安装mysql源
#  shell: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-11.noarch.rpm

#- name: 安装mysql
#  yum: name=mysql-server disablerepo=mysql80-community enablerepo=mysql57-community state=present

你可能感兴趣的:(linux,linux,ansible,zabbix)