Buzut Buzut - 1 year ago 146
YAML Question

Ansible replace and brackets "["

I'm tryong to modify a config file with Ansible, but as brackets are considered as regex element, they don't seem to be taken into account.

But if I try to escape them, I have a syntax error… How is it supposed to work?

# does nothing
regexp: "# unicorn['worker_processes'] = 2"

# error
regexp: "# unicorn\['worker_processes'\] = 2"

# error
regexp: '# unicorn\[\'worker_processes\'\] = 2'

Answer Source

The regexes you're talking about are in string literals. That means you should escape the backslashes that come before square brackets like this:

regexp: '# unicorn\\[\'worker_processes\'\\] = 2'

Even better:

regexp: "# unicorn\\['worker_processes'\\] = 2"