Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible replace and brackets "["

I'm trying 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'
like image 329
Buzut Avatar asked Sep 04 '16 21:09

Buzut


1 Answers

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"
like image 107
dorukayhan Avatar answered Sep 20 '22 10:09

dorukayhan