# win\_lineinfile does not create new file

**URL:** <https://forum.ansible.com/t/win-lineinfile-does-not-create-new-file/23747>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [February 16, 2017, 6:52pm UTC](https://forum.ansible.com/t/win-lineinfile-does-not-create-new-file/23747 "2017-02-16T18:52:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cupcake](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@cupcake](https://forum.ansible.com/u/cupcake)\
**Post date:** [February 16, 2017, 6:52pm UTC](https://forum.ansible.com/t/win-lineinfile-does-not-create-new-file/23747/1 "2017-02-16T18:52:24Z")

</div>

ansible core 2.1

When using win\_lineinfile module and the `create: yes` option it seems it should create a new file. it fails the same as if create:yes is not specified.

- hosts: all  
tasks:

- name: win\_lineinfile test  
win\_lineinfile:  
dest: ‘c:\temp\ansible-windows.log’  
create: yes  
line: ‘{{ ansible\_hostname }}-{{ ansible\_date\_time.iso8601 }}’  
regexp: ‘{{ ansible\_hostname }}-’  
state: present

If dest doesn’t exist it fails. Guessing I am doing something wrong?

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [February 28, 2017, 2:00pm UTC](https://forum.ansible.com/t/win-lineinfile-does-not-create-new-file/23747/2 "2017-02-28T14:00:30Z")

</div>

If you run with -vvvvv what error message do you see?

Does your ansible user have permission to write to c:\temp\ansible-windows.log ?

Hope this helps,

Jon

---

<div class="post-metadata">

**Author:** ![cupcake](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@cupcake](https://forum.ansible.com/u/cupcake)\
**Post date:** [March 10, 2017, 2:55pm UTC](https://forum.ansible.com/t/win-lineinfile-does-not-create-new-file/23747/3 "2017-03-10T14:55:38Z")

</div>

The user account is a local admin account with perm to the c:\temp folder. Error is:

`  
fatal: [[winserver1.foo.com](http://winserver1.foo.com)]: FAILED! =\> {  
“changed”: false,  
“failed”: true,  
“invocation”: {  
“module\_name”: “win\_lineinfile”  
},  
“msg”: “Destination c:\temp\ansible-windows\_base.log does not exist !”  
}

`

If the file exists it works:

`  
changed: [[winserver1.foo.com](http://winserver1.foo.com)] =\> {  
“backup”: “”,  
“changed”: true,  
“encoding”: “utf-8”,  
“invocation”: {  
“module\_name”: “win\_lineinfile”  
},  
“msg”: “line added”  
}

`

And if i create a task to create the file ahead of time all is OK

`

**- hosts: all**  
**tasks:**

**- name: create file**  
**win\_file:**  
**path: ‘c:\temp\ansible-windows\_base.log’**  
**state: touch**

**- name: win\_lineinfile test**  
**win\_lineinfile:**  
**dest: ‘c:\temp\ansible-windows\_base.log’**  
**create: true**  
**line: ‘test-{{ ansible\_hostname }}-{{ ansible\_date\_time.iso8601 }}’**  
**regexp: ‘test-{{ ansible\_hostname }}-’**  
**state: present**

`

And re-run:

`  
TASK [create file] \*\*\*\*\*\*  
changed: [[winserver1.foo.com](http://winserver1.foo.com)] =\> { “changed”: true, “invocation”: { “module\_name”: “win\_file” } }

TASK [win\_lineinfile test] \*\*\*\*\*\*  
changed: [[winserver1.foo.com](http://winserver1.foo.com)] =\> { “backup”: “”, “changed”: true, “encoding”: “utf-16”, “invocation”: { “module\_name”: “win\_lineinfile” }, “msg”: “line added” }

`

I’ve also upgrade to 2.2.1.0 and getting the same. Since the linux module will create a new file I’m assuming something is funky with the windows one.
