• 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

  • 相关阅读:
    字典
    列表
    重要的方法
    一笔"狗"销,"猪"事顺利!!!
    基础数据类型
    循环,格式化,运算符
    算法——三角形图形
    算法——字母金字塔
    算法——二进制求和
    Python power函数
  • 原文地址:https://www.cnblogs.com/metasequoia/p/9837474.html
Copyright © 2020-2023  润新知