简体   繁体   中英

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.

Tried using "\\" and "." for every character, and tried using !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}

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._-]* )'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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