# Ansible "replace" & "lineinfile" issue

**URL:** <https://forum.ansible.com/t/ansible-replace-lineinfile-issue/28340>\
**Category:** Ansible Project\
**Created:** [August 16, 2018, 6:30am UTC](https://forum.ansible.com/t/ansible-replace-lineinfile-issue/28340 "2018-08-16T06:30:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kanhaiya\_Ashtekar](https://avatars.discourse-cdn.com/v4/letter/k/e274bd/32.png) [@Kanhaiya\_Ashtekar](https://forum.ansible.com/u/Kanhaiya_Ashtekar)\
**Post date:** [August 16, 2018, 6:30am UTC](https://forum.ansible.com/t/ansible-replace-lineinfile-issue/28340/1 "2018-08-16T06:30:32Z")

</div>

I am having a requirement where I need to replace/comment a line from rsyslog.conf file  
I am unable to correctly state the regexp. Please Help.

INADEQUATE INFORMATION AVAILABLE ONLINE.

I have been testing the following script which is failing:

YAML using “lineinfile”

> **(attachments)**
>
> ![ansible_issue.PNG](https://us1.discourse-cdn.com/ansible/original/2X/f/f8c5e1a1b421994234aeb66a7b7b150958c0ed31.png)

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [August 16, 2018, 10:29am UTC](https://forum.ansible.com/t/ansible-replace-lineinfile-issue/28340/2 "2018-08-16T10:29:08Z")

</div>

Your regex has a closing parenthesis but no starting one.  
Also, the wildcard character needs to be escaped.  
If your goal is to comment out whatever you matched first, then it  
makes sense to use backrefs in the substituted line.  
You should also use the $ to match the end of the string.  
In this specific case it won't be a problem (i.e. there are probably  
no instances like \*.\*@10.1.0.1000), but suppose you wanted to match:

\*.\*@10.1.0.10

then without the $ this would also match \*.\*@10.1.0.100, \*.\*@10.1.0.104, etc

This should work for you:

&nbsp;&nbsp;&nbsp;&nbsp;- name: Comment out \*.\*@10.1.0.100 line in /etc/rsyslog.conf  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lineinfile:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: /etc/rsyslog.conf  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: '^(\*\.\*@10\.1\.0\.100)'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: '#\1'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backrefs: yes

BTW that screen capture is huge and makes it hard to read the  
message... you might want to just copy/paste the text next time...

Dick
