saltstack相关

假设:

salt-server: AMH

salt-minion: AMH , client1741 , cdn-nginx , cdn-varnish

查看salt-minion是否 在线

[root@AMH ~]# salt ‘*’ test.ping

cdn-nginx:

True

client1741:

True

AMH:

True

cdn-varnish:

True

 

查看所有的minion,包括认证的和没有认证的

[root@AMH ~]# salt-key -L

Accepted Keys:

AMH

cdn-nginx

cdn-varnish

client1741

Denied Keys:

Unaccepted Keys:

Rejected Keys:

 

添加一个minion

[root@AMH ~]# salt-key -a

添加所有的minion

[root@AMH ~]# salt-key -A

删除一个minion

[root@AMH ~]# salt-key -d

例如:

[root@AMH ~]# salt-key -d 192.168.17.111

The following keys are going to be deleted:

Accepted Keys:

192.168.17.111

Proceed? [N/y] y

Key for minion 192.168.17.111 deleted.

删除所有的minion

[root@AMH ~]# salt-key -D

执行salt-key –help 可以看到更多用法

 

cmd.run

执行简单的命令

[root@AMH ~]# salt ‘*’ cmd.run ‘date’

cdn-nginx:

Tue Apr 17 14:11:31 CST 2018

client1741:

Tue Apr 17 14:11:31 CST 2018

AMH:

Tue Apr 17 14:11:31 CST 2018

cdn-varnish:

Tue Apr 17 14:11:32 CST 2018

[root@AMH ~]# salt ‘*’ cmd.run ‘df -h’

client1741:

Filesystem Size Used Avail Use% Mounted on

/dev/mapper/VolGroup-lv_root 28G 7.3G 19G 29% /

tmpfs 242M 12K 242M 1% /dev/shm

/dev/sda1 485M 35M 426M 8% /boot

cdn-varnish:

Filesystem Size Used Avail Use% Mounted on

/dev/mapper/VolGroup-lv_root 28G 7.3G 19G 29% /

tmpfs 242M 12K 242M 1% /dev/shm

/dev/sda1 485M 35M 426M 8% /boot

AMH:

Filesystem Size Used Avail Use% Mounted on

/dev/mapper/VolGroup-lv_root 28G 13G 14G 47% /

tmpfs 301M 16K 301M 1% /dev/shm

/dev/sda1 485M 35M 426M 8% /boot

cdn-nginx:

Filesystem Size Used Avail Use% Mounted on

/dev/mapper/VolGroup-lv_root 28G 9.4G 17G 37% /

tmpfs 242M 12K 242M 1% /dev/shm

/dev/sda1 477M 55M 398M 12% /boot

 

让master上的脚本在远程执行

vim /etc/salt/master配置文件

添加红框中的内容

conf1

/etc/init.d/salt-master restart

在/srv/salt/下创建script目录,编辑查看日期的脚本

vim /srv/salt/script/date.sh

#!/bin/bash

echo “show current time”

date

#end

salt_config2

master执行这个脚本

salt ‘*’ cmd.script salt://script/data.sh

salt_config3

 

[root@AMH script]# salt ‘*’ cmd.script salt://script/date.sh

AMH:

———-

pid:

2022

retcode:

0

stderr:

stdout:

show current time

Tue Apr 17 14:28:51 CST 2018

cdn-nginx:

———-

pid:

50672

retcode:

0

stderr:

stdout:

show current time

Tue Apr 17 14:28:51 CST 2018

client1741:

———-

pid:

2488

retcode:

0

stderr:

stdout:

show current time

Tue Apr 17 14:28:51 CST 2018

cdn-varnish:

———-

pid:

3195

retcode:

0

stderr:

stdout:

show current time

Tue Apr 17 14:28:51 CST 2018

 

cp.get_file和cp.get_dir

将master上的脚本传到远程服务器的/opt目录下

假设脚本sysinfo.sh 放在 /srv/salt/上

[root@AMH salt]# salt ‘*’ cp.get_file salt://sysinfo.sh /opt/sysinfo.sh

或者采用gzip压缩方式分发该脚本

[root@AMH salt]# salt ‘*’ cp.get_file salt://sysinfo.sh /opt/sysinfo.sh gzip=6

然后执行 salt ‘*’ cmd.run ‘ls /opt/sysinfo.sh’ 看每台服务器是否存在该文件

salt_config4

 

每一台minion执行此脚本

[root@AMH salt]# salt ‘*’ cmd.script ‘/opt/sysinfo.sh’

salt_config5

 

将test_dir目录传到 minion的/tmp下

[root@AMH salt]#salt '*' cp.get_dir salt://test_dir /tmp gzip=6 

 

pkg模块

相当于RHEL的yum 和ubuntu的apt-get

salt ‘*’ pkg.install package

salt ‘*’ pkg.remove package

举例

[root@AMH salt]# salt ‘*’ pkg.install httpd

client1741:

———-

AMH:

———-

cdn-nginx:

———-

httpd:

———-

new:

2.2.15-60.el6.centos.6

old:

