Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible regexp ignore lines commented out

Tags:

ansible

I am attempting to comment out below line using Ansible. I am running into an issue where a comment keeps getting added every time the playbook is run.

How can I change my regexp to ignore the line if already commented out?

- replace:
      path: /etc/rsyslog.conf
      regexp: '(.*@hostname.*)'
      replace: '#\1'
  notify:
      - Restart rsyslog
like image 279
Bob Avatar asked Nov 05 '22 23:11

Bob


1 Answers

You will need a negative look-ahead for this, to exclude line starting with a sharp (#).

So, your regex should be ^(?!#)(.*@hostname.*) and your task would end up being:

- replace:
    path: /etc/rsyslog.conf
    regexp: '^(?!#)(.*@hostname.*)'
    replace: '#\1'
like image 124
β.εηοιτ.βε Avatar answered Nov 11 '22 12:11

β.εηοιτ.βε