環境: rhel6.5 server1master
server2 minion server3 minion
配置yum安裝包:rhel6
[root@server1 ~]# yum install salt-master
[root@server1 ~]# /etc/init.d/salt-master start
[root@server2 ~]# yum install salt-minion
[root@server2 ~]# vim /etc/salt/minion
master: 172.25.135.1
[root@server2 ~]# /etc/init.d/salt-minion start
[root@server1 ~]# salt-key -A
The following keys are going to be accepted:
Unaccepted Keys:
server2
Proceed? [n/Y] y
Key for minion server2 accepted.
[root@server1 ~]# salt-key -L
Accepted Keys:
server2
Denied Keys:
Unaccepted Keys:
Rejected Keys:
[root@server1 ~]# salt server2 test.ping
server2:
True
[root@server1 ~]# salt server2 cmd.run hostname
server2:
server2
[root@server1 ~]# salt server2 cmd.run df
server2:
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/vg_server0-lv_root 18102140 2078072 15104516 13% /
tmpfs 510200 16 510184 1% /dev/shm
/dev/vda1 495844 34532 435712 8% /boot
[root@server1 ~]# salt server2 cmd.run poweroff #測試關機
server2:
[root@server1 ~]# vim /etc/salt/master
[root@server1 ~]# cd /srv/salt/
[root@server1 salt]# ls
[root@server1 salt]# mkdir apache
[root@server1 salt]# cd apache/
[root@server1 apache]# vim install.sls
httpd:
pkg.installed #寫個簡單的http安裝
[root@server1 apache]# salt server2 state.sls apache.install
[root@server1 apache]# mkdir files
[root@server2 ~]# scp /etc/httpd/conf/httpd.conf server1:/srv/salt/apache/files
[root@server1 apache]# cd files/
[root@server1 files]# ls
httpd.conf
[root@server1 files]# vim httpd.conf #簡單修改一下80端口爲8080
[root@server1 apache]# ls
files install.sls
[root@server1 apache]# vim install.sls
apache-install:
pkg.installed:php
pkgs:python
file.managed:mysql
group: rootlinux
service.running:nginx
[root@server1 pkgs]# ls
make.sls
[root@server1 pkgs]# cd ..
[root@server1 salt]# ls
apache nginx pkgs
[root@server1 salt]# cd nginx/
[root@server1 nginx]# ls
files install.sls
[root@server1 nginx]# vim install.sls
include:web
nginx-install:
file.managed:sql
source: salt://nginx/files/nginx-1.14.0.tar.gzshell
cmd.run:apache
/usr/local/nginx/conf/nginx.conf:
file.managed:vim
/etc/init.d/nginx:
file.managed:
nginx:
service.running:
haproxy-install:
file.managed:
/etc/haproxy:
file.directory:
/usr/sbin/haproxy:
file.symlink:
/etc/haproxy/haproxy.cfg:
file.managed:
haproxy-service:
file.managed:
[root@server1 salt]# mkdir users
[root@server1 users]# vim haproxy.sls
haproxy-group:
group.present:
haproxy-user:
user.present:
os:
RedHat
[root@server1 salt]# salt -G 'os:redhat' cmd.run hostname
server2:
server2
server1:
server1
server3:
server3
[root@server2 ~]# vim /etc/salt/minion
grains:
roles:
roles: - apache
roles: nginx
[root@server1 salt]# vim top.sls
base:
"server1":
hello: world
[root@server1 salt]# cd /etc/salt
[root@server1 salt]# mkdir /srv/pillar/
[root@server1 pillar]# /etc/init.d/salt-master restart
[root@server1 pillar]# mkdir web
[root@server1 pillar]# cd web/
[root@server1 web]# ls
[root@server1 web]# vim install.sls
{% if grains['fqdn'] == 'server2' %}
webserver: httpd
{% elif grains['fqdn'] == 'server3'%}
webserver: nginx
{% endif %}
[root@server1 web]# cd ..
[root@server1 pillar]# ls
web
[root@server1 pillar]# vim top.sls
base:
'*':
webserver:
httpd
[root@server1 pillar]# salt -I 'webserver:nginx' test.ping
server3:
True
[root@server1 pillar]# salt -S 172.25.135.0/24 test.ping
server3:
True
server2:
True
server1:
True
金佳模版:
[root@server1 salt]# cd apache/
[root@server1 apache]# vim install.sls
apache-install:
pkg.installed:
pkgs:
file.managed:
def df():
cmd = 'df -h'
return salt'cmd.run'
[root@server1 _modules]# salt '*' saltutil.sync_modules
server1:
[root@server4 ~]# /etc/init.d/salt-master start
[root@server4 ~]# salt-key -L
[root@server4 ~]# salt-key -A
[root@server1 _modules]# yum install salt-syndic
[root@server1 _modules]# /etc/init.d/salt-master restart
[root@server1 _modules]# salt-key -L
[root@server1 _modules]# /etc/init.d/salt-syndic start
[root@server4 ~]# salt '' my_disk.df
[root@server4 ~]# salt '' test.ping
salt-ssh 推送
[root@server1 _modules]# yum install salt-ssh
[root@server1 _modules]# vim /etc/salt/roster
server2:
host: 172.25.135.2
user: root
passwd: redhat
server3:
host: 172.25.135.3
user: root
passwd: redhat
停掉server2和server3上salt-minion[root@server1 _modules]# salt-ssh '*' test.ping