-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add New Module file_remove #11032
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add New Module file_remove #11032
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
5b8e67c to
fc19d13
Compare
fc19d13 to
c2250a8
Compare
|
The typing errors are unrelated and have been fixed in another PR. |
482a0e2 to
cdb95e8
Compare
kginonredhat
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good
russoz
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hi @shahargolshani Thanks for your contribution!
Overall it looks good, just a few adjustments. I have not checked the logic of the tests, but the use case for this is not very complex, I am trusting they are covering what's needed.
felixfontein
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution!
41e7806 to
31765e4
Compare
russoz
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @shahargolshani thanks for the updates. Still a couple of dangling bits, but much closer to complete
russoz
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Thanks!
felixfontein
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about adding type hints? Now that we have type checking and no longer support Python before 3.7, this would improve the module code's quality :)
I've added some suggestions below, I think this should do it (I haven't tested it though).
| files_count: | ||
| description: Number of files removed. | ||
| type: int | ||
| returned: always |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| returned: always | |
| returned: success |
| path: | ||
| description: The directory path that was searched. | ||
| type: str | ||
| returned: always |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This return value is only returned when exit_json() is called.
| returned: always | |
| returned: success |
| path = module.params["path"] | ||
| pattern = module.params["pattern"] | ||
| use_regex = module.params["use_regex"] | ||
| recursive = module.params["recursive"] | ||
| file_type = module.params["file_type"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| path = module.params["path"] | |
| pattern = module.params["pattern"] | |
| use_regex = module.params["use_regex"] | |
| recursive = module.params["recursive"] | |
| file_type = module.params["file_type"] | |
| path: str = module.params["path"] | |
| pattern: str = module.params["pattern"] | |
| use_regex: bool = module.params["use_regex"] | |
| recursive: bool = module.params["recursive"] | |
| file_type: t.Literal["file", "link", "any"] = module.params["file_type"] |
|
|
||
| import os | ||
| import re | ||
| import glob |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| import glob | |
| import glob | |
| import typing as t | |
| from ansible.module_utils.basic import AnsibleModule | ||
|
|
||
|
|
||
| def find_matching_files(path, pattern, use_regex, recursive, file_type): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| def find_matching_files(path, pattern, use_regex, recursive, file_type): | |
| def find_matching_files(path: str, pattern: str, use_regex: bool, recursive: bool, file_type: t.Literal["file", "link", "any"]) -> list[str]: |
| return sorted(matching_files) | ||
|
|
||
|
|
||
| def should_include_file(file_path, file_type): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| def should_include_file(file_path, file_type): | |
| def should_include_file(file_path: str, file_type: t.Literal["file", "link", "any"]) -> bool: |
| return False | ||
|
|
||
|
|
||
| def remove_files(module, files): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| def remove_files(module, files): | |
| def remove_files(module: AnsibleModule, files: list[str]) -> tuple[list[str], list[str]]: |
| return removed_files, failed_files | ||
|
|
||
|
|
||
| def main(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| def main(): | |
| def main() -> None: |
SUMMARY
This PR introduces a new module,
file_remove, designed to idempotently delete a single file from a target host.Rationale: This module serves as an enhancement for many file deletion cases not currently supported by ansible.builtin.file with state=absent.
Pattern-based deletion: It allows using regular expressions to remove multiple files that match a specific pattern (e.g., all *.log or *.tmp files) in a single task.
Design: This avoids the common workaround of using ansible.builtin.find, registering the results, and then looping over them with ansible.builtin.file. This module performs that entire operation idempotently in one step.
This new module provides a more powerful and intuitive interface for these common "clean-up" style operations.
changelog: New Module
ISSUE TYPE
COMPONENT NAME
ADDITIONAL INFORMATION