mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-23 13:20:23 -07:00
win_find: allow module to skip on files it fails to check (#32105)
* win_find: allow module to skip on files it fails to check * fixed up test creation to work pre psv5
This commit is contained in:
parent
3e080ad53d
commit
56a7278256
8 changed files with 846 additions and 863 deletions
|
@ -1,21 +1,10 @@
|
|||
#!powershell
|
||||
# This file is part of Ansible
|
||||
#
|
||||
# Ansible is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Ansible is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
# WANT_JSON
|
||||
# POWERSHELL_COMMON
|
||||
# Copyright (c) 2016 Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
#Requires -Module Ansible.ModuleUtils.Legacy
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
|
@ -23,21 +12,20 @@ $params = Parse-Args -arguments $args -supports_check_mode $true
|
|||
|
||||
$paths = Get-AnsibleParam -obj $params -name 'paths' -failifempty $true
|
||||
|
||||
$age = Get-AnsibleParam -obj $params -name 'age' -failifempty $false -default $null
|
||||
$age_stamp = Get-AnsibleParam -obj $params -name 'age_stamp' -failifempty $false -default 'mtime' -ValidateSet 'mtime','ctime','atime'
|
||||
$file_type = Get-AnsibleParam -obj $params -name 'file_type' -failifempty $false -default 'file' -ValidateSet 'file','directory'
|
||||
$follow = Get-AnsibleParam -obj $params -name 'follow' -type "bool" -failifempty $false -default $false
|
||||
$hidden = Get-AnsibleParam -obj $params -name 'hidden' -type "bool" -failifempty $false -default $false
|
||||
$patterns = Get-AnsibleParam -obj $params -name 'patterns' -failifempty $false -default $null
|
||||
$recurse = Get-AnsibleParam -obj $params -name 'recurse' -type "bool" -failifempty $false -default $false
|
||||
$size = Get-AnsibleParam -obj $params -name 'size' -failifempty $false -default $null
|
||||
$use_regex = Get-AnsibleParam -obj $params -name 'use_regex' -type "bool" -failifempty $false -default $false
|
||||
$get_checksum = Get-AnsibleParam -obj $params -name 'get_checksum' -type "bool" -failifempty $false -default $true
|
||||
$checksum_algorithm = Get-AnsibleParam -obj $params -name 'checksum_algorithm' -failifempty $false -default 'sha1' -ValidateSet 'md5', 'sha1', 'sha256', 'sha384', 'sha512'
|
||||
$age = Get-AnsibleParam -obj $params -name 'age'
|
||||
$age_stamp = Get-AnsibleParam -obj $params -name 'age_stamp' -default 'mtime' -ValidateSet 'mtime','ctime','atime'
|
||||
$file_type = Get-AnsibleParam -obj $params -name 'file_type' -default 'file' -ValidateSet 'file','directory'
|
||||
$follow = Get-AnsibleParam -obj $params -name 'follow' -type "bool" -default $false
|
||||
$hidden = Get-AnsibleParam -obj $params -name 'hidden' -type "bool" -default $false
|
||||
$patterns = Get-AnsibleParam -obj $params -name 'patterns'
|
||||
$recurse = Get-AnsibleParam -obj $params -name 'recurse' -type "bool" -default $false
|
||||
$size = Get-AnsibleParam -obj $params -name 'size'
|
||||
$use_regex = Get-AnsibleParam -obj $params -name 'use_regex' -type "bool" -default $false
|
||||
$get_checksum = Get-AnsibleParam -obj $params -name 'get_checksum' -type "bool" -default $true
|
||||
$checksum_algorithm = Get-AnsibleParam -obj $params -name 'checksum_algorithm' -default 'sha1' -ValidateSet 'md5', 'sha1', 'sha256', 'sha384', 'sha512'
|
||||
|
||||
$result = @{
|
||||
files = @()
|
||||
warnings = @()
|
||||
examined = 0
|
||||
matched = 0
|
||||
changed = $false
|
||||
|
@ -121,7 +109,7 @@ Function Assert-Age($info) {
|
|||
}
|
||||
}
|
||||
} else {
|
||||
Fail-Json $result "failed to process age"
|
||||
throw "failed to process age for file $($info.FullName)"
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -204,7 +192,7 @@ Function Assert-Size($info) {
|
|||
}
|
||||
}
|
||||
} else {
|
||||
Fail-Json $result "failed to process size"
|
||||
throw "failed to process size for file $($info.FullName)"
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -282,8 +270,12 @@ Function Get-FileStat($file) {
|
|||
$file_stat.extension = $file.Extension
|
||||
|
||||
if ($get_checksum) {
|
||||
$checksum = Get-FileChecksum -path $path -algorithm $checksum_algorithm
|
||||
$file_stat.checksum = $checksum
|
||||
try {
|
||||
$checksum = Get-FileChecksum -path $path -algorithm $checksum_algorithm
|
||||
$file_stat.checksum = $checksum
|
||||
} catch {
|
||||
throw "failed to get checksum for file $($file.FullName)"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -329,8 +321,14 @@ foreach ($path in $paths) {
|
|||
$paths_to_check = $paths_to_check | Select-Object -Unique
|
||||
|
||||
foreach ($path in $paths_to_check) {
|
||||
$file = Get-Item -Force -Path $path
|
||||
$info = Get-FileStat -file $file
|
||||
try {
|
||||
$file = Get-Item -Force -Path $path
|
||||
$info = Get-FileStat -file $file
|
||||
} catch {
|
||||
Add-Warning -obj $result -message "win_find failed to check some files, these files were ignored and will not be part of the result output"
|
||||
break
|
||||
}
|
||||
|
||||
$new_examined = $result.examined + 1
|
||||
$result.examined = $new_examined
|
||||
|
||||
|
|
|
@ -1,23 +1,11 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# (c) 2016, Ansible, inc
|
||||
#
|
||||
# This file is part of Ansible
|
||||
#
|
||||
# Ansible is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Ansible is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
# Copyright (c) 2016 Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
# this is a windows documentation stub. actual code lives in the .ps1
|
||||
# file of the same name
|
||||
|
||||
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
||||
'status': ['preview'],
|
||||
|
@ -27,7 +15,7 @@ DOCUMENTATION = r'''
|
|||
---
|
||||
module: win_find
|
||||
version_added: "2.3"
|
||||
short_description: return a list of files based on specific criteria
|
||||
short_description: Return a list of files based on specific criteria
|
||||
description:
|
||||
- Return a list of files based on specified criteria.
|
||||
- Multiple criteria are AND'd together.
|
||||
|
@ -40,45 +28,38 @@ options:
|
|||
less than the specified time. You can choose seconds, minutes,
|
||||
hours, days or weeks by specifying the first letter of an of
|
||||
those words (e.g., "2s", "10d", 1w").
|
||||
required: false
|
||||
age_stamp:
|
||||
description:
|
||||
- Choose the file property against which we compare C(age). The
|
||||
default attribute we compare with is the last modification time.
|
||||
required: false
|
||||
default: mtime
|
||||
choices: ['atime', 'mtime', 'ctime']
|
||||
checksum_algorithm:
|
||||
description:
|
||||
- Algorithm to determine the checksum of a file. Will throw an error
|
||||
if the host is unable to use specified algorithm.
|
||||
required: false
|
||||
default: sha1
|
||||
choices: ['md5', 'sha1', 'sha256', 'sha384', 'sha512']
|
||||
file_type:
|
||||
description: Type of file to search for
|
||||
required: false
|
||||
description: Type of file to search for.
|
||||
default: file
|
||||
choices: ['file', 'directory']
|
||||
follow:
|
||||
description:
|
||||
- Set this to true to follow symlinks in the path. This needs to
|
||||
be used in conjunction with C(recurse).
|
||||
required: false
|
||||
default: false
|
||||
choices: ['true', 'false']
|
||||
type: bool
|
||||
default: 'no'
|
||||
get_checksum:
|
||||
description:
|
||||
- Whether to return a checksum of the file in the return info (default sha1),
|
||||
use C(checksum_algorithm) to change from the default.
|
||||
required: false
|
||||
default: true
|
||||
choices: ['true', 'false']
|
||||
type: bool
|
||||
default: 'yes'
|
||||
hidden:
|
||||
description: Set this to include hidden files or folders
|
||||
required: false
|
||||
default: false
|
||||
choices: ['true', 'false']
|
||||
description: Set this to include hidden files or folders.
|
||||
type: bool
|
||||
default: 'no'
|
||||
paths:
|
||||
description:
|
||||
- List of paths of directories to search for files or folders in.
|
||||
|
@ -91,14 +72,12 @@ options:
|
|||
option. The patterns retrict the list of files or folders to be
|
||||
returned based on the filenames. For a file to be matched it
|
||||
only has to match with one pattern in a list provided.
|
||||
required: false
|
||||
recurse:
|
||||
description:
|
||||
- Will recursively descend into the directory looking for files
|
||||
or folders
|
||||
required: false
|
||||
default: false
|
||||
choices: ['true', 'false']
|
||||
or folders.
|
||||
type: bool
|
||||
default: 'no'
|
||||
size:
|
||||
description:
|
||||
- Select files or folders whose size is equal to or greater than
|
||||
|
@ -106,123 +85,114 @@ options:
|
|||
or less than the specified size. You can specify the size with
|
||||
a suffix of the byte type i.e. kilo = k, mega = m... Size is not
|
||||
evaluated for symbolic links.
|
||||
required: false
|
||||
default: false
|
||||
use_regex:
|
||||
description:
|
||||
- Will set patterns to run as a regex check if true
|
||||
required: false
|
||||
default: false
|
||||
choices: ['true', 'false']
|
||||
notes:
|
||||
- For non-Windows targets, use the M(find) module instead.
|
||||
author: "Jordan Borean (@jborean93)"
|
||||
- Will set patterns to run as a regex check if true.
|
||||
type: bool
|
||||
default: 'no'
|
||||
author:
|
||||
- Jordan Borean (@jborean93)
|
||||
'''
|
||||
|
||||
EXAMPLES = r'''
|
||||
# Find files in path
|
||||
- win_find:
|
||||
- name: Find files in path
|
||||
win_find:
|
||||
paths: D:\temp
|
||||
|
||||
# Find hidden files in path
|
||||
- win_find:
|
||||
- name: Find hidden files in path
|
||||
win_find:
|
||||
paths: D:\temp
|
||||
hidden: True
|
||||
|
||||
# Find files in multiple paths
|
||||
- win_find:
|
||||
- name: Find files in multiple paths
|
||||
win_find:
|
||||
paths: ['C:\temp', 'D:\temp']
|
||||
|
||||
# Find files in directory while searching recursively
|
||||
- win_find:
|
||||
- name: Find files in directory while searching recursively
|
||||
win_find:
|
||||
paths: D:\temp
|
||||
recurse: True
|
||||
|
||||
# Find files in directory while following symlinks
|
||||
- win_find:
|
||||
- name: Find files in directory while following symlinks
|
||||
win_find:
|
||||
paths: D:\temp
|
||||
recurse: True
|
||||
follow: True
|
||||
|
||||
# Find files with .log and .out extension using powershell wildcards
|
||||
- win_find:
|
||||
- name: Find files with .log and .out extension using powershell wildcards
|
||||
win_find:
|
||||
paths: D:\temp
|
||||
patterns: ['*.log', '*.out']
|
||||
|
||||
# Find files in path based on regex pattern
|
||||
- win_find:
|
||||
- name: Find files in path based on regex pattern
|
||||
win_find:
|
||||
paths: D:\temp
|
||||
patterns: 'out_\d{8}-\d{6}.log'
|
||||
|
||||
# Find files older than 1 day
|
||||
- win_find:
|
||||
- name: Find files older than 1 day
|
||||
win_find:
|
||||
paths: D:\temp
|
||||
age: 86400
|
||||
|
||||
# Find files older than 1 day based on create time
|
||||
- win_find:
|
||||
- name: Find files older than 1 day based on create time
|
||||
win_find:
|
||||
paths: D:\temp
|
||||
age: 86400
|
||||
age_stamp: ctime
|
||||
|
||||
# Find files older than 1 day with unit syntax
|
||||
- win_find:
|
||||
- name: Find files older than 1 day with unit syntax
|
||||
win_find:
|
||||
paths: D:\temp
|
||||
age: 1d
|
||||
|
||||
# Find files newer than 1 hour
|
||||
- win_find:
|
||||
- name: Find files newer than 1 hour
|
||||
win_find:
|
||||
paths: D:\temp
|
||||
age: -3600
|
||||
|
||||
# Find files newer than 1 hour with unit syntax
|
||||
- win_find:
|
||||
- name: Find files newer than 1 hour with unit syntax
|
||||
win_find:
|
||||
paths: D:\temp
|
||||
age: -1h
|
||||
|
||||
# Find files larger than 1MB
|
||||
- win_find:
|
||||
- name: Find files larger than 1MB
|
||||
win_find:
|
||||
paths: D:\temp
|
||||
size: 1048576
|
||||
|
||||
# Find files larger than 1GB with unit syntax
|
||||
- win_find:
|
||||
- name: Find files larger than 1GB with unit syntax
|
||||
win_find:
|
||||
paths: D:\temp
|
||||
size: 1g
|
||||
|
||||
# Find files smaller than 1MB
|
||||
- win_find:
|
||||
- name: Find files smaller than 1MB
|
||||
win_find:
|
||||
paths: D:\temp
|
||||
size: -1048576
|
||||
|
||||
# Find files smaller than 1GB with unit syntax
|
||||
- win_find:
|
||||
- name: Find files smaller than 1GB with unit syntax
|
||||
win_find:
|
||||
paths: D:\temp
|
||||
size: -1g
|
||||
|
||||
# Find folders/symlinks in multiple paths
|
||||
- win_find:
|
||||
- name: Find folders/symlinks in multiple paths
|
||||
win_find:
|
||||
paths: ['C:\temp', 'D:\temp']
|
||||
file_type: directory
|
||||
|
||||
# Find files and return SHA256 checksum of files found
|
||||
- win_find:
|
||||
- name: Find files and return SHA256 checksum of files found
|
||||
win_find:
|
||||
paths: C:\temp
|
||||
get_checksum: True
|
||||
checksum_algorithm: sha256
|
||||
|
||||
# Find files and do not return the checksum
|
||||
- win_find:
|
||||
- name: Find files and do not return the checksum
|
||||
win_find:
|
||||
path: C:\temp
|
||||
get_checksum: False
|
||||
'''
|
||||
|
||||
RETURN = r'''
|
||||
changed:
|
||||
description: Whether anything was chagned
|
||||
returned: always
|
||||
type: boolean
|
||||
sample: True
|
||||
examined:
|
||||
description: The number of files/folders that was checked
|
||||
returned: always
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue