# Creating missing file for blockinfile edit

**URL:** <https://forum.ansible.com/t/creating-missing-file-for-blockinfile-edit/22825>\
**Category:** Ansible Project\
**Created:** [October 19, 2016, 10:33am UTC](https://forum.ansible.com/t/creating-missing-file-for-blockinfile-edit/22825 "2016-10-19T10:33:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Delin](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@Daniel\_Delin](https://forum.ansible.com/u/Daniel_Delin)\
**Post date:** [October 19, 2016, 10:33am UTC](https://forum.ansible.com/t/creating-missing-file-for-blockinfile-edit/22825/1 "2016-10-19T10:33:19Z")

</div>

Hi,

New to Ansible, evaluating it for replacing Cfengine.  
One feature we use frequently is the replacement of a block of text within a file, and the Ansible blockinfile module looks to be just that.

However I run in to problems adding a block of text to a file if it does not already exist. I have tried with this code:

- name: Add mappings to /tmp/hosts  
blockinfile:  
dest: /tmp/hosts  
state: present  
create: yes  
block: |  
destination logserver1 { tcp(“xx.xx.xx.xx” port(514)); };  
destination logserver2 { tcp(“xx.xx.x.x” port(514)); };  
log { source(src); destination(logserver1); };  
log { source(src); destination(logserver2); };  
log { source(src); destination(logserver3); };  
destination logserver3 { tcp(“x.x.xx.xxx” port(5000)); };  
marker: "# {mark} ANSIBLE HOSTS BLOCK "

When I run this it bails out because of the create: yes line, if I comment that out it runs through, but of course does not create the file, and therefore fails the blockedit. If I create an empty file manually it works as intended. According to the docs, both state and create are supported by the blockinfile module.

Ansible version is 2.1.2.0

Help appreciated

```auto

```

---

<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:** [October 19, 2016, 5:17pm UTC](https://forum.ansible.com/t/creating-missing-file-for-blockinfile-edit/22825/2 "2016-10-19T17:17:14Z")

</div>

I tested you code on my 2.1.2 and it works.

$ ls /tmp/hosts  
ls: cannot access '/tmp/hosts': No such file or directory

$ ansible-playbook test.yml

PLAY [localhost] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [Add mappings to /tmp/hosts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [localhost]

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=1 changed=1 unreachable=0 failed=0

$ ls /tmp/hosts  
/tmp/hosts

---

<div class="post-metadata">

**Author:** ![Daniel\_Delin](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@Daniel\_Delin](https://forum.ansible.com/u/Daniel_Delin)\
**Post date:** [October 20, 2016, 8:46am UTC](https://forum.ansible.com/t/creating-missing-file-for-blockinfile-edit/22825/3 "2016-10-20T08:46:59Z")

</div>

Thanks, think I found my rookie mistake, I have used the --check flag liberally when testing the playbooks, but that of course causes problems in this case since it does not create the file because of that, and then the rest of the playbook fails.