2.2.15-29.el6.centos

httpd-manual:

———-

new:

2.2.15-60.el6.centos.6

old:

2.2.15-29.el6.centos

httpd-tools:

———-

new:

2.2.15-60.el6.centos.6

old:

2.2.15-29.el6.centos

mod_ssl:

———-

new:

1:2.2.15-60.el6.centos.6

old:

1:2.2.15-29.el6.centos

cdn-varnish:

———-

httpd:

———-

new:

2.2.15-60.el6.centos.6

old:

2.2.15-29.el6.centos

httpd-manual:

———-

new:

2.2.15-60.el6.centos.6

old:

2.2.15-29.el6.centos

httpd-tools:

———-

new:

2.2.15-60.el6.centos.6

old:

2.2.15-29.el6.centos

mod_ssl:

———-

new:

1:2.2.15-60.el6.centos.6

old:

1:2.2.15-29.el6.centos

[root@AMH salt]# salt ‘*’ cmd.run ‘rpm -qa | grep httpd’

cdn-nginx:

httpd-tools-2.2.15-60.el6.centos.6.x86_64

httpd-2.2.15-60.el6.centos.6.x86_64

httpd-manual-2.2.15-60.el6.centos.6.noarch

cdn-varnish:

httpd-manual-2.2.15-60.el6.centos.6.noarch

httpd-tools-2.2.15-60.el6.centos.6.x86_64

httpd-2.2.15-60.el6.centos.6.x86_64

client1741:

httpd-manual-2.2.15-60.el6.centos.6.noarch

httpd-2.2.15-60.el6.centos.6.x86_64

httpd-tools-2.2.15-60.el6.centos.6.x86_64

httpd-devel-2.2.15-60.el6.centos.6.x86_64

AMH:

httpd-tools-2.2.15-60.el6.centos.6.x86_64

httpd-2.2.15-60.el6.centos.6.x86_64

httpd-devel-2.2.15-60.el6.centos.6.x86_64

 

[root@AMH salt]# salt ‘*’ pkg.remove httpd

cdn-nginx:

———-

gnome-user-share:

———-

new:

old:

2.28.2-3.el6

httpd:

———-

new:

old:

2.2.15-60.el6.centos.6

httpd-manual:

———-

new:

old:

2.2.15-60.el6.centos.6

mod_auth_kerb:

———-
new:

old:

5.4-10.el6

mod_perl:

———-

new:

old:

2.0.4-10.el6

mod_ssl

———-

new

old:

1:2.2.15-60.el6.centos.6

mod_wsgi:

———-

new:

old:

3.2-3.el6

php:

———-
new:

old:

5.3.3-26.el6

piranha:

———-

new:

old:

0.8.6-4.el6

AMH:

———-

httpd:

———-

new:

old:

2.2.15-60.el6.centos.6

httpd-devel

———-
new:

old:

2.2.15-60.el6.centos.6

php

———-

new:

old:

5.3.3-49.el6

client1741:

———-
gnome-user-share:
———-
new

old:

2.28.2-3.el6

httpd:

———-

new:

old:

2.2.15-60.el6.centos.6

httpd-devel:

———-

new:

old:

2.2.15-60.el6.centos.6

httpd-manual:

———-

new:

old:

2.2.15-60.el6.centos.6

ipa-server:

———-

new:

old:

3.0.0-37.el6

ipa-server-selinux:

———-

new:

old:

3.0.0-37.el6

mod_auth_kerb:

———-

new:

old:

5.4-10.el6

mod_perl:

———-

new:

old:

2.0.4-10.el6

mod_ssl:

———-

new:

old:

1:2.2.15-60.el6.centos.6

mod_wsgi:

———-
new:

old:

3.2-3.el6

php:

———-

new:

old:

5.3.3-26.el6

piranha:

———-

new:

old:

0.8.6-4.el6

webalizer:

———-

new:

old:

2.21_02-3.3.el6

cdn-varnish:

———-

gnome-user-share:

———-

new:

old:

2.28.2-3.el6

httpd:

———-

new:

old:

2.2.15-60.el6.centos.6

httpd-manual:

———-

new:

old:

2.2.15-60.el6.centos.6

ipa-server:

———-

new:

old:

3.0.0-37.el6

ipa-server-selinux:

———-

new:

old:

3.0.0-37.el6

mod_auth_kerb:

———-

new:

old:

5.4-10.el6

mod_perl:

———-

new:

old:

2.0.4-10.el6

mod_ssl:

———-

new:

old:

1:2.2.15-60.el6.centos.6

mod_wsgi:

———-

new:

old:

3.2-3.el6

php:

———-

new:

old:

5.3.3-26.el6

piranha:

———-

new:

old:

0.8.6-4.el6

webalizer:

———-

new:

old:

2.21_02-3.3.el6

 

service模块

salt ‘*’ service.enable service_name

salt ‘*’ service.disable service_name

salt ‘*’ service.start service_name

salt ‘*’ service.stop service_name

salt ‘*’ service.restart service_name

举例

salt_config7

salt_config8

 

Leave a Reply