Dam
Dam

Reputation: 71

Ansible win_file delete doesnt work

I'm having issue with the win_file module, for a basic utilisation.. I need to delete files registered in a variable:

- name: Find files older than 30d
  win_find:
    paths: "{{ web_backup_dir }}"
    age: 2592000
    age_stamp: ctime
  register: result

#delete files
- win_file:
    path: "{{ web_backup_dir }}\\{{ item }}"
    state: absent
  when: 
- "{{ result.matched != 0 }}"
  with_items:
- "{{ result.files }}"

My playbook run correctly, without errors, but its does not delete any files..

The Console output:

ok: [myserver] => {
"changed": false, 
"examined": 8, 
"files": [
    {
        "attributes": "Normal", 
        "checksum": "35c005a2986458d1bf6a16451bd6f116c82a1ca1", 
        "creationtime": 1511347916.6440868, 
        "extension": ".zip", 
        "filename": "my_files_2017-11-22_10h51.zip", 
        "isarchive": false, 
        "isdir": false, 
        "ishidden": false, 
        "islnk": false, 
        "isreadonly": false, 
        "isshared": false, 
        "lastaccesstime": 1511347916.6440868, 
        "lastwritetime": 1511348372.3509662, 
        "owner": "BUILTIN\\Administrators", 
        "path": "path\\myfile_2017-11-22_10h51.zip", 
        "size": 78729097
    },
.... (other files)
        }
], 
"matched": 6
}

TASK [xxxxx : win_file]
Using module file /usr/local/lib/python2.7/dist-packages/ansible-2.3.2.0-py2.7.egg/ansible/modules/windows/win_file.ps1

ok: [myserver] => (item={u'isdir': False, u'extension': u'.zip', u'isreadonly': False, u'checksum': u'35c005a2986458d1bf6a16451bd6f116c82a1ca1', u'size': 78729097, u'creationtime': 1511347916.6440868, u'filename': u'myfile_2017-11-22_10h51.zip', u'lastaccesstime': 1511347916.6440868, u'owner': u'BUILTIN\\Administrators', u'lastwritetime': 1511348372.3509662, u'islnk': False, u'attributes': u'Normal', u'path': u'myfile_2017-11-22_10h51.zip', u'isarchive': False, u'ishidden': False, u'isshared': False}) => {
"changed": false, 
"item": {
    "attributes": "Normal", 
    "checksum": "35c005a2986458d1bf6a16451bd6f116c82a1ca1", 
    "creationtime": 1511347916.6440868, 
    "extension": ".zip", 
    "filename": "myfile_2017-11-22_10h51.zip", 
    "isarchive": false, 
    "isdir": false, 
    "ishidden": false, 
    "islnk": false, 
    "isreadonly": false, 
    "isshared": false, 
    "lastaccesstime": 1511347916.6440868, 
    "lastwritetime": 1511348372.3509662, 
    "owner": "BUILTIN\\Administrators", 
    "path": "myfile_2017-11-22_10h51.zip", 
    "size": 78729097
}
....(my 5 others files spotted prvly)
}

So the file is well spotted, the module runs perfectly, no "skipped" options, everything is green but, my file is still present in my folder... I tried the win_file module on a specific file (with a complete name and not a variable), and it works fine, the file deletes. What did i skip ?

Thanks

Upvotes: 1

Views: 2562

Answers (1)

Andrew H
Andrew H

Reputation: 453

You should revise your win_file module to use the correct path.

- win_file:
    path: "{{ item.path }}" 
    # OR path: "{{ web_backup_dir }}\\{{ item.filename }}"
    state: absent
  with_items:
    - "{{ result.files }}"

The when condition is extraneous since it will loop per element in result.files.

Upvotes: 1

Related Questions