Ansible: place quotes around variable, but only if the quotes do not already exist

Viewed 352

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.

2 Answers

Use

- name: modify the file again to add quotes to variable
  replace:
    path: "/path/to/file"
    regexp: "^SOMEVARIABLE='*(.*?)'*$"
    replace: >-
      SOMEVARIABLE='\1'

See regex proof

EXPLANATION

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  SOMEVARIABLE=            'SOMEVARIABLE='
--------------------------------------------------------------------------------
  '*                       '\'' (0 or more times (matching the most
                           amount possible))
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  '*                       '\'' (0 or more times (matching the most
                           amount possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

You can try with below regex and also use a folded block scalar with the - to take care of the newline generated.

- name: modify the file again to add quotes to variable
  replace:
    path: "/path/to/file"
    regexp: "^SOMEVARIABLE=['+]?(.+?)['+]?$"
    replace: >-
      SOMEVARIABLE='\1'
Related