saltstack远程控制(文件及命令)
1、远程连接命令控制
[root@server1 ~]# salt server4 sys.doc pkg #查看模块文档
[root@server1 ~]# salt '*' pkg.verify lftp #校验
[root@server1 ~]# salt '*' pkg.install wget #安装
[root@server1 ~]# salt '*' pkg.remove wget #删除
2、编写操作命令
[root@server1 ~]# mkdir /srv/salt
[root@server1 salt]# pwd
/srv/salt
[root@server1 salt]# vim install.sls
[root@server1 salt]# cat install.sls
httpd:
pkg.installed
[root@server1 salt]# salt server2 state.sls install
3、编写远程执行模块
master:
[root@server1 _modules]# salt server2 cmd.run df #命令调用模块
[root@server1 _modules]# pwd
/srv/salt/_modules
[root@server1 _modules]# vim mydisk.py
[root@server1 _modules]# cat mydisk.py
def df():
return _salt_['cmd.run']('df -h')
[root@server1 _modules]# salt server2 saltutil.sync_modules
server2:
- modules.mydisk
[root@server1 _modules]# salt server2 mydisk.df #调用
minion:
[root@server2 salt]# cd /var/cache/salt/minion/ #查看,同步模块
[root@server2 minion]# pwd
/var/cache/salt/minion
[root@server2 minion]# tree .
4、编写安装多个软件包
方法一:
httpd:
pkg.installed
wget:
pkg.installed
方法二:
[root@server1 salt]# vim install.sls #卸载将install改为remove即可
[root@server1 salt]# cat install.sls
apache:
pkg.installed:
- pkgs:
- httpd
- wget
- php
[root@server1 salt]# salt server2 state.sls install
5、不同节点装不同的安装包
方式一:
[root@server1 salt]# salt server2 state.sls apache
[root@server1 salt]# salt server4 state.sls nfs
方式二:
[root@server1 salt]# cat top.sls
base:
'server2':
- apache
'server4':
- nfs #指当前目录的文件
[root@server1 salt]# salt '*' state.highstate
6、归类
方式一:
[root@server1 salt]# mkdir apache
[root@server1 salt]# mkdir nfs
[root@server1 salt]# mv apache.sls apache
[root@server1 salt]# mv nfs.sls nfs
[root@server1 salt]# salt server2 state.sls nfs.nfs
[root@server1 salt]# cat top.sls
base:
'server2':
- apache.apache
'server4':
- nfs.nfs
[root@server1 salt]# salt '*' state.highstate
方式二:
[root@server1 apache]# mv apache.sls init.sls
[root@server1 apache]# ls
init.sls
[root@server1 apache]# cd ..
[root@server1 salt]# vim top.sls
base:
'server2':
- apache
[root@server1 salt]# salt '*' state.highstate