# lineinfile creating line when regexp not found

**URL:** <https://forum.ansible.com/t/lineinfile-creating-line-when-regexp-not-found/29779>\
**Category:** Ansible Project\
**Created:** [February 26, 2019, 9:57pm UTC](https://forum.ansible.com/t/lineinfile-creating-line-when-regexp-not-found/29779 "2019-02-26T21:57:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [February 26, 2019, 9:57pm UTC](https://forum.ansible.com/t/lineinfile-creating-line-when-regexp-not-found/29779/1 "2019-02-26T21:57:10Z")

</div>

Consider the following:

`

- name: Change sshd\_config settings  
lineinfile:  
path: /etc/ssh/sshd\_config  
regexp: “{{ item.regexp }}”  
line: “{{ item.line }}”  
with\_items:
- {regexp: “^#._ClientAliveInterval._$”, line: “ClientAliveInterval 1800”}
- {regexp: “^#._ClientAliveCountMax._$”, line: “ClientAliveCountMax 0”}  
notify:
- restart ssh  
`

If I run this against a server multiple times, it will append the lines to the bottom of the file (even if present). According to the docs the line will only be applied if the regexp is found. I must be missing something. Any ideas? If the regexp isn’t found, I do want them appended to the file, but in this case I am getting duplicate entries.

sshd\_config sample:

`  
…  
#PermitUserEnvironment no  
#Compression delayed  
ClientAliveInterval 1800  
ClientAliveCountMax 0  
…

# X11Forwarding no

# AllowTcpForwarding no

# PermitTTY no

# ForceCommand cvs server

ClientAliveInterval 1800  
ClientAliveCountMax 0  
ClientAliveInterval 1800  
ClientAliveCountMax 0  
ClientAliveInterval 1800  
ClientAliveCountMax 0

`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 27, 2019, 3:22pm UTC](https://forum.ansible.com/t/lineinfile-creating-line-when-regexp-not-found/29779/2 "2019-02-27T15:22:34Z")

</div>

Your regexps is checking for a line starting with hash #, but you don't have a line starting with # and contain ClientAliveInterval and ClientAliveCountMax.

And when you add a line that is also without # in the start of the line.

Remove your # in regexp.

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [February 27, 2019, 4:09pm UTC](https://forum.ansible.com/t/lineinfile-creating-line-when-regexp-not-found/29779/3 "2019-02-27T16:09:08Z")

</div>

But the original did have a #. I am showing you the results after running it a few times.

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [February 27, 2019, 4:11pm UTC](https://forum.ansible.com/t/lineinfile-creating-line-when-regexp-not-found/29779/4 "2019-02-27T16:11:06Z")

</div>

I removed the hash as you said (leaving in the wild) and it is working now. Thank you.
