Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Add a "file" parameter to "path" module
- Add a "find" module

## [1.1.0] - 2024-09-13

Expand Down
48 changes: 48 additions & 0 deletions plugins/action/find.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from __future__ import annotations

from ansible.plugins.action import ActionBase

from pathlib import Path


class ActionModule(ActionBase):
'''Return a list of paths based on specific criteria'''

def run(self, tmp=None, task_vars=None):

result = super(ActionModule, self).run(tmp, task_vars)

validation, args = self.validate_argument_spec(
argument_spec={
'path': {'type': 'path', 'required': True},
'patterns': {'type': 'list', 'default': [], 'elements': 'str'},
'excludes': {'type': 'list', 'elements': 'str'},
},
)

find = self._execute_module(
module_name='ansible.builtin.find',
module_args={
'paths': [args['path']],
'file_type': 'file',
'patterns': args['patterns'],
'excludes': args['excludes'],
},
task_vars=task_vars,
)

files = find.pop('files')

result.update(find)
result['paths'] = []

for file in files:
result['paths'].append({
'path': str(Path(file['path']).relative_to(Path(args['path']))),
'state': 'file',
'user': file['pw_name'],
'group': file['gr_name'],
'mode': file['mode'],
})

return result
18 changes: 18 additions & 0 deletions plugins/modules/find.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import annotations

DOCUMENTATION = '''
---
module: find
short_description: Return a list of paths based on specific criteria
description:
- Return a list of paths based on specific criteria
author:
- Manala (@manala)
'''

EXAMPLES = '''
- name: Find
manala.path.find:
'''
32 changes: 32 additions & 0 deletions tests/integration/targets/find/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---

- name: Find
vars:
path: /tmp/integration/find
block:
- name: Find | Setup root
ansible.builtin.file: # noqa: risky-file-permissions
path: "{{ path }}"
state: "{{ item }}"
loop: [absent, directory]
- name: Find | Setup paths
manala.path.path:
path: "{{ [path, item.path] | ansible.builtin.path_join }}"
content: "{{ item.content }}"
loop:
- path: foo
content: foo
- path: bar
content: bar
- name: Find | Converge
manala.path.find:
path: "{{ path }}"
register: paths
- name: Find | Verify
vars:
expected:
- { path: foo, state: file, user: lazy, group: lazy, mode: "0644" }
- { path: bar, state: file, user: lazy, group: lazy, mode: "0644" }
ansible.builtin.assert:
that:
- (paths.paths | ansible.builtin.difference(expected)) | length == 0