feat: checksum_algo param for find module (#83014)

Co-authored-by: shirann <shirannx@gmail.com>
Co-authored-by: Sviatoslav Sydorenko (Святослав Сидоренко) <webknjaz@redhat.com>
pull/84118/head
ShIRann Chen 1 year ago committed by GitHub
parent 48be6f8b6f
commit 58cb8ca4fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,2 @@
minor_changes:
- find - add a checksum_algorithm parameter to specify which type of checksum the module will return

@ -29,6 +29,10 @@ options:
- You can choose seconds, minutes, hours, days, or weeks by specifying the
first letter of any of those words (e.g., "1w").
type: str
get_checksum:
default: false
checksum_algorithm:
version_added: "2.19"
patterns:
default: []
description:
@ -132,11 +136,6 @@ options:
- Set this to V(true) to follow symlinks in path for systems with python 2.6+.
type: bool
default: no
get_checksum:
description:
- Set this to V(true) to retrieve a file's SHA1 checksum.
type: bool
default: no
use_regex:
description:
- If V(false), the patterns are file globs (shell).
@ -163,7 +162,7 @@ options:
- Default is unlimited matches.
type: int
version_added: "2.18"
extends_documentation_fragment: action_common_attributes
extends_documentation_fragment: [action_common_attributes, checksum_common]
attributes:
check_mode:
details: since this action does not modify the target it just executes normally during check mode
@ -482,6 +481,9 @@ def main():
hidden=dict(type='bool', default=False),
follow=dict(type='bool', default=False),
get_checksum=dict(type='bool', default=False),
checksum_algorithm=dict(type='str', default='sha1',
choices=['md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'],
aliases=['checksum', 'checksum_algo']),
use_regex=dict(type='bool', default=False),
depth=dict(type='int'),
mode=dict(type='raw'),
@ -583,7 +585,7 @@ def main():
r.update(statinfo(st))
if stat.S_ISREG(st.st_mode) and params['get_checksum']:
r['checksum'] = module.sha1(fsname)
r['checksum'] = module.digest_from_file(fsname, params['checksum_algorithm'])
if stat.S_ISREG(st.st_mode):
if sizefilter(st, size):
@ -608,7 +610,7 @@ def main():
r.update(statinfo(st))
if params['get_checksum']:
r['checksum'] = module.sha1(fsname)
r['checksum'] = module.digest_from_file(fsname, params['checksum_algorithm'])
filelist.append(r)
elif stat.S_ISLNK(st.st_mode) and params['file_type'] == 'link':

@ -25,23 +25,6 @@ options:
- Whether to follow symlinks.
type: bool
default: no
get_checksum:
description:
- Whether to return a checksum of the file.
type: bool
default: yes
version_added: "1.8"
checksum_algorithm:
description:
- Algorithm to determine checksum of file.
- Will throw an error if the host is unable to use specified algorithm.
- The remote host has to support the hashing method specified, V(md5)
can be unavailable if the host is FIPS-140 compliant.
type: str
choices: [ md5, sha1, sha224, sha256, sha384, sha512 ]
default: sha1
aliases: [ checksum, checksum_algo ]
version_added: "2.0"
get_mime:
description:
- Use file magic and return data about the nature of the file. This uses
@ -59,8 +42,11 @@ options:
default: yes
aliases: [ attr, attributes ]
version_added: "2.3"
get_checksum:
version_added: "1.8"
extends_documentation_fragment:
- action_common_attributes
- checksum_common
attributes:
check_mode:
support: full

@ -0,0 +1,27 @@
# Copyright (c) 2024 ShIRann Chen <shirannx@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import annotations
class ModuleDocFragment(object):
DOCUMENTATION = r"""
options:
checksum_algorithm:
description:
- Algorithm to determine checksum of file.
- Will throw an error if the host is unable to use specified algorithm.
- The remote host has to support the hashing method specified, V(md5)
can be unavailable if the host is FIPS-140 compliant.
- Availability might be restricted by the target system, for example FIPS systems won't allow md5 use
type: str
choices: [ md5, sha1, sha224, sha256, sha384, sha512 ]
default: sha1
aliases: [ checksum, checksum_algo ]
version_added: "2.0"
get_checksum:
description:
- Whether to return a checksum of the file.
type: bool
default: yes
"""

@ -227,6 +227,29 @@
that:
- success_to_read_right_encoding_file.matched == 1
- name: specify the checksum_algo
register: find_by_specific_algo_res
find:
paths: "{{ remote_tmp_dir_test }}"
get_checksum: true
checksum_algorithm: md5
- debug: var=find_by_specific_algo_res
- name: get the md5 checksum by stat
register: all_files_st
stat:
get_checksum: true
checksum_algorithm: md5
path: "{{ item.path }}"
loop: "{{ find_by_specific_algo_res.files }}"
- assert:
that: "all_files_st.results[index].stat.checksum == item.checksum"
loop: "{{ find_by_specific_algo_res.files }}"
loop_control:
index_var: index
- name: read a gbk file by non-exists encoding
find:
paths: "{{ remote_tmp_dir_test }}"

Loading…
Cancel
Save