# Check if line matches the regex in lineinfile module

**URL:** <https://forum.ansible.com/t/check-if-line-matches-the-regex-in-lineinfile-module/15339>\
**Category:** Ansible Developer\
**Created:** [May 27, 2014, 3:08pm UTC](https://forum.ansible.com/t/check-if-line-matches-the-regex-in-lineinfile-module/15339 "2014-05-27T15:08:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander\_Thaller](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@Alexander\_Thaller](https://forum.ansible.com/u/Alexander_Thaller)\
**Post date:** [May 27, 2014, 3:08pm UTC](https://forum.ansible.com/t/check-if-line-matches-the-regex-in-lineinfile-module/15339/1 "2014-05-27T15:08:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Adam\_Morris](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Adam\_Morris](https://forum.ansible.com/u/Adam_Morris)\
**Post date:** [May 27, 2014, 6:20pm UTC](https://forum.ansible.com/t/check-if-line-matches-the-regex-in-lineinfile-module/15339/2 "2014-05-27T18:20:32Z")

</div>

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. ^#
