saltstack实战4--综合练习4

时间:2023-03-09 05:56:13
saltstack实战4--综合练习4
Saltstack配置管理-给minion增加Zabbix-agent
zabbix-agent的包
[root@A ~]# rpm -qa |grep zabbix
zabbix-2.4.8-1.el6.x86_64
zabbix-web-2.4.8-1.el6.noarch
zabbix-web-mysql-2.4.8-1.el6.noarch
zabbix-server-mysql-2.4.8-1.el6.x86_64
zabbix-server-2.4.8-1.el6.x86_64
zabbix-agent-2.4.8-1.el6.x86_64
zabbix-get-2.4.8-1.el6.x86_64
zabbix-release-2.4-1.el6.noarch
[root@A ~]#

因为我们想让一个系统在配置业务之前就让它被监控到,因此写的配置zabbix的状态文件应该放到系统初始化里面的

可以看到配置文件使用了jinja模板,把变量提取出来。可以这么理解,Server后面就是10.0.1.161, 而这个10.0.1.161是通过pillar获取的,pillar文件在/srv/salt/pillar文件里
[root@master prod]# cd /srv/salt/base/init/
[root@master init]# ls
audit.sls dns.sls env_init.sls files history.sls sysctl.sls
[root@master init]# vim zabbix_agent.sls
[root@master init]# cat zabbix_agent.sls
zabbix-agent-install:
pkg.installed:
- name: zabbix-agent
file.managed:
- name: /etc/zabbix/zabbix_agentd.conf
- source: salt://init/files/zabbix_agentd.conf
- template: jinja
- defaults:
Server: {{ pillar['zabbix-agent']['Zabbix-Server'] }}
- require:
- pkg: zabbix-agent-install
service.running:
- name: zabbix-agent
- enable: True
- watch:
- pkg: zabbix-agent-install
- file: zabbix-agent-install
[root@master init]#

上面内容用到了pillar

目前还没有pillar,执行肯定会报错
下面这个是个两级的key

pillar['zabbix-agent']['Zabbix-Server']

对pillar进行管理

vim /etc/salt/master

pillar_roots:
base:
- /srv/pillar/base

重启服务,建立pillar的基础目录

[root@master init]# vim /etc/salt/master
[root@master init]# /etc/init.d/salt-master restart
Stopping salt-master daemon: [ OK ]
Starting salt-master daemon: [ OK ]
[root@master init]# mkdir /srv/pillar/base -p
[root@master init]#

建立zabbix相关的pillar配置

[root@master base]# cd /srv/pillar/base/
[root@master base]# cat zabbix.sls
zabbix-agent:
Zabbix-Server: 10.0.1.161
[root@master base]# cat top.sls
base:
'*':
- zabbix
[root@master base]#

把现网zabbix-agent文件传输到master机器上

我本地有个10.0.1.161机器安装了zabbix-agent

[root@A ~]# ll /etc/zabbix/
total 32
drwxr-x---. 2 apache apache 4096 Nov 29 05:55 web
-rw-r--r--. 1 root root 7905 Nov 29 05:59 zabbix_agentd.conf
drwxr-xr-x. 2 root root 4096 Nov 29 03:13 zabbix_agentd.d
-rw-r-----. 1 root zabbix 13545 Nov 29 05:45 zabbix_server.conf
[root@A ~]# nc -l 18080 < /etc/zabbix/zabbix_agentd.conf
[root@A ~]#

master机器获取zabbix-agent的配置文件

[root@master base]# cd /srv/salt/base/init/files/
[root@master files]# nc 10.0.1.161 18080 > zabbix_agentd.conf
[root@master files]#

修改此配置文件,把变量的值替换为一个jinja模板形式的变量

[root@master files]# grep Server=  zabbix_agentd.conf
# Server=
Server=10.0.1.161
[root@master files]# grep ^Server= zabbix_agentd.conf
Server=10.0.1.161
[root@master files]# grep 10.0.1.161 zabbix_agentd.conf
Server=10.0.1.161
[root@master files]# sed -i 's#10.0.1.161#{{ Server }}#g' zabbix_agentd.conf
[root@master files]# grep ^Server= zabbix_agentd.conf
Server={{ Server }}
[root@master files]#

刷新pillar信息,尝试是否可以获取

这里用到了pillar.get模块

