一、配置管理远程命令
master上
# vi /srv/salt/shell_test.sls //加入如下内容
shell_test: #这是ID
cmd.script: #模块
- source: salt://test/1.sh #这里定义的是要执行的shell脚本
- user: root #执行它的用户
# vi /srv/salt/test/1.sh //加入如下内容
#!/bin/bash
touch /tmp/111.txt
if [ ! -d /tmp/1233 ]
then
mkdir /tmp/1233
fi
更改top.sls内容
# vi /srv/salt/top.sls
base:
'*':
- shell_test
# salt 'yw02' state.highstate
检查是否有/tmp/111.txt和/tmp/1233
二、配置管理任务计划
master上
# vi /srv/salt/cron_test.sls //加入如下内容
cron_test:
cron.present:
- name: /bin/touch /tmp/111.txt #这里的name就相当于crontab -e里的最后一段
- user: root #执行它的用户
- minute: '*' #下面对应的就是crontab里的分、时、日、月、周
- hour: 20
- daymonth: '*'
- month: '*'
- dayweek: '*'
注意,*需要用单引号引起来。当然我们还可以使用file.managed模块来管理cron,因为系统的cron都是以配置文件的形式存在的。想要删除该cron,需要增加:
cron.absent:
- name: /bin/touch /tmp/111.txt
两者不能共存,要想删除一个cron,那之前的present就得去掉。
更改top.sls
# vi /srv/salt/top.sls
base:
'*':
- cron_test
# salt 'yw02' state.highstate
到yw02上检查cron:crontab -l,会看到提示# Lines below here are managed by Salt, do not edit
我们不能随意改动它,否则就没法删除或者修改这个cron了。
# crontab -l
# Lines below here are managed by Salt, do not edit #这两行一定不要动,改了就没办法再去管理了。
# SALT_CRON_IDENTIFIER:/bin/touch /tmp/111.txt
* 20 * * * /bin/touch /tmp/111.txt #这一行就是通过脚本定义的,要修改就从脚本修改即可。
三、其他命令
cp.get_file 拷贝master上的文件到客户端
# salt '*' cp.get_file salt://test/1.sh /tmp/123.sh
yw02:
/tmp/123.sh
fuxi01:
/tmp/123.sh
cp.get_dir 拷贝目录
# salt '*' cp.get_dir salt://test/123 /tmp/
//会自动在客户端创建123目录,所以后面不要加123,如果写成 /tmp/123/ 则会在/tmp/123/目录下又创建123目录。
# salt-run manage.up 显示存活的minion
# salt '*' cmd.script salt://test/1.sh 命令行下执行master上的shell脚本,对机器批量执行。
四、salt-ssh使用
salt-ssh不需要对客户端做认证,客户端也不用安装salt-minion,也不用启动任何服务,就能够到对方机器上操作,前提是把本机的公钥放到对方机器上去,它类似pssh/expect。
安装很简单
先安装好yum源
# yum install -y https://repo.saltstack.com/yum/redhat/salt-repo-latest-2.el7.noarch.rpm
# yum install -y salt-ssh
# vi /etc/salt/roster //增加如下内容
fuxi01:
host: 192.168.149.133
user: root
passwd: 1346
yw02:
host: 192.168.149.131
user: root
passwd: 1346
# salt-ssh --key-deploy '*' -r 'w' //出现这种是因为首次登陆时会询问yes/no,重新ssh连接一下即可。
yw02:
----------
retcode:
254
stderr:
stdout:
The host key needs to be accepted, to auto accept run salt-ssh with the -i flag:
The authenticity of host '192.168.149.131 (192.168.149.131)' can't be established.
ECDSA key fingerprint is SHA256:Z7xp3qHrdUE3yl4C34LCIrYyaCTvwC/hhZsWu1iZfS4.
ECDSA key fingerprint is MD5:ff:06:a8:bd:b0:d9:2f:72:df:64:07:b2:b0:36:c4:06.
Are you sure you want to continue connecting (yes/no)?
fuxi01:
----------
retcode:
254
stderr:
stdout:
The host key needs to be accepted, to auto accept run salt-ssh with the -i flag:
The authenticity of host '192.168.149.133 (192.168.149.133)' can't be established.
ECDSA key fingerprint is SHA256:PZXNkWqC/6h5hUQYkfOM9AMj82OTskLMIB4qLkgeajU.
ECDSA key fingerprint is MD5:99:19:04:c5:11:8d:94:ad:9a:86:40:b9:ad:b9:d4:8f.
Are you sure you want to continue connecting (yes/no)?
# salt-ssh --key-deploy '*' -r 'w'
yw02:
----------
retcode:
0
stderr:
stdout:
17:01:40 up 3 days, 16:58, 2 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 1610月19 6:57m 0.24s 0.24s -bash
root pts/0 192.168.149.1 10:05 6:55m 0.01s 0.01s -bash
fuxi01:
----------
retcode:
0
stderr:
stdout:
17:01:44 up 3 days, 20:23, 2 users, load average: 2.00, 1.43, 0.64
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 1610月19 6:56m 0.10s 0.10s -bash
root pts/0 192.168.149.1 17:01 24.00s 12.36s 0.70s salt-ssh
# ls -l /root/.ssh/authorized_keys //两台机器上查看公钥,时间一致。
-rw------- 1 root root 393 10月 26 16:59 /root/.ssh/authorized_keys
//第一次执行的时候会自动把本机的公钥放到对方机器上,然后就可以把roster里面的密码去掉。
//--key-deploy,如果不加这条,它就不会自动的把公钥推送到对方机器上去。
现在把/etc/salt/roster中的密码删除,再执行salt-ssh --key-deploy '*' -r 'w',同样也可以执行成功,这样就是因为把公钥推送过去了。第一次需要密码,第二次就不用了。