[英]Ansible replace or lineinfile?
所有,
我一直在努力尋找解決辦法,我可以使用shell模塊進行sed操作,但無法使用更本地的方式(例如replace或lineinfile)來完成。 這就是我想要做的:
我有一個文件,其中有幾行,有些行以不同的字符串開頭,而其他幾行以我要編輯的相同,那么所有的行在某個點上都有特定的字符串,我只想編輯此字符串以該特定字符串開頭的行。
這是一個更直觀的示例:
順便說一下,文件是/ etc / fstab,看起來像這樣
Aaaaa:/mount1 /mountpoint1 nfs mount-options 0 0
Aaaaa:/mount2 /mountpoint4 nfs mount-options 0 0
Bbbbbn:/mount1 /mountpoin9 nfs mount-options 0 0
正如您在本示例中看到的那樣,我要更改的安裝選項是相同的,但是我只想編輯以aaaa開頭的行中的選項。
有什么線索如何使用諸如replace或lineinfile之類的模塊而不是使用Shell來執行此操作?
謝謝
更新:讓我澄清一下,aaaaa之后的內容:直到nfs mount-options 0 0可以是任何東西,因為服務器上的安裝文件夾和安裝點可以是任何東西。 這也是我使用shell模塊使它工作的方式,仍然在尋找一個更本地的模塊而不是shell:
- name: Inserting new mounting options
shell: sed -i '/^aaaaa/ s/mount-options/new-mount-options/g' /etc/fstab
在上面的示例中,我成功地定位了以aaaaa開頭的行,並將mount-options替換為不同的選項,同時保持0 0到此結束。
替換更適合用例。 請參見下面的示例。 例如,讓我們
dump fsck
設置為1 1
,其中行以Aaaaa
開頭 fstype
設置為ext4
,其中行以Bbbbbn
下面的任務
- replace:
path: /scratch/fstab
regexp: "{{ item.regexp }}"
replace: "{{ item.replace }}"
loop:
- regexp: '^Aaaaa(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)(.*)$'
replace: 'Aaaaa\1 \2 \3 \4 1 1'
- regexp: '^Bbbbbn(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)(.*)$'
replace: 'Bbbbbn\1 \2 ext4 \4 \5 \6'
給
$ cat fstab
Aaaaa:/mount1 /mountpoint1 nfs mount-options 1 1
Aaaaa:/mount2 /mountpoint4 nfs mount-options 1 1
Bbbbbn:/mount1 /mountpoin9 ext4 mount-options 0 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.