[英]Ansible lineinfile replace line after regex
我的文件有很多不同的條目。 看起來像這樣:
- file: url: filestore://20180205/group2/test123.xml sha1: b856565abe4d9416323d82fce9005db29e9a6c57 dest_folder: group-2 ensure: present - file: url: filestore://20180213/group2/othertest.xml sha1: ec9d3d82232c05f7b49a034dcec9ea83f5c1981a dest_folder: group-2 ensure: present
現在,我需要使用新數據更改url和sha1行。 我可以使用lineinfile模塊更改url行,因為我知道文件的組和名稱,但是我不知道sha1值。
那么如何更改sha1線?
我這樣嘗試過:
---
- name: regex Test
hosts: localhost
become: no
tasks:
- name: regex two lines
lineinfile:
dest: /tmp/testdatei
regexp: 'group2/test123.xml'
line: ' url: filestore://gpv/hal/20180213/group2/test1'
- name: regex two lines
lineinfile:
dest: /tmp/testdatei
regexp: 'group2/test123.xml
sha1: '
line: ' sha1: test'
Ansible在文件末尾添加新的sha1行,不要將其替換為當前的sha1行。
使用Ansible 更換模塊
- name: regex two lines
replace:
dest: /tmp/testdatei
regexp: '^(\s*url: ).*group2/test123.xml\n(\s*sha1: ).*$'
replace: '\1filestore://gpv/hal/20180213/group2/test1\n\2test'
backup: yes
我現在找到了解決方案。
---
- name: replace url and hash
replace:
path: /tmp/testdatei
regexp: 'test1\n[^\n]+'
replace: 'FileName\nsha1hash'
- name: regex url
lineinfile:
dest: /tmp/testdatei
regexp: 'FileName'
line: ' url: filestore://gpv/hal/{{current_date}}/{{group_name}}/{{file_name}}'
- name: regex hash
lineinfile:
dest: /tmp/testdatei
regexp: 'sha1hash'
line: ' sha1: {{sha1var.stdout}}'
我這樣做是因為,必須正確地相同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.