win_lineinfile does not create new file

116 views
Skip to first unread message

cupcake

unread,
Feb 16, 2017, 1:52:24 PM2/16/17
to Ansible Project
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?



J Hawkesworth

unread,
Feb 28, 2017, 9:00:30 AM2/28/17
to Ansible Project
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

cupcake

unread,
Mar 10, 2017, 9:55:39 AM3/10/17
to Ansible Project
The user account is a local admin account with perm to the c:\temp folder. Error is:

fatal: [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] => {
   
"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] => { "changed": true, "invocation": { "module_name": "win_file" } }
 

TASK [win_lineinfile test] ******
changed: [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.
Reply all
Reply to author
Forward
0 new messages