file.replace

一、简介

salt file.replace 文件内容处理函数,类似于ansible的lineinfile模块

 

二、参数介绍

name

  被编辑文件的绝对路径,支持软链接

pattern

  常规表达式,使用python的search函数进行匹配

repl

  新替换的文件内容

count

  替换发生的次数,默认为0全部替换

flags

  re module定义的flags,格式为list,such as ['IGNORECASE', 'MULTILINE']

append_if_not_found

  默认: False,在文件末尾添加

prepend_if_not_found

  默认: False,在文件开始处添加

not_found_content

  默认: None,使用repl参数的内容

  使用此处内容进行替换,如果匹配的文本内容没有找到,并且append_if_not_found或prepend_if_not_found设置成True,

  如果repl使用反向引用,那么就要设置not_found_content

backup

  在编辑文件之前,对文件进行备份(没有改动的文件不会做备份操作)

  备份在当前文件目录下,后缀名为你在backup中定义的

show_changes

  True: 输出新旧文件内容的差异处

  False: 如果对文件有改动,返回一个bool值

 

三、Example

/tmp/sshd_config:
  file.replace:
    - pattern: "^(PermitRootLogin) no"
    - repl: '\1 yes'
    - backup: .bak20181024
    - show_changes: True
    - append_if_not_found: True
    - not_found_content: "PermitRootLogin no"

 

PS: salt version / 2016.11

 

posted @ 2018-10-23 16:33  metasequoia  阅读(2095)  评论(0编辑  收藏  举报