Need support for a task using lineinfile module

I am trying to replace the line
using replace & lineinfile modules. But it doesn’t work as expected.


  • name: Check line present or not
    shell: grep ‘^${}’ /app/IBM/SSC/log4j2.xml
    register: grep_output
  • name: Replace a line with special characters
    path: /app/IBM/SSC/log4j2.xml
    regexp: ‘^${}’
    line: ‘/app/IBM/logs/SSC’
    backrefs: no
    when: grep_output.rc == 0

The first task with shell module works fine but second one adds a new line at bottom of the file, instead of replacing it.
I did try adding state=absent argument in the second task, but still no luck. Could you suggest. Thanks!

On which lab did you experience this issue?

It is not related to the lab. I am trying to implement lineinfile/replace modules at my work place.