使用ansible在文件中查找带有$的单词

问题描述 投票:0回答:1

我正在尝试在文件中查找单词,并使用ansible替换它。

问题是单词包含结尾的正则表达式特殊字符$-$ wordToFind。

已经尝试使用[$] wordToFind或\ $ wordToFind对其进行转义。这是我正在使用的代码:

- name: Check if the /pathToFile/file.ext file contains the entry value $wordToFind
shell: "cat /pathToFile/file.ext | grep  \"schwordToFindeme\""
become: yes
become_method: sudo
failed_when: false
register: cat_result
when: condition == "A"

- name: change to newWord if $wordToFind was found
replace:
    dest: //pathToFile/file.ext
    regexp: "$wordToFind"
    replace: "newWord"
    backup: yes
when: cat_result.rc > 0 and condition == "A"
regex ansible
1个回答
0
投票

尝试更改为单引号。下面的一个为我工作

  - name: change to newWord if $wordToFind was found
    replace:
     dest: //pathToFile/file.ext
     regexp: '\$wordToFind'
     replace: 'newWord'
© www.soinside.com 2019 - 2024. All rights reserved.