SaltStack远程安装apache

############################################################
*
* SaltStack远程安装apache

############################################################

[root@M01 pillar]# tree /srv/salt/apache/
/srv/salt/apache/
└── apache-install.sls

[root@M01 salt]# mkdir -p /srv/salt/apache/

[root@M01 apache]# cat /srv/salt/apache/apache-install.sls
apache-install:
pkg.installed:

  • names:
    • httpd
      httpd:
      service.running:
  • enable: True
  • reload: True

#服务端执行命令
salt 'WEB0?' state.sls apache.apache-install

#服务端配置文件
[root@M01 salt]# cat /srv/salt/top.sls
base:
'*':

  • apache.apache-install

#执行高级模块,适合去检测开机自起动的程序
[root@M01 salt]# salt 'WEB01' state.highstate

####grains
#查询grains值,grains适合做一些静态的属性值的采集
[root@M01 salt]# salt 'LB02' grains.items

#通过grains值过滤IP
[root@M01 salt]# salt 'LB02' grains.get ip_interfaces:eth0
LB02:

  • 192.168.44.6
  • fe80::20c:29ff:fe3e:ec5b

#-G是调用grains值,属性值为CentOS的执行cmd模块,run方法,w命令
[root@M01 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 LOGIN@ 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 LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.44.1 08:37 34:28 0.09s 0.09s -bash

#通过grains属性值,查服务的快速服务代码
[root@M01 salt]# salt 'LB02' grains.get server_id|awk NR==2|awk '{print $1}'
241255280

####pallar
#服务器端配置pillar,用于敏感数据,处理变量的差异性
[root@M01 salt]# vim /etc/salt/master
pillar_opts: Fales
pillar_roots:
base:

  • /srv/pillar
    [root@M01 salt]# /etc/init.d/salt-master restart
    [root@M01 salt]# mkdir -p /srv/pillar

[root@M01 pillar]# cat /srv/pillar/apache.sls
{% if grains['os'] == 'CentOS' %}
apache: httpd
{% elif grains['os'] == 'Debian' %}
apache: apache2
{% endif %}
[root@M01 pillar]# cat /srv/pillar/top.sls
base:
'*':

  • apache.apache-install

[root@M01 pillar]# salt '*' pillar.items

#如果取不到客户端数据就salt刷新一下
#报错 Minion did not return. [No response] ,刷新一下
[root@M01 pillar]# salt '*' saltutil.refresh_pillar

#判断grains的类型os的结果为CentOS
[root@M01 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自身的命令
[root@M01 pillar]# salt 'windowshost' cmd.run 'ipconfig'

#授权user用户有test.ping和network.*的权限
[root@M01 pillar]# cat /etc/salt/master
client_acl:
user:

  • test.ping
  • network.*

[root@M01 pillar]# chmod 755 /var/cache/salt /var/cache/salt/master /var/cache/salt/master/jobs /var/run/salt /var/run/salt/master

猜你喜欢

转载自blog.51cto.com/yehaixiao/2125090
今日推荐