简体   繁体   English

如何用Ansible Playbook替换具有多个特殊字符的行

[英]How to replace a line which has multiple special characters with ansible playbook

Trying to replace a line which has multiple special characters, which gets interpreted as escape characters by ansible. 尝试替换具有多个特殊字符的行,该行被ansible解释为转义字符。

Tried using "\\" and "." 尝试使用“ \\”和“。” for every character, and tried using !unsafe. 每个字符,并尝试使用!unsafe。

- name: Update file
  replace:
    path: /some/file
    regexp: "*[0-9a-zA-Z._-]* )" #<<=== This is line to be replaced
    replace: "*[0-9a-z._-]* )"   #<<== With this
    backup: yes

Getting errors like: 出现如下错误:

raise error, v # invalid expression\\r\\nsre_constants.error: nothing to repeat\\r\\n", "msg": "MODULE FAILURE\\nSee stdout/stderr for the exact error", "rc": 1} 引发错误,v#无效表达式\\ r \\ nsre_constants.error:无需重复\\ r \\ n“,” msg“:”模块失败\\ n请参见stdout / stderr了解确切错误“,” rc“:1}

You should escape the special characters with one backslash each: 您应该对特殊字符进行转义,每个特殊字符之间应加一个反斜杠:

regexp: '\*\[0\-9a\-zA\-Z\._\-\]\* \)'

And you should use single quotes. 并且您应该使用单引号。

Tried this and it worked: 尝试了一下,它起作用了:

regexp: '\*\[0-9a-zA-Z\._-\]\* \)'
replace: '*[0-9a-z._-]* )'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM