每日学习-ansible replace模块

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

猜你喜欢

转载自blog.51cto.com/jiayimeng/2593956