[root@master files]# salt '*' saltutil.refresh_pillar
minion02:
True
minion01:
True
[root@master files]# salt '*' pillar.items
minion01:
----------
zabbix-agent:
----------
Zabbix-Server:
10.0.1.161
minion02:
----------
zabbix-agent:
----------
Zabbix-Server:
10.0.1.161
[root@master files]# salt '*' pillar.get zabbix-agent
minion02:
----------
Zabbix-Server:
10.0.1.161
minion01:
----------
Zabbix-Server:
10.0.1.161
[root@master files]# salt '*' pillar.get zabbix-agent:Zabbix-Server
minion01:
10.0.1.161
minion02:
10.0.1.161
[root@master files]#

把安装zabbi_agent的状态模块include系统初始化模块里

[root@master init]# pwd
/srv/salt/base/init
[root@master init]# ll
total 28
-rw-r--r-- 1 root root 173 Jan 1 21:39 audit.sls
-rw-r--r-- 1 root root 131 Jan 1 21:20 dns.sls
-rw-r--r-- 1 root root 92 Jan 2 16:18 env_init.sls
drwxr-xr-x 2 root root 4096 Jan 2 16:09 files
-rw-r--r-- 1 root root 88 Jan 1 21:25 history.sls
-rw-r--r-- 1 root root 174 Jan 1 21:55 sysctl.sls
-rw-r--r-- 1 root root 466 Jan 2 15:58 zabbix_agent.sls
[root@master init]# cat env_init.sls
include:
- init.dns
- init.history
- init.audit
- init.sysctl
- init.zabbix_agent
[root@master init]#

 先测试,没问题再安装

----------
ID: zabbix-agent-install
Function: pkg.installed
Name: zabbix-agent
Result: None
Comment: The following packages are set to be installed/updated: zabbix-agent
Started: 16:20:57.360039
Duration: 4128.621 ms
Changes:
----------
ID: zabbix-agent-install
Function: file.managed
Name: /etc/zabbix/zabbix_agentd.conf
Result: None
Comment: The file /etc/zabbix/zabbix_agentd.conf is set to be changed
Started: 16:21:01.489318
Duration: 9.849 ms
Changes:
----------
newfile:
/etc/zabbix/zabbix_agentd.conf
----------
ID: zabbix-agent-install
Function: service.running
Name: zabbix-agent
Result: None
Comment: Service is set to be started
Started: 16:21:01.505098
Duration: 9.422 ms
Changes: Summary
------------
Succeeded: 9 (unchanged=3, changed=1)
Failed: 0
------------
Total states run: 9
[root@master base]# salt 'minion01' state.sls init.env_init test=True

对左右minion执行安装,截取后面的执行过程

                  +#	It is allowed to include multiple LoadModule parameters.
+#
+# Mandatory: no
+# Default:
+# LoadModule=
----------
ID: zabbix-agent-install
Function: service.running
Name: zabbix-agent
Result: True
Comment: Service zabbix-agent has been enabled, and is running
Started: 16:22:21.084665
Duration: 137.363 ms
Changes:
----------
zabbix-agent:
True Summary
------------
Succeeded: 9 (changed=3)
Failed: 0
------------
Total states run: 9
[root@master base]# salt '*' state.sls init.env_init

 验证确认

[root@master base]# salt '*'  cmd.run 'ps -ef | grep zabbix'
minion01:
zabbix 24282 1 0 16:22 ? 00:00:00 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
zabbix 24284 24282 0 16:22 ? 00:00:00 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
zabbix 24285 24282 0 16:22 ? 00:00:00 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
zabbix 24286 24282 0 16:22 ? 00:00:00 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
zabbix 24287 24282 0 16:22 ? 00:00:00 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
zabbix 24288 24282 0 16:22 ? 00:00:00 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
root 24307 24306 0 16:23 ? 00:00:00 /bin/sh -c ps -ef | grep zabbix
root 24309 24307 0 16:23 ? 00:00:00 grep zabbix
minion02:
zabbix 18617 1 0 16:22 ? 00:00:00 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
zabbix 18618 18617 0 16:22 ? 00:00:00 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
zabbix 18620 18617 0 16:22 ? 00:00:00 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
zabbix 18621 18617 0 16:22 ? 00:00:00 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
zabbix 18622 18617 0 16:22 ? 00:00:00 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
zabbix 18623 18617 0 16:22 ? 00:00:00 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
root 18649 18648 0 16:23 ? 00:00:00 /bin/sh -c ps -ef | grep zabbix
root 18651 18649 0 16:23 ? 00:00:00 grep zabbix
[root@master base]# salt '*' cmd.run 'chkconfig --list zabbix-agent'
minion02:
zabbix-agent 0:off 1:off 2:on 3:on 4:on 5:on 6:off
minion01:
zabbix-agent 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@master base]#

  至此zabbix-agent也安装完成