Skip to content

Conversation

@shahargolshani
Copy link

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
  • New Module/Plugin Pull Request
COMPONENT NAME
  • file_remove
ADDITIONAL INFORMATION
(New module)

@ansibullbot ansibullbot added integration tests/integration module module plugins plugin (any type) tests tests labels Nov 5, 2025
@ansibullbot

This comment was marked as resolved.

@ansibullbot ansibullbot added ci_verified Push fixes to PR branch to re-run CI needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR labels Nov 5, 2025
@ansibullbot ansibullbot removed ci_verified Push fixes to PR branch to re-run CI needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR labels Nov 5, 2025
@felixfontein felixfontein added check-before-release PR will be looked at again shortly before release and merged if possible. backport-12 Automatically create a backport for the stable-12 branch labels Nov 5, 2025
@felixfontein
Copy link
Collaborator

The typing errors are unrelated and have been fixed in another PR.

@shahargolshani shahargolshani force-pushed the dev/file_remove branch 2 times, most recently from 482a0e2 to cdb95e8 Compare November 6, 2025 10:28
Copy link

@kginonredhat kginonredhat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

Copy link
Collaborator

@russoz russoz left a 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.

Copy link
Collaborator

@felixfontein felixfontein left a 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!

@shahargolshani shahargolshani force-pushed the dev/file_remove branch 2 times, most recently from 41e7806 to 31765e4 Compare November 9, 2025 15:41
@ansibullbot ansibullbot added needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR and removed needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR labels Nov 9, 2025
Copy link
Collaborator

@russoz russoz left a 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

Copy link
Collaborator

@russoz russoz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks!

Copy link
Collaborator

@felixfontein felixfontein left a 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
returned: always
returned: success

path:
description: The directory path that was searched.
type: str
returned: always
Copy link
Collaborator

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.

Suggested change
returned: always
returned: success

Comment on lines +230 to +234
path = module.params["path"]
pattern = module.params["pattern"]
use_regex = module.params["use_regex"]
recursive = module.params["recursive"]
file_type = module.params["file_type"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def main():
def main() -> None:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-12 Automatically create a backport for the stable-12 branch check-before-release PR will be looked at again shortly before release and merged if possible. integration tests/integration module module plugins plugin (any type) tests tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants