lineinfile module to replace a string

32 views
Skip to first unread message

Veera

unread,
Jun 19, 2023, 12:47:23 PM6/19/23
to Ansible Project
Hi,

I am trying to replace a  line/string   in a 224   lines file .
Trying with replace and lineinfile module  , but seems i am missing a bit..

below is the string to consider:

logging.files:
  # Configure the path where the logs are written. The default is the logs directory
  # under the home path (the binary location).
  path: /var/log/original

.
Here I want to replace the " /var/log/original"  to " /var/log/newfile"  and I have to search based on the string "logging.files:"  which is 3 lines above.  

- name: Configuring the inputs for the file  
  replace:
    path: /etc/myagent/myfile
    after: 'logging.files:'    here  i want to search 4th  line below the match.. similar to grep -A3)
    regexp: ' path: /var/log/original '
    replace: " path: /var/log/newfile  "

 I have multiple lines with similar entries and I cannot replace directly (based on first match too)

Dick Visser

unread,
Jun 19, 2023, 2:54:54 PM6/19/23
to ansible...@googlegroups.com
What kind of file is this?
It looks like yaml?

--
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/2f54435b-b00c-48b5-b349-53f2953c5725n%40googlegroups.com.
--
Sent from Gmail Mobile

Veera

unread,
Jun 19, 2023, 3:37:15 PM6/19/23
to ansible...@googlegroups.com
Yes.. It's basically another yaml file. 

You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/hIYZwIgSsgc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAF8BbLbPajg-fNs1sc-kW9EtQENxOxsrZQM3hZdmrRu7GgkkEA%40mail.gmail.com.

Dick Visser

unread,
Jun 19, 2023, 4:16:19 PM6/19/23
to ansible...@googlegroups.com
On Mon, 19 Jun 2023 at 21:37, Veera <svee...@gmail.com> wrote:
Yes.. It's basically another yaml file. 

I'm asking because string manipulation of otherwise structured data will become messy quickly - as you have found out.
If you must keep all the comments then you'll have to use logic to handle that.
Whenever someone edits the file, or by some software update, your logic might break.
I would either template the entire thing out, based on a config that you have in your ansible code.
Or, use a combination of slurp and write out the structure. This will remove any comments, but one could argue that that is not required anyway.
For example:

  vars:
    filename: /etc/myagent/myfile
    config_opts:
      logging.files:
        path: /var/log/newfile

  tasks:
    - slurp:
        src: "{{ filename }}"
      register: foo

    - set_fact:
        foo: "{{ foo.content | b64decode | from_yaml | combine(config_opts) }}"

    - copy:
        content: "{{ foo | to_nice_yaml }}"
        dest: "{{ filename }}"

Veera

unread,
Jun 21, 2023, 4:21:27 AM6/21/23
to Ansible Project
Thanks . It worked as expected 
Reply all
Reply to author
Forward
0 new messages