Ansible win_file删除不起作用

时间:2018-01-29 15:04:05

标签: powershell ansible

我遇到win_file模块的问题,基本使用..我需要删除在变量中注册的文件:

- 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 }}"

我的Playbook运行正常,没有错误,但它不会删除任何文件..

控制台输出:

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)
}

因此该文件被发现,模块运行完美,没有“跳过”选项,一切都是绿色但是,我的文件仍然存在于我的文件夹中... 我在特定文件上尝试了win_file模块(具有完整的名称而不是变量),并且它工作正常,文件删除。 我跳过了什么?

由于

1 个答案:

答案 0 :(得分:1)

您应该修改win_file模块以使用正确的路径。

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

when条件无关紧要,因为它会在result.files中循环每个元素。