Replace indented string in file

276 views
Skip to first unread message

Tony

unread,
Jan 10, 2022, 7:03:55 AM1/10/22
to Ansible Project
I am trying to replace a specific part of an indented string, but I cannot get it working with lineinfile/replace modules for some reason.

What I would like:

before:        Substitute "s~(src|href)=('|\")/((?!version).*)~$1=$2/version//$3~i"

after:            Substitute "s~(src|href)=('|\")/((?!version).*)~$1=$2/9.1.2.3/$3~i"

Anyone help me out with this one please?

Cheers!

Dick Visser

unread,
Jan 10, 2022, 3:54:56 PM1/10/22
to ansible...@googlegroups.com
Are those actual entire lines of content?
Because they contain regex patterns. 

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/00b8d501-d0f1-470f-88e5-ebb6ddec2196n%40googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Tony

unread,
Jan 11, 2022, 3:36:57 AM1/11/22
to Ansible Project
Hey, 
Are those actual entire lines of content?
Yes, they are the enter lines of the content. It is part of an application configuration file.

Because they contain regex patterns. 
Yes and I think this is why I cant get it working

Any pointers would be great, I just need to insert a replace "version" with a version number i.e. 9.1.2.3

Dick Visser

unread,
Jan 11, 2022, 6:54:03 AM1/11/22
to ansible...@googlegroups.com
I think you misunderstood me.
Are those lines you put there the same as in your source file?
Because I think they're not. 

Instead of posting what appear to be randomly selected lines of a playbook:

* post the actual playbook/task that you have
* post a snippet of the real file content that you want to change






--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Tony

unread,
Jan 11, 2022, 11:30:29 AM1/11/22
to Ansible Project
I have a file called "bla.conf" which contains this indented line Substitute "s~(src|href)=('|\")/((?!version).*)~$1=$2/version//$3~i"

I want to change the indented line from bla.conf to Substitute "s~(src|href)=('|\")/((?!version).*)~$1=$2/NEWVERSIONHERE//$3~i"  

Dick Visser

unread,
Jan 11, 2022, 12:11:26 PM1/11/22
to ansible...@googlegroups.com
Ah OK.
In such cases it is very hard to get the escaping right by hand, so what I usually do is use a helper task using regex_escape.
This lets ansible to the escaping for you. Once you have that escaped regex, the only thing you need to do, is to add parenthesis:

---

- hosts: localhost

  connection: local

  gather_facts: no


  vars:

    path: /tmp/out.txt

    original_line: Substitute "s~(src|href)=('|\")/((?!version).*)~$1=$2/version//$3~i"

    new_version: v3.78.1


  tasks:


    # Helper task to escape the entire line

    - debug: var=original_line|regex_escape



    - replace:

        path: "{{ path }}"


        # This regexp has the verbatim output of the previous debug task, it is used to confirm a match

        #regexp: Substitute\ "s\~\(src\|href\)=\('\|\\"\)/\(\(\?!version\)\.\*\)\~\$1=\$2/version//\$3\~i"$


        # This is the same, but with the part before and after "version" in parenthesis

        regexp: ^(Substitute\ "s\~\(src\|href\)=\('\|\\"\)/\(\(\?!version\)\.\*\)\~\$1=\$2/)version(//\$3\~i")$


        replace: '\1{{ new_version }}\2'


~                                                                                                                   

~                                                                                                                   

regex_replace2.yml                                                                                18,18          All

"regex_replace2.yml" 27L, 807B written



Of course this is done using the literal string "version" - so you'd have to adjust that if you want to match something else.


Dick


Reply all
Reply to author
Forward
0 new messages