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.

try:
re.compile(pattern)
except re.error as e:
module.fail_json(msg=f"Invalid regular expression pattern: {to_native(e)}")
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is redundant, since we no longer support Python 2, more occurrences around the code should be adjusted.

Suggested change
module.fail_json(msg=f"Invalid regular expression pattern: {to_native(e)}")
module.fail_json(msg=f"Invalid regular expression pattern: {e}")

# SPDX-License-Identifier: GPL-3.0-or-later

azp/posix/3
destructive
Copy link
Collaborator

Choose a reason for hiding this comment

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

Arguably not, since everything is created and destroyed within a temporary directory. @felixfontein what do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, this isn't needed. The tests aren't installing/removing/modifying packages, system config files, services etc. or doing similar destructive things, which this aliases entry is for.

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!

Comment on lines +17 to +20
- This module removes files from a specified directory that match a given pattern.
- The pattern can include wildcards and regular expressions.
- By default, only files in the specified directory are removed (non-recursive).
- Use the O(recursive) option to search and remove files in subdirectories.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think some fewer paragraphs would be better here:

Suggested change
- This module removes files from a specified directory that match a given pattern.
- The pattern can include wildcards and regular expressions.
- By default, only files in the specified directory are removed (non-recursive).
- Use the O(recursive) option to search and remove files in subdirectories.
- This module removes files from a specified directory that match a given pattern.
The pattern can include wildcards and regular expressions.
- By default, only files in the specified directory are removed (non-recursive).
Use the O(recursive) option to search and remove files in subdirectories.

Comment on lines +28 to +33
check_mode:
description: Can run in check_mode and return changed status without modifying the target.
support: full
diff_mode:
description: Will return details on what has changed (or possibly needs changing in check_mode).
support: full
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please remove the descriptions and use the appropriate doc fragment (see basically every other module in this collection).

pattern:
description:
- Pattern to match files for removal.
- Supports wildcards (*, ?, [seq], [!seq]) for glob-style matching.
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
- Supports wildcards (*, ?, [seq], [!seq]) for glob-style matching.
- Supports wildcards (V(*), V(?), V([seq]), V([!seq])) for glob-style matching.

Comment on lines +68 to +72
- V(file) - remove only regular files.
- V(link) - remove only symbolic links.
- V(any) - remove both files and symbolic links.
type: str
choices: ['file', 'link', 'any']
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
- V(file) - remove only regular files.
- V(link) - remove only symbolic links.
- V(any) - remove both files and symbolic links.
type: str
choices: ['file', 'link', 'any']
type: str
choices:
file: remove only regular files.
link: remove only symbolic links.
any: remove both files and symbolic links.

Comment on lines +125 to +130

msg:
description: Status message.
type: str
returned: always
sample: "Removed 2 files matching pattern '*.log'"
Copy link
Collaborator

Choose a reason for hiding this comment

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

We generally don't document standard return values.

Suggested change
msg:
description: Status message.
type: str
returned: always
sample: "Removed 2 files matching pattern '*.log'"

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