replace模块经常使用参数python
• 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:修改源文件前建立一个包含时间戳信息的备份文件git
replace模块doc文档示例github
- 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 replaceexpress