Help with regex, replace, and quotes

I’ve been trying to use replace to replace config lines in a config file that I have good reasons not to template.

Specifically, it’s php associative array. Thus, I need to be able to match lines that contain quotes.

So far, I can’t get it to work.

I eventually pulled the essentials out of my role and into a single playbook that I’ve posted on pastebin http://pastebin.com/Y6X5dZqz

So, if a file contains

$CONFIG = array (
‘dbhost’ => ‘laskdjflkjsdlfkj’,
);

How do I replace ‘dbhost’ => ‘whatever the setting happens to be.’, with whatever I want? Also, the single quotes can sometimes be double quotes.

I generally get an error that looks something like:

fatal: [192.168.88.2] => failed to parse: SUDO-SUCCESS-wlskunaawgbgwiokatisjtavicfvfbux
Traceback (most recent call last):
File “/home/vagrant/.ansible/tmp/ansible-tmp-1399578173.9-133172102011107/replace”, line 1330, in
main()
File “/home/vagrant/.ansible/tmp/ansible-tmp-1399578173.9-133172102011107/replace”, line 123, in main
supports_check_mode=True
File “/home/vagrant/.ansible/tmp/ansible-tmp-1399578173.9-133172102011107/replace”, line 350, in init
(self.params, self.args) = self._load_params()
File “/home/vagrant/.ansible/tmp/ansible-tmp-1399578173.9-133172102011107/replace”, line 901, in _load_params
items = shlex.split(args)
File “/usr/lib/python2.7/shlex.py”, line 279, in split
return list(lex)
File “/usr/lib/python2.7/shlex.py”, line 269, in next
token = self.get_token()
File “/usr/lib/python2.7/shlex.py”, line 96, in get_token
raw = self.read_token()
File “/usr/lib/python2.7/shlex.py”, line 172, in read_token
raise ValueError, “No closing quotation”
ValueError: No closing quotation

My local machine is running Ubuntu 14.04, and Ansible devel branch from a couple days ago.

My testing vagrant vm is Ubuntu 12.04.

Thanks!

This worked for me:

re.compile(r"“”(?:‘dbhost’)\s*(?:=>)\s*'"['“],”“”).match(r"“”‘dbhost’ => ‘laskdjflkjsdlfkj’,“”").groups()[0]
‘laskdjflkjsdlfkj’

It would also match a string with unbalanced quotes like 'foo" but it’s a much longer regex to enforce avoid that (at least in my experience, others may have better regex knowledge).