ansible 正则表达式替换具有特殊字符的字符串

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

我需要将配置文件中的一个字符串(具有特殊字符,如 {,(, ] 等)替换为另一个字符串。

尝试使用下面的替换模块执行时出现一些语法错误:

- name: update password in config file
  become: true
  become_user: "{{ my_user }}"
  replace:
    path: /var/tmp/config.xml
    regexp: "{{ old_pwd.stdout }}"
    replace: "{{ new_pwd.stdout }}"
    backup: true

错误信息:

An exception occurred during task execution. To see the full stack trace use -vvv. The error was: re.error: missing ), unterminated subpattern at position 5

仅当正则表达式值具有特殊字符时,上述剧本才会失败,例如

gT5{t4(dfR]p

输入有特殊字符时如何解决这个问题

regex replace ansible
1个回答
0
投票

您可以使用

regex_escape
过滤器来转义正则表达式模式中的特殊字符:

regexp: "{{ old_pwd.stdout }} | regex_escape()"
© www.soinside.com 2019 - 2024. All rights reserved.