############################################################
*
* SaltStack远程安装apache
############################################################
[[email protected] pillar]# tree /srv/salt/apache/
/srv/salt/apache/
└── apache-install.sls
[[email protected] salt]# mkdir -p /srv/salt/apache/
[[email protected] apache]# cat /srv/salt/apache/apache-install.sls
apache-install:
pkg.installed:
- names:
- httpd
httpd:
service.running:
- httpd
- enable: True
- reload: True
#服务端执行命令
salt ‘WEB0?‘ state.sls apache.apache-install
#服务端配置文件
[[email protected] salt]# cat /srv/salt/top.sls
base:
‘*‘:
- apache.apache-install
#执行高级模块,适合去检测开机自起动的程序
[[email protected] salt]# salt ‘WEB01‘ state.highstate
####grains
#查询grains值,grains适合做一些静态的属性值的采集
[[email protected] salt]# salt ‘LB02‘ grains.items
#通过grains值过滤IP
[[email protected] salt]# salt ‘LB02‘ grains.get ip_interfaces:eth0
LB02:
- 192.168.44.6
- fe80::20c:29ff:fe3e:ec5b
#-G是调用grains值,属性值为CentOS的执行cmd模块,run方法,w命令
[[email protected] salt]# salt -G os:CentOS cmd.run ‘w‘
WEB02:
10:23:25 up 1:46, 1 user, load average: 0.00, 0.00, 0.00
USER TTY FROM [email protected] IDLE JCPU PCPU WHAT
root pts/1 192.168.44.1 08:37 1:45m 0.00s 0.00s -bash
LB02:
10:23:25 up 1:46, 1 user, load average: 0.00, 0.00, 0.00
USER TTY FROM [email protected] IDLE JCPU PCPU WHAT
root pts/0 192.168.44.1 08:37 34:28 0.09s 0.09s -bash
#通过grains属性值,查服务的快速服务代码
[[email protected] salt]# salt ‘LB02‘ grains.get server_id|awk NR==2|awk ‘{print $1}‘
241255280
####pallar
#服务器端配置pillar,用于敏感数据,处理变量的差异性
[[email protected] salt]# vim /etc/salt/master
pillar_opts: Fales
pillar_roots:
base:
- /srv/pillar
[[email protected] salt]# /etc/init.d/salt-master restart
[[email protected] salt]# mkdir -p /srv/pillar
[[email protected] pillar]# cat /srv/pillar/apache.sls
{% if grains[‘os‘] == ‘CentOS‘ %}
apache: httpd
{% elif grains[‘os‘] == ‘Debian‘ %}
apache: apache2
{% endif %}
[[email protected] pillar]# cat /srv/pillar/top.sls
base:
‘*‘:
- apache.apache-install
[[email protected] pillar]# salt ‘*‘ pillar.items
#如果取不到客户端数据就salt刷新一下
#报错 Minion did not return. [No response] ,刷新一下
[[email protected] pillar]# salt ‘*‘ saltutil.refresh_pillar
#判断grains的类型os的结果为CentOS
[[email protected] pillar]# salt -I ‘apache:httpd‘ test.ping
LB02:
True
#windows客户端安装
https://repo.saltstack.com/windows/Salt-Minion-2018.3.0-Py3-AMD64-Setup.exe
备注:安装时指向服务器地址就行
#查看win主机的ip,需要使用windows自身的命令
[[email protected] pillar]# salt ‘windowshost‘ cmd.run ‘ipconfig‘
#授权user用户有test.ping和network.*的权限
[[email protected] pillar]# cat /etc/salt/master
client_acl:
user:
- test.ping
- network.*
[[email protected] pillar]# chmod 755 /var/cache/salt /var/cache/salt/master /var/cache/salt/master/jobs /var/run/salt /var/run/salt/master
原文地址:http://blog.51cto.com/yehaixiao/2125090