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