最近一直在使用saltstack,每次使用到file模块都还需要去查文档,今天在此记录,以便以后使用

文档:http://docs.saltstack.com/ref/states/all/salt.states.file.html

本文中用的top.sls

/srv/salt/top.sls
test:
  'minion1':
    - file

创建
1. 空文件

/srv/salt/test/file.sls 
/tmp/a.txt:
  file.managed

2. 根据模板文件生成文件

/srv/salt/test/file.sls
/tmp/a.txt:
  file.managed:
    - source: salt://files/a.txt

/srv/salt/test/files/a.txt
just a test

3. 根据变量生成文件

/srv/salt/test/file.sls
/tmp/a.txt:
  file.managed:
    - source: salt://files/a.txt
    - template: jinja
/srv/salt/test/files/a.txt
{{ pillar['os'] }}

4. 创建多个文件

/srv/salt/test/file.sls
{% set create_files = ['/tmp/a', '/tmp/b', '/tmp/c'] %}
{% for eachfile in create_files %}
{{eachfile}}:
  file.managed:
    - source: salt://files/tmp
    - template: jinja 
{% endfor %}

删除

/srv/salt/test/file.sls
/tmp/a.txt:
  file.absent

添加内容

/srv/salt/test/file.sls
  /tmp/a.txt:
    file.append:
      - text:
        - 'this is append line1'
        - 'this is append line2'
 

重命名

/srv/salt/test/file.sls
  /tmp/new:
    file.rename:
      - source: /tmp/old

目录

/srv/salt/test/file.sls
  /var/log/httpd:
    file.directory:
    - user: root
    - group: root
    - dir_mode: 755
    - file_mode: 644
    - recurse:
        - user
        - group
        - mode
sed
/srv/salt/test/file.sls

/etc/yum.repos.d/epel.repo:
  file.sed:
    - before: 1
    - after: 0
    - limit: ^enabled=

软连

/srv/salt/test/file.sls
  /tmp/new:
    file.symlink:
      - target: /tmp/old

是否存在

/srv/salt/test/file.sls
  /tmp/new:
    file.exists


分享到: 更多

这篇日志的 QR 二维码为:

五月 4th, 2014

Posted In: 网络技术

发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量