【Linux】salt的cmd.script命令介绍

salt是一个很棒的自动化运维工具之一,常用的有cmd.run,今天介绍的是cmd.script 

其实一眼就能看出这个命令是执行脚本的命令

具体操作如下:

1.将/etc/salt/master中的

file_roots:

   base:

     - /srv/salt

这三行取消注释,记住是取消注释

取消完成后保存

这个就执行了需要执行脚本存放的位置

2.创建该路径

mkdir /srv/salt/script/ -p

3.在该路径下存放脚本

这里有一个侧式脚本test.sh

#!/bin/bash

if [ ! -f "/test/test.txt" ];
then
       mkdir /test
       cp /test/test.txt /test/test.txt.bak
       echo "this is a test file " >/test/test.txt 
else
       touch /test/test.txt
      echo "this is a test file " >/test/test.txt 
fi

4.执行该脚本

我的minion有两台,分别是

[root@test6_100 script]# salt-key -L
Accepted Keys:
test6_101
test6_102

Denied Keys:
Unaccepted Keys:

下面将操作的是master服务器,将脚本的内容在这两台minion上执行

salt "*" cmd.script salt://script/test.sh

test6_102:
    ----------
    pid:
        2333
    retcode:
        0
    stderr:
    stdout:
test6_101:
    ----------
    pid:
        3325
    retcode:
        0
    stderr:
    stdout:

执行完不会报任何的error,如果有error证明有错误,需要查看脚本后,在进行操作

5.查看结果

分别登陆到这两台服务器中,找到/test/test.txt,并查看里面的内容,如果是this is a test file,那么命令就执行成功了

 

 

如果真的有error的错误的话,查看下minion的日志

日志位置查看配置文件

#####         Logging settings       #####
##########################################
# The location of the minion log file
# The minion log can be sent to a regular file, local path name, or network
# location. Remote logging works best when configured to use rsyslogd(8) (e.g.:
# ``file:///dev/log``), with rsyslogd(8) configured for network logging. The URI
# format is: <file|udp|tcp>://<host|socketpath>:<port-if-required>/<log-facility>
#log_file: /var/log/salt/minion
#log_file: file:///dev/log
#log_file: udp://loghost:10514
#
#log_file: /var/log/salt/minion
#key_logfile: /var/log/salt/key

配置文件中默认的位置是/var/log/salt/minion

tail -200 /var/log/salt/minion

查看下具体的报错

 

 

posted @ 2018-08-20 10:40  zclinux  阅读(577)  评论(0)    收藏  举报