5

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
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Bob
  • 51
  • 1

1 Answers1

1

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'
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83