Ansible "replace" & "lineinfile" issue

30 views
Skip to first unread message

Kanhaiya Ashtekar

unread,
Aug 16, 2018, 2:30:32 AM8/16/18
to Ansible Project
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"
---
- name: Comment out *.*@10.1.0.100 line in /etc/rsyslog.conf
  lineinfile:
      path: /etc/rsyslog.conf
      regexp: '^*\.*@10\.1\.0\.100)'
      line: '# *.* @10.1.0.100'


YAML using "replace"
---

- name: Comment out *.*@10.1.0.100 line in /etc/rsyslog.conf
  replace:
      path: /etc/rsyslog.conf
      regexp: '*.* @10.1.0.100'
      replace: '# *.* @10.1.0.100'
      

ansible_issue.PNG



PLEASE HELP. 
####################################################################################
## CHECKED WITH ANSIBLE 2.6 DOCUMENTATION. FOLLOWED THE GIVEN INFORMATION. #####
## YET HAVING ISSUE WITH PARSING THE "REGEXP" and "REPLACE"ing                                 #####
####################################################################################

Dick Visser

unread,
Aug 16, 2018, 6:29:42 AM8/16/18
to ansible...@googlegroups.com
On 16 August 2018 at 08:30, Kanhaiya Ashtekar
<ashtekar...@gmail.com> wrote:
>
> 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"
> ---
> - name: Comment out *.*@10.1.0.100 line in /etc/rsyslog.conf
> lineinfile:
> path: /etc/rsyslog.conf
> regexp: '^*\.*@10\.1\.0\.100)'
> line: '# *.* @10.1.0.100'
>



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:


- name: Comment out *.*@10.1.0.100 line in /etc/rsyslog.conf
lineinfile:
path: /etc/rsyslog.conf
regexp: '^(\*\.\*@10\.1\.0\.100)'
line: '#\1'
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
Reply all
Reply to author
Forward
0 new messages