安装
## ubuntu16安装python3版本(python3.5及以上)
wget -O - https://repo.saltstack.com/py3/ubuntu/16.04/amd64/latest/SALTSTACK-GPG-KEY.pub | sudo apt-key add -
deb http://repo.saltstack.com/py3/ubuntu/16.04/amd64/latest xenial main
## centos7
yum install https://repo.saltstack.com/py3/redhat/salt-py3-repo-latest-2.el7.noarch.rpm
salt-ssh
## 在不安装salt-minion的情况下通过ssh执行salt命令
/etc/salt/roster
salt-master (控制中心)
##yum install salt-master
## 4505发送命令,监听信息
## 4506建立一对一连接,异步处理
## /etc/salt/master
#interface: [hostname]
#hash_type: sha512 证书认证方式
salt-minion(被控制端)
## yum install salt-minion
## /etc/salt/minion
#master: [master ip]
#hash_type: sha512
#id: [minion id]
批量安装salt-minion可以参考我的另一篇文章:https://blog.csdn.net/wuguifa/article/details/86599424
查看minion列表
salt-key -L # 查看所有清单
salt-key -A # 接受所有Unaccepted keys
salt-key -a [salt-minion id] # 手动添加证书认证
salt-key -D # 删除所有证书
salt-key -d [salt-minion id] # 删除单个
saltstack命令行远程执行命令
salt -N "group1" cmd.run "ifconfig"
# -E 以正则匹配主机ID
# -L 以逗号分隔
# -N nodegroups
master配置文件中配置好组名和对应主机ID
group1: "L@id1,id2" #L表示后面的ID以列表形式分隔,分隔符","
# -C 多条件匹配,not,and,or
# -S 根据主机IP地址或IP子网进行匹配
常用命令
## cmd.run(cmd ,cwd ,...)
salt "*" cmd.run "ifconfig"
## cp模块
1、salt-cp命令
salt-cp “*” cp.get_file [localPath] [remotePath]
2、Salt 文件服务器
salt://表示 文件服务器的base环境。
Salt文件服务器/etc/salt/master配置文件中的flie_roots选项管理。
3、cp.get_fle,用于minion从master下载一个文件,如果目录不存在,cp.get_file是不会主动创建目录的,makedirs=True创建目录
salt "*" cp.get_file salt://tmp/python1.py /tmp/dir1/ makedirs=True
4、cp.get_dir
5、cp.push
cp.push功能默认不开启,需要修改配置文件中的file_recv 环境,默认为False
上传的文件存放在master端的 /var/cache/salt/master/minions/<minion_id>/files/ 目录下
## file模块
1、file.copy
salt '*' file.copy /path/to/src_dir /path/to/dst_dir recurse=True remove_existing=True
2、file.move
salt '*' file.move /path/to/src /path/to/dst
3、file.remove
salt '*' file.rename /path/to/src /path/to/dst
4、file.chown
salt '*' file.chown /etc/passwd root root
5、file.mkdir
salt '*' file.mkdir /opt/jetty/context
6、file.file_exists
salt '*' file.file_exists /etc/passwd
7、file.stats
salt '*' file.file_exists /etc/passwd
PYTHON API(python3版本2018.3)
import salt.client
client = salt.client.LocalClient()
## cmd(tgt, fun, arg=(), timeout=None, tgt_type=u'glob', ret=u'', jid=u'', full_return=False, kwarg=None, **kwargs)
tgt: minion主机,默认是glob。由tgt_type选项修改
fun: 模块
arg: 要传递给远程函数的参数列表,["arg1","arg2"]传入两个参数。
timeout: 超时
tgt_type: glob,list,nodegroup,pcre
result = client.cmd("group1" ,"cmd.run" ,arg = ["ls -la"] ,tgt_type = "nodegroup")