[英]Ansible “win_lineinfile” two changes to make on lines where originals are identical
[英]Escape comma in ansible win_lineinfile module
我有一個 PowerShell 腳本文件: C:\Workdir\createLog.ps1
我想注釋掉這一行Remove-Item -Force ($Path + '*' + $Suffix);
在腳本中
我使用 ansible 使用win_lineinfile
模塊自動執行此操作。 代碼是這樣的:
- name: Comment out content
win_lineinfile:
path: "C:\\Workdir\\createLog.ps1"
regexp: '^(\s+)Remove-Item -Force \(\$Path \+ \'\*\' \+ \$Suffix\);'
line: '$1# Remove-Item -Force ($Path + '*' + $Suffix);'
backrefs: yes
但它返回一個錯誤,因為'*'
不能在正則表達式中作為字符串傳遞
似乎您想在帶有Remove-Item -Force ($Path + '*' + $Suffix);
的行之前找到帶有任意數量空格的行。 內容並在前導空格后添加#
+ 空格。
您可以使用
regexp: '^(\s*)(Remove-Item -Force \(\$Path \+ \'\*\' \+ \$Suffix\);)'
line: '\1#\2'
backrefs: yes
請參閱此正則表達式演示。 backrefs
參數中的\1#\2
指定替換應為 Group 1 值 + #
+ Group 2 值。
模式匹配
^
- 字符串/行的開頭(\s*)
- 第 1 組 ( \1
):零個或多個空格(Remove-Item -Force \(\$Path \+ \'\*\' \+ \$Suffix\);)
- 第 2 組 ( \2
): Remove-Item -Force ($Path + '*' + $Suffix);
細繩。 您可能想用\s+
(一個或多個)或\s*
(零個或多個空格)替換文字空格以匹配任何類型的空格。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.