Regex is stripping spaces

jjill007 asked:

I need help fixing regex expression. Its stripping the preceding white spaces after writing to output file.
here is the code

  hosts: serv1
  tasks:
    - name: Rename VM hostname
      block:
        - command: hostname -f
          register: result
        - lineinfile:
            path: /home/home/setup.yml
            state: present
            regexp: 'URL: (.*)$'
            line: "URL: 'https://{{ result.stdout }}/key/auth'"

the expected output is

    KEY_USER: ‘KEY_ADMIN'
    KEY_PASS: “KEY_PASS"
    URL: 'https://serv1/key/auth'
    KEY_CLIENT: “CLIENT_KEY"
    LDAP_URL: ‘LDAP_URL'

but I get

    KEY_USER: ‘KEY_ADMIN'
    KEY_PASS: “KEY_PASS"
URL: 'https://serv1/key/auth'
    KEY_CLIENT: “CLIENT_KEY"
    LDAP_URL: ‘LDAP_URL'

Any idea on how to fix it?
Thanks

My answer:


The regex is not stripping spaces. You stripped them.

You wrote that the replacement line should not have any leading spaces:

            line: "URL: 'https://{{ result.stdout }}/key/auth'"

If you want leading spaces, you need to put them in. It might look like this:

            line: "    URL: 'https://{{ result.stdout }}/key/auth'"

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.