salt-ssh简介
salt-ssh 是 0.17.0 新出现的一个功能,它是依赖 ssh 来进行远程命令执行的工具,好处就是不需要在客户端安装 minion,也不需要安装 master(直接安装 salt-ssh 这个包即可),有些时候你还真的需要 salt-ssh(例如:条件不允许安装 minion、不用长期管理某台 minion) 最重要的是 salt-ssh 并不只是单纯的 ssh 工具,它支持 salt 大部分的功能,如 grains、modules、state 等
需要注意的是,salt-ssh 并没有继承原来的通讯架构 (ZeroMQ),也就是说它的执行速度啥的都会比较慢

salt-ssh安装

yum install salt-ssh –y

salt-ssh 需要配合roster使用,首先配置roster,默认地址/etc/salt/roster,roster 语法很简答,定义 ID、host、user、password 即可

1
2
3
4
5
6
7
[root@localhost salt]# cat /etc/salt/roster 
lnmp135:
host: 192.168.1.135
user: root
passwd: mima123
port: 22
timeout: 10

salt-ssh远程安装minion客户端

在/etc/salt/states/下新增文件夹minions_init,以下文件皆放在此文件夹下
添加批量脚本方便写进/etc/salt/roster

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[root@localhost minions_init]# cat Ip_add.sh 
#!/bin/bash
for i in `cat /etc/salt/minions_init/Ip_list.txt`
do
echo "$i:" >> /etc/salt/roster
echo " host: $i" >> /etc/salt/roster
echo " user: adminuser" >> /etc/salt/roster
echo " passwd: ABC123" >> /etc/salt/roster
echo " sudo: True" >> /etc/salt/roster
echo " timeout: 10" >> /etc/salt/roster
done
IP地址格式:
[root@localhost minions_init]# cat Ip_list.txt
192.168.1.235
新增install.sls
[root@localhost minions_init]# cat install.sls
minion_install:
pkg.installed:
- pkgs:
- salt-minion
- unless: rpm -qa | grep salt-minion 当存在salt-minion客户端时候不执行minion_install

minion_conf:
file.managed:
- name: /etc/salt/minion
- source: salt://minions_init/file/minion
- user: root
- group: root
- mode: 640
- require:
- pkg: minion_install 当执行完minion_install才能执行minion_conf
minion_service:
service.running:
- name: salt-minion
- enable: True
- require:
- file: minion_conf

执行命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
[root@localhost salt]# salt-ssh -i '*' state.sls minions_init.install
192.168.1.135:
----------
ID: minion_install
Function: pkg.installed
Result: True
Comment: unless execution succeeded
Started: 11:31:21.058348
Duration: 3028.149 ms
Changes:
----------
ID: minion_conf
Function: file.managed
Name: /etc/salt/minion
Result: True
Comment: File /etc/salt/minion updated
Started: 11:31:24.088089
Duration: 372.671 ms
Changes:
----------
diff:
---
+++
@@ -642,3 +642,5 @@
############################################
# Which returner(s) will be used for minion's result:
#return: mysql
+master: 192.168.1.248
+#id: {{ minion_id }}
----------
ID: minion_service
Function: service.running
Name: salt-minion
Result: True
Comment: Service salt-minion is already enabled, and is running
Started: 11:31:24.462094
Duration: 2698.344 ms
Changes:
----------
salt-minion:
True

Summary
------------
Succeeded: 3 (changed=2)
Failed: 0
------------
Total states run: 3

[root@localhost ~]# salt –versions-report
Salt: 2015.5.10
Python: 2.6.6 (r266:84292, Aug 18 2016, 15:13:37)
Jinja2: 2.2.1
M2Crypto: 0.20.2
msgpack-python: 0.4.6
msgpack-pure: Not Installed
pycrypto: 2.0.1
libnacl: Not Installed
PyYAML: 3.10
ioflo: Not Installed
PyZMQ: 14.3.1
RAET: Not Installed
ZMQ: 3.2.5
Mako: 0.3.4
Tornado: Not Installed
timelib: Not Installed
dateutil: Not Installed

Comments

2017-03-02