doina

一个小菜鸟运维工程师.

Ansible win_find模块

根据指定的条件返回文件列表

参数 选项 作用
age 等于或大于指定时间的文件或文件夹,比如10s,10m,10d,10w
age_stamp atime 按访问时间查找
ctime 按创建时间查找
mtime 按修改时间查找,默认选项
file_type directory 查找的类型为目录
file 查找的类型为文件
follow no 默认选项
yes 查找链接文件,需要结合recurse
get_checksum no 不返回文件的校验和
yes 默认选项,返回文件的校验和
hidden no 默认选项,不包含隐藏文件或文件夹
yes 包含隐藏文件或文件夹
paths 搜索文件或文件夹的目录路径列表,可以为单个路径或路径列表
patterns 一个或多个(powershell或regex)模式来比较文件名。模式匹配的类型由use_regex选项控制。
recurse no 默认选项,不采用递归查找
yes 采用递归查找
size 选择大小等于或大于指定大小的文件或文件夹
use_regex no 默认选项,使用普通匹配
yes 如果设置为yes,则将模式设置为正则表达式检查yes

模块使用

#ansible方式
#根据时间查找,默认是mtime,查找一天前的文件
[root@squid win_find]# ansible windows -m win_find -a "paths="C:\\win_find_test" age=86400"
10.241.0.4 | SUCCESS => {
    "changed": false, 
    "examined": 5, 
    "files": [
        {
            "attributes": "Archive", 
            "checksum": "e4ad167da8c232d9cb8e035a01eb2cf6674c6a28", 
            "creationtime": 1535109700.7934108, 
            "extension": ".exe", 
            "filename": "jdk-7u2-windows-x64.exe", 
            "isarchive": true, 
            "isdir": false, 
            "ishidden": false, 
            "islnk": false, 
            "isreadonly": false, 
            "isshared": false, 
            "lastaccesstime": 1535109700.7934108, 
            "lastwritetime": 1521040214.9753127, 
            "owner": "BUILTIN\\Administrators", 
            "path": "C:\\win_find_test\\jdk-7u2-windows-x64.exe",
            "size": 91590032
        }
    ],
    "matched": 1
}

#使用文件扩展名查找
[root@squid win_find]# ansible windows -m win_find -a "paths="C:\\win_find_test" patterns="*.zip"" 
10.241.0.4 | SUCCESS => {
    "changed": false, 
    "examined": 5, 
    "files": [
        {
            "attributes": "Archive", 
            "checksum": "80ca47dac0306bf683bf568476ef95b19c44be86", 
            "creationtime": 1535054855.5902135, 
            "extension": ".zip", 
            "filename": "at.zip", 
            "isarchive": true, 
            "isdir": false, 
            "ishidden": false, 
            "islnk": false, 
            "isreadonly": false, 
            "isshared": false, 
            "lastaccesstime": 1535054855.5902135, 
            "lastwritetime": 1535054855.6215131, 
            "owner": "BUILTIN\\Administrators", 
            "path": "C:\\win_find_test\\at.zip", 
            "size": 601
        }
    ], 
    "matched": 1
}

#playbook方式
[root@squid win_find]# cat win_find.yaml 
---
- hosts: windows
  tasks: 
  - name: find zip and rar
    win_find:
      paths: c:\\win_find_test
      patterns: [ '*.zip', '*.rar' ]
    register: win_find_out
  - debug: msg="{{win_find_out}}"

[root@squid win_find]# ansible-playbook win_find.yaml 

PLAY [windows] ***********************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************
ok: [10.241.0.4]

TASK [find zip and rar] **************************************************************************************************************************
ok: [10.241.0.4]

TASK [debug] *************************************************************************************************************************************
ok: [10.241.0.4] => {
    "msg": {
        "changed": false, 
        "examined": 5, 
        "failed": false, 
        "files": [
            {
                "attributes": "Archive", 
                "checksum": "63951dd0263fc03643d06cdd1225a0f4b3c30143", 
                "creationtime": 1535055333.6527555, 
                "extension": ".rar", 
                "filename": "at.rar", 
                "isarchive": true, 
                "isdir": false, 
                "ishidden": false, 
                "islnk": false, 
                "isreadonly": false, 
                "isshared": false, 
                "lastaccesstime": 1535055333.6527555, 
                "lastwritetime": 1535055333.6840353, 
                "owner": "BUILTIN\\Administrators", 
                "path": "C:\\win_find_test\\at.rar", 
                "size": 366
            }, 
            {
                "attributes": "Archive", 
                "checksum": "80ca47dac0306bf683bf568476ef95b19c44be86", 
                "creationtime": 1535054855.5902135, 
                "extension": ".zip", 
                "filename": "at.zip", 
                "isarchive": true, 
                "isdir": false, 
                "ishidden": false, 
                "islnk": false, 
                "isreadonly": false, 
                "isshared": false, 
                "lastaccesstime": 1535054855.5902135, 
                "lastwritetime": 1535054855.6215131, 
                "owner": "BUILTIN\\Administrators", 
                "path": "C:\\win_find_test\\at.zip", 
                "size": 601
            }
        ], 
        "matched": 2
    }
}

PLAY RECAP ***************************************************************************************************************************************
10.241.0.4                 : ok=3    changed=0    unreachable=0    failed=0
点赞

发表评论

电子邮件地址不会被公开。

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据