Check if line matches the regex in lineinfile module

Would it be possile to print a warning if the regexp of lineinfile wont match the given line?

It think there is almost never an usecase where the regex wont match the line so maybe adding an option to the lineinfile module to check the regex by default would be nice.

According to the documentation regexp is not required. In fact I have some interesting use cases where I use lineinfile without regexp. How would you handle those?

It’s a bit of a hack, and I could have replaced the file entirely, but it’s so that I can then use the cron module to then modify a given entry that was not originally created by Ansible…

Add an entry to the cron file to make sure that the line only occurs once

  • name: Verify the crontab file is managed by Ansible
    lineinfile: ‘backup=no dest=/etc/cron.d/sysstat state=present
    line=“#Ansible: sar” insertbefore=“[1].*/usr/lib/sa/sa1”’

So does that work as a counter use case for you?

Adam


  1. ^# ↩︎