I have some text that I wish to place in quotes [1] in a conf file. Assume for the time being that the string does not contain single or double quotes within it: no ' or ",
[1] Single or double quotes would work here. For simplicity all examples below use single-quotes.
Before:
SOMEVARIABLE=string_that_I_want_in_quotes
After:
SOMEVARIABLE='string_that_I_want_in_quotes'
To this end I am using the replace module, here is what I have tried
- name: modify the file again to add quotes to variable
replace:
path: "/path/to/file"
regexp: '^SOMEVARIABLE=(.+)'
replace: |
SOMEVARIBLE='\1'
The problem is that on a subsequent run of the ansible task, the variable is encapsulated in a second set of single quotes. And on a third run, this process is repeated.
After two (2) ansible runs
SOMEVARIABLE=''string_that_I_want_in_quotes''
After three (3) ansible runs
SOMEVARIABLE='''string_that_I_want_in_quotes'''
I suspect I need to craft some regex, that will somehow not run the replacement if the string is already has single-quotes around it. N.B. A regex that would detect if the string has double-quotes around it would work as well. Is there some regex that would help solve this problem?
Note: I cannot generate the file with quotes around the string to begin with. Instead the file is auto-generated by an installer I do not control, and I am trying to modify the output of said installer with quotes.