replace模块用于在文件中根据指定的正则表达式替换匹配的内容

replace模块常用参数

• path:必须参数,指定要修改的文件,2.3版本之前,这个参数叫dest、destfile、name;现在这三个名称是path参数的别名
• regexp:必须参数,指定一个正则表达式,可以是python正则
• replace:替换regexp参数匹配到的字符串,
• owner:结果文件或目录的所属用户名,相当于chown命令修改
• group:结果文件或目录的所属组名,相当于chown命令修改
• mode:结果文件或目录的权限,与chmod命令不一致的是,replace模块的mode参数需要添加前导零,以便ansible的YAML解析器知道它是八进制;1.8版本后可以设置为符号模式(u+rwx或u=rw),2.6版本后可以是特殊字符串(preserve),当设置为'preserve'时,文件将被赋予与源文件相同的权限。
• others:可以指定file模块的所有参数
• encoding:用于读取和写入文件的字符编码
• before:如果指定,则仅替换/删除此匹配之前的内容,可以和after参数结合使用
• after:如果指定,则仅替换/删除此匹配之后的内容,可以和before参数结合使用
• attributes:结果文件或目录的特殊属性,相当chattr,默认使用=运算符,指定文件或目录的某项属性
• backup:修改源文件前创建一个包含时间戳信息的备份文件

replace模块doc文档示例

- name: Before Ansible 2.3, option 'dest', 'destfile' or 'name' was used instead of 'path'  replace:    path: /etc/hosts    regexp: '(\s+)old\.host\.name(\s+.*)?$'    replace: '\1new.host.name\2'- name: Replace after the expression till the end of the file (requires Ansible >= 2.4)  replace:    path: /etc/apache2/sites-available/default.conf    after: 'NameVirtualHost [*]'    regexp: '^(.+)$'    replace: '# \1'- name: Replace before the expression till the begin of the file (requires Ansible >= 2.4)  replace:    path: /etc/apache2/sites-available/default.conf    before: '# live site config'    regexp: '^(.+)$'    replace: '# \1'# Prior to Ansible 2.7.10, using before and after in combination did the opposite of what was intended.# see https://github.com/ansible/ansible/issues/31354 for details.- name: Replace between the expressions (requires Ansible >= 2.4)  replace:    path: /etc/hosts    after: '<VirtualHost [*]>'    before: '</VirtualHost>'    regexp: '^(.+)$'    replace: '# \1'- name: Supports common file attributes  replace:    path: /home/jdoe/.ssh/known_hosts    regexp: '^old\.host\.name[^\n]*\n'    owner: jdoe    group: jdoe    mode: '0644'- name: Supports a validate command  replace:    path: /etc/apache/ports    regexp: '^(NameVirtualHost|Listen)\s+80\s*$'    replace: '\1 127.0.0.1:8080'    validate: '/usr/sbin/apache2ctl -f %s -t'- name: Short form task (in ansible 2+) necessitates backslash-escaped sequences  replace: path=/etc/hosts regexp='\\b(localhost)(\\d*)\\b' replace='\\1\\2.localdomain\\2 \\1\\2'- name: Long form task does not  replace:    path: /etc/hosts    regexp: '\b(localhost)(\d*)\b'    replace: '\1\2.localdomain\2 \1\2'- name: Explicitly specifying positional matched groups in replacement  replace:    path: /etc/ssh/sshd_config    regexp: '^(ListenAddress[ ]+)[^\n]+$'    replace: '\g<1>0.0.0.0'- name: Explicitly specifying named matched groups  replace:    path: /etc/ssh/sshd_config    regexp: '^(?P<dctv>ListenAddress[ ]+)(?P<host>[^\n]+)$'    replace: '#\g<dctv>\g<host>\n\g<dctv>0.0.0.0'

replace模块示例

- name: replace hostname  replace:     path: /root/hwreport.txt    regexp: "inventoryhostname"    replace: "{{ ansible_hostnam }}"- name: replace disk vdb  replace:     path: /root/hwreport.txt    regexp: "disk_vdb_size"    replace: "{{ ansible_device.vdb.size }}"  when: "'vdb' in ansible_devices"- name: replace disk vdb  replace:     path: /root/hwreport.txt    regexp: "disk_vdb_size"    replace: "NONE"  when: "'vdb' not in ansible_devices"

引自:ansible-doc replace

更多相关文章

  1. 全文检索引擎 Solr 系列(3)— Solr 核心概念、配置文件
  2. 使用Rails 3.2和AJAX(非flash上传解决方案)将多个文件直接上载到
  3. 使用ajax请求上传文件[副本]
  4. 使用JQuery从外部文件中通过id选择器获取html内容
  5. GET ajax请求发送到同一个php文件
  6. Jquery Validate 相关参数及常用的自定义验证规则
  7. 无法使用jquery发送简单的ajax请求来获取json文件
  8. 在上传前预览xls、xlsx、doc等文件的缩略图
  9. 静态h5页面没有后台直接访问本地json文件

随机推荐

  1. 解决Apache2+PHP上传文件大小限制的问题
  2. 如何在PHP 5.4.33 Apache服务器上启用mbs
  3. 解析错误:语法错误,E:\xampp\htdocs\on
  4. 访问控制允许原点 - 不工作
  5. 如何使用ChromePhp Logger记录数组
  6. 最佳做法,PHP,每天跟踪数百万次展示
  7. 使用PHP和MySQL开发“测试”Web应用程序
  8. 如何获取关联数组的当前元素数?
  9. 如何使用PHP读取.xls文件(Excel) ?
  10. 在返回按钮上重新加载页面。