文章目录
待完善…
目录结构如下
[root@server1 srv]# pwd
/srv
[root@server1 srv]# ls
pillar salt
[root@server1 srv]# tree .
.
├── pillar
│ ├── top.sls
│ └── zabbix.sls
└── salt
├── mariadb
│ ├── files
│ │ └── create.sql.gz
│ └── install.sls
├── repos
│ └── zabbix.sls
├── top.sls
├── zabbix-agent
│ ├── files
│ │ └── zabbix_agentd.conf
│ └── install.sls
├── zabbix-server
│ ├── files
│ │ └── zabbix_server.conf
│ └── install.sls
└── zabbix-web
├── files
│ └── zabbix.conf
└── install.sls
11 directories, 12 files
各个文件的内容以及说明如下
mariadb的安装以及配置文件编写为如下:
[root@server1 salt]# cd mariadb/
[root@server1 mariadb]# ls
files install.sls
[root@server1 mariadb]# cat install.sls
db-install:
pkg.installed:
- pkgs:
- mariadb
- mariadb-server
- MySQL-python
service.running:
- name: mariadb-server
cmd.run:
- name: mysql -e "DELETE FROM mysql.user WHERE User='';" && "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');" && "DROP DATABASE IF EXISTS test;" && "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'" && "FLUSH PRIVILEGES;" #根据mariadb的初始化脚本编写
- onlyif : mysql -e "show databases;" #只有在执行show databases不报错时才进行安全初始化 -e 表示执行
mysql_user.present:
- name: root
- password: westos
db-configure:
file.managed:
- name: /mnt/create.sql.gz
- source: salt://mariadb/files/create.sql.gz
mysql_database.present:
- name: zabbix
- character_set: utf8
- collate: utf8_bin
- connection_user: root
- connection_pass: westos
mysql_user.present:
- name: zabbix
- host: localhost
- password: westos
- connection_user: root
- connection_pass: westos
mysql_grants.present: #授权
- grant: all privileges
- database: zabbix.*
- user: zabbix
- connection_user: root
- connection_pass: westos
cmd.wait:
- name: zcat /mnt/create.sql.gz | mysql -pwestos zabbix
- watch:
- mysql_database: db-configure
注意:mariadb初始化脚本的查看方法:
[root@foundation11 ~]# which mysql_secure_installation
/bin/mysql_secure_installation
[root@foundation11 ~]# vim /bin/mysql_secure_installation
- files目录下为要导入的数据库。
[root@server1 mariadb]# ls
files install.sls
[root@server1 mariadb]# ls files/
create.sql.gz
yum源的配置目录以及文件编写为如下:
[root@server1 salt]# cd repos/
[root@server1 repos]# ls
zabbix.sls
[root@server1 repos]# cat zabbix.sls
zabbix:
pkgrepo.managed:
- baseurl: http://172.25.11.250/zabbix/4.0
- gpgcheck: 0
top.sls文件内容为如下:
[root@server1 salt]# cat top.sls
base:
'server1': #server端
- mariadb.install
- zabbix-server.install
- zabbix-web.install
'server2':
- zabbix-agent.install #客户端
zabbix-agent的安装文件为如下:
[root@server1 salt]# cd zabbix-agent/
[root@server1 zabbix-agent]# ls
files install.sls
[root@server1 zabbix-agent]# cat install.sls
include:
- repos.zabbix
agent-install:
pkg.installed:
- name: zabbix-agent
file.managed:
- name: /etc/zabbix/zabbix_agentd.conf
- source: salt://zabbix-agent/files/zabbix_agentd.conf
- template: jinja
- context:
zabbixserver: {{ pillar['ZABBIX-SERVER'] }}
hostname: {{ grains['fqdn'] }}
service.running:
- name: zabbix-agent
- watch:
- file: agent-install
模版文件中修改3处:
[root@server1 files]# vim zabbix_agentd.conf
98 Server= {{ zabbixserver }}
139 ServerActive={{ zabbixserver }}
150 Hostname={{ hostname }}
zabbix-server的安装文件为如下:
[root@server1 salt]# cd zabbix-server/
[root@server1 zabbix-server]# ls
files install.sls
[root@server1 zabbix-server]# cat install.sls
include:
- repos.zabbix
server-install:
pkg.installed:
- zabbix-server-mysql
- zabbix-agent
file.managed:
- name: /etc/zabbix/zabbix_server.conf
- source: salt://zabbix-server/files/zabbix_server.conf
- template: jinja
- context:
dbpasswd: {{ pillar['DBPASSWD'] }}
service.running:
- name: zabbix-server
- watch:
- file: server-install
zabbix-agent:
service.running
模版文件修改2处:
[root@server1 zabbix-server]# cd files/
[root@server1 files]# ls
zabbix_server.conf
[root@server1 files]# vim zabbix_server.conf
124 DBPassword={{ dbpasswd }}
web前端界面zabbix-web内容如下:
[root@server1 salt]# cd zabbix-web/
[root@server1 zabbix-web]# ls
files install.sls
[root@server1 zabbix-web]# cat install.sls
include:
- repos.zabbix
web-install:
pkg.installed:
- pkgs:
- zabbix-web-mysql
- zabbix-web
- httpd
- php
- php-mysql
file.managed:
- name: /etc/httpd/conf.d/zabbix.conf
- source: salt://zabbix-web/files/zabbix.conf
service.running:
- name: httpd
- watch:
- file: web-install
模版文件修改时区:
[root@server1 files]# ls
zabbix.conf
[root@server1 files]# vim zabbix.conf
20 php_value date.timezone Asia/Shanghai
pillar目录下:
[root@server1 srv]# ls
pillar salt
[root@server1 srv]# cd pillar/
[root@server1 pillar]# ls
top.sls zabbix.sls
[root@server1 pillar]# cat top.sls
base:
'*':
- zabbix
[root@server1 pillar]# cat zabbix.sls
{% if grains['fqdn'] == 'server1' %}
DBPASSWD: westos
{% else %}
ZABBIX-SERVER: 172.25.11.1
{% endif %}
高级推:
[root@server1 pillar]# salt '*' state.highstate
完成后浏览器输入访问。
注意:salt-master与zabbix-server不要在同一台主机。