# lineinfile task fails on missing file, doesn't add line when it should

**URL:** <https://forum.ansible.com/t/lineinfile-task-fails-on-missing-file-doesnt-add-line-when-it-should/17775>\
**Category:** Ansible Project\
**Created:** [March 18, 2015, 4:01pm UTC](https://forum.ansible.com/t/lineinfile-task-fails-on-missing-file-doesnt-add-line-when-it-should/17775 "2015-03-18T16:01:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bret\_Wortman](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Bret\_Wortman](https://forum.ansible.com/u/Bret_Wortman)\
**Post date:** [March 18, 2015, 4:01pm UTC](https://forum.ansible.com/t/lineinfile-task-fails-on-missing-file-doesnt-add-line-when-it-should/17775/1 "2015-03-18T16:01:10Z")

</div>

I’m pretty sure I’m doing something wrong here and am hoping some of you with more seasoning can help me figure it out.

I have a task:

- name: Ensure /etc/hosts.deny has default ALL entry  
lineinfile: \>  
dest=“/etc/hosts.deny”  
line=“ALL {{‘:’}} ALL”  
insertbefore=EOF  
create=yes  
state=present  
tags:
- deny

When I run this on a system which doesn’t have an /etc/hosts.deny, I get this:

TASK: [compliance | Ensure /etc/hosts.deny has default ALL entry] \*\*\*\*\*\*\*\*\*\*\*\*\*\*  
failed: [us202] =\> {“failed”: true, “parsed”: false}  
Traceback most recent call last):  
File “”, line 2200, in

File “”, line 395, in main

File “”, line 288, in present

File “”, line 178, in check\_file\_attrs

File “”, line 1199, in set\_fs\_attributes\_if\_different

File “”, line 1037, in set\_mode\_if\_different

OSError: [Errorno 2] No such file or directory: ‘/etc/hosts.deny’

I would have expected ansible to create the file instead of erroring out.

If the file does exist but is empty or contains comments (and not the “ALL : ALL” line), ansible just reports an “ok” without adding the line.

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![Phonthip\_Namkaew](https://avatars.discourse-cdn.com/v4/letter/p/eada6e/32.png) [@Phonthip\_Namkaew](https://forum.ansible.com/u/Phonthip_Namkaew)\
**Post date:** [March 18, 2015, 8:45pm UTC](https://forum.ansible.com/t/lineinfile-task-fails-on-missing-file-doesnt-add-line-when-it-should/17775/2 "2015-03-18T20:45:16Z")

</div>

Are you sure that EOF in \<insertbefore=EOF\> is valid ?

---

<div class="post-metadata">

**Author:** ![Phonthip\_Namkaew](https://avatars.discourse-cdn.com/v4/letter/p/eada6e/32.png) [@Phonthip\_Namkaew](https://forum.ansible.com/u/Phonthip_Namkaew)\
**Post date:** [March 18, 2015, 9:14pm UTC](https://forum.ansible.com/t/lineinfile-task-fails-on-missing-file-doesnt-add-line-when-it-should/17775/3 "2015-03-18T21:14:02Z")

</div>

> Try this:

(…)

tasks:

- name: add a line to /etc/hosts.deny  
lineinfile:  
“dest=‘/etc/hosts.deny’  
line=‘ALL : ALL’  
insertafter=EOF  
create=yes  
state=present”  
(…)

---

<div class="post-metadata">

**Author:** ![Michael\_Bushey](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@Michael\_Bushey](https://forum.ansible.com/u/Michael_Bushey)\
**Post date:** [March 18, 2015, 9:14pm UTC](https://forum.ansible.com/t/lineinfile-task-fails-on-missing-file-doesnt-add-line-when-it-should/17775/4 "2015-03-18T21:14:27Z")

</div>

> I would have expected ansible to create the file instead of erroring out.

Do this first to make sure it exists:

- name: Create /etc/hosts.deny  
shell: touch /etc/hosts.deny creates=/etc/hosts.deny

---

<div class="post-metadata">

**Author:** ![Bret\_Wortman](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Bret\_Wortman](https://forum.ansible.com/u/Bret_Wortman)\
**Post date:** [March 19, 2015, 10:15am UTC](https://forum.ansible.com/t/lineinfile-task-fails-on-missing-file-doesnt-add-line-when-it-should/17775/5 "2015-03-19T10:15:39Z")

</div>

If the file already exists, this task also reports “ok”, though it doesn’t add the line, even to an empty file.

---

<div class="post-metadata">

**Author:** ![Bret\_Wortman](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Bret\_Wortman](https://forum.ansible.com/u/Bret_Wortman)\
**Post date:** [March 19, 2015, 10:17am UTC](https://forum.ansible.com/t/lineinfile-task-fails-on-missing-file-doesnt-add-line-when-it-should/17775/6 "2015-03-19T10:17:20Z")

</div>

That yields a parsing error:

TASK: [compliance | Ensure /etc/hosts.deny has default ALL entry] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
failed: [us202] =\> {“failed”: true}  
msg: line= is required with state=present

I switched it back to a single-line version but quoted as you showed, but the result is the same. A nonexistent file isn’t created, and an existing file isn’t modified.

---

<div class="post-metadata">

**Author:** ![Bret\_Wortman](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Bret\_Wortman](https://forum.ansible.com/u/Bret_Wortman)\
**Post date:** [March 19, 2015, 10:17am UTC](https://forum.ansible.com/t/lineinfile-task-fails-on-missing-file-doesnt-add-line-when-it-should/17775/7 "2015-03-19T10:17:52Z")

</div>

When I do this and the file already exists, then it fails to add the line.

---

<div class="post-metadata">

**Author:** ![Phonthip\_Namkaew](https://avatars.discourse-cdn.com/v4/letter/p/eada6e/32.png) [@Phonthip\_Namkaew](https://forum.ansible.com/u/Phonthip_Namkaew)\
**Post date:** [March 19, 2015, 11:49am UTC](https://forum.ansible.com/t/lineinfile-task-fails-on-missing-file-doesnt-add-line-when-it-should/17775/8 "2015-03-19T11:49:07Z")

</div>

Again:

I strongly believe that EOF is not valid option here: \<insertbefore=EOF\>

Did you try his :

insertafter=EOF

---

<div class="post-metadata">

**Author:** ![Bret\_Wortman](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Bret\_Wortman](https://forum.ansible.com/u/Bret_Wortman)\
**Post date:** [March 19, 2015, 11:55am UTC](https://forum.ansible.com/t/lineinfile-task-fails-on-missing-file-doesnt-add-line-when-it-should/17775/9 "2015-03-19T11:55:31Z")

</div>

Ugh. I just re-checked the online docs and you’re absolutely right. And that change fixed the task.

Thank you for pointing this out to me again, after I missed it on my first read. Problem solved!
