1、文件传送:

salt自带有cp模块,模块的方法可以用下面的命令查看下

salt '*' sys.doc | grep "salt '\*' cp"


我可以这样发送一个文件给客户端:

root@ubuntu:/srv/salt# salt '*' cp.get_file salt://test/test.sh.jinja /root/test.sh
192.168.1.101:
    /root/test.sh

salt的主目录在/srv/salt下面,这个可以在配置文件里设定,我上面就是把/srv/salt/test/test.sh.jinja这个文件推送到客户端去


2、文件追加:

使用file模块的append方法:

root@ubuntu:/srv/salt# salt '*' file.append /root/test.sh "adfadf"                
192.168.1.101:
    Wrote 1 lines to "/root/test.sh"

这样可以轻松把内容添加到文件上去,file还有很多方法,具体可以看看源码


如果文件零碎这样的方法明显不太科学,还有如果新加了机器你还要为他们单独推送


下面salt提供的sls模板来实现下

先在/srv/salt目录下建立一个入口文件

root@ubuntu:/srv/salt# cat top.sls
base:
    '*':
      - scpfile.my

第二行是一个标志

第三行是指定机器

第四行就是去执行scpfile目录下my.sls这个文件

root@ubuntu:/srv/salt/scpfile# cat my.sls
/root/test.sh:
    file.managed:
      - source: salt://scpfile/test.sh
      - mode: 755

第二行是客户端文件位置

第三行是salt函数

第四行是源文件

第五行是指文件的权限控制

然后在scpfile目录下新建一个test.sh

最后我们来推送一下:

root@ubuntu:/srv/salt/scpfile# salt '*' state.highstate -v
Executing job with jid 20131031164349965881
-------------------------------------------
192.168.1.101:
----------
    State: - file
    Name:      /root/test.sh
    Function:  managed
        Result:    True
        Comment:   File /root/test.sh updated
        Changes:   diff: New file
                   mode: 755
                                                              
Summary
------------
Succeeded: 1
Failed:    0
------------
Total:     1


追加内容:

root@ubuntu:/srv/salt/scpfile# vi my.sls                 
/root/test.sh:
    file.managed:
      - source: salt://scpfile/test.sh
      - mode: 755
/root/test.sh:
    file.append:
      - text:
        - "11111111111111"
root@ubuntu:/srv/salt/scpfile# salt '*' state.highstate -v
Executing job with jid 20131031165032612196
-------------------------------------------
192.168.1.101:
----------
    State: - file
    Name:      /root/test.sh
    Function:  append
        Result:    True
        Comment:   Appended 1 lines
        Changes:   diff: --- 
+++ 
@@ -1,3 +1,4 @@
 adfkdddddd
 ddddddddddd
 ddd
+11111111111111
                                                        
Summary
------------
Succeeded: 1
Failed:    0
------------
Total:     1


sed修改内容:

root@ubuntu:/srv/salt/scpfile# vi my.sls
      - textdd:
/root/test.sh:
    file.managed:
      - source: salt://scpfile/test.sh
      - mode: 755
/root/test.sh:
    file.append:
      - text:
        - "11111111111111"
/root/test.sh:
    file.sed:
      - before: 11111111111111
      - after: 2222
--------------------------------------------------------
root@ubuntu:/srv/salt/scpfile# salt '*' state.highstate -v
Executing job with jid 20131031165305244140
-------------------------------------------
192.168.1.101:
----------
    State: - file
    Name:      /root/test.sh
    Function:  sed
        Result:    True
        Comment:   sed ran without error
        Changes:   diff: --- 
+++ 
@@ -1,4 +1,4 @@
 adfkdddddd
 ddddddddddd
 ddd
-11111111111111
+2222
                                       
Summary
------------
Succeeded: 1
Failed:    0
------------
Total:     1


好像管理起来挺方便的~~~~明天再试试别的功能。。。