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

Regex 使用ansible查找文件中带$的单词,regex,ansible,Regex,Ansible,我试图在文件中找到一个单词,并用ansible替换它 问题是单词包含结尾的regexp特殊字符$-$wordToFind 已尝试使用[$]wordToFind或\$wordToFind将其转义。 这是我正在使用的代码: - name: Check if the /pathToFile/file.ext file contains the entry value $wordToFind shell: "cat /pathToFile/file.ext | grep \"schwordToFind

我试图在文件中找到一个单词,并用ansible替换它

问题是单词包含结尾的regexp特殊字符$-$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"

尝试更改为单引号。下面一个对我有用

  - name: change to newWord if $wordToFind was found
    replace:
     dest: //pathToFile/file.ext
     regexp: '\$wordToFind'
     replace: 'newWord'

正如我可以推断的那样,您正在尝试制作某种模板系统,对吗?你为什么不干脆用它来代替呢?嗨,阿克迪亚斯!因为文件不是我提供的。。。它存在,我只需要替换一个单词!这只是一个猜测,听起来可能很愚蠢,但您是否尝试过在
\\$
中使用双反斜杠?是否有任何错误?或者可能是你的状况有问题