Help with regex, replace, and quotes

526 views
Skip to first unread message

David Reagan

unread,
May 8, 2014, 3:56:04 PM5/8/14
to ansible...@googlegroups.com
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 <module>
    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!

James Cammarata

unread,
May 9, 2014, 1:52:42 AM5/9/14
to ansible...@googlegroups.com
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).



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7c2f93f5-b7f7-45e2-8af5-2b2d271f5e62%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages