Skip to content

Commit de3bf6b

Browse files
authored
Merge pull request #13 from manala/feat/find-module
feat: Add a "find" module
2 parents 83cd32d + d2cda5c commit de3bf6b

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- Add a "file" parameter to "path" module
13+
- Add a "find" module
1314

1415
## [1.1.0] - 2024-09-13
1516

plugins/action/find.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from __future__ import annotations
2+
3+
from ansible.plugins.action import ActionBase
4+
5+
from pathlib import Path
6+
7+
8+
class ActionModule(ActionBase):
9+
'''Return a list of paths based on specific criteria'''
10+
11+
def run(self, tmp=None, task_vars=None):
12+
13+
result = super(ActionModule, self).run(tmp, task_vars)
14+
15+
validation, args = self.validate_argument_spec(
16+
argument_spec={
17+
'path': {'type': 'path', 'required': True},
18+
'patterns': {'type': 'list', 'default': [], 'elements': 'str'},
19+
'excludes': {'type': 'list', 'elements': 'str'},
20+
},
21+
)
22+
23+
find = self._execute_module(
24+
module_name='ansible.builtin.find',
25+
module_args={
26+
'paths': [args['path']],
27+
'file_type': 'file',
28+
'patterns': args['patterns'],
29+
'excludes': args['excludes'],
30+
},
31+
task_vars=task_vars,
32+
)
33+
34+
files = find.pop('files')
35+
36+
result.update(find)
37+
result['paths'] = []
38+
39+
for file in files:
40+
result['paths'].append({
41+
'path': str(Path(file['path']).relative_to(Path(args['path']))),
42+
'state': 'file',
43+
'user': file['pw_name'],
44+
'group': file['gr_name'],
45+
'mode': file['mode'],
46+
})
47+
48+
return result

plugins/modules/find.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
2+
3+
from __future__ import annotations
4+
5+
DOCUMENTATION = '''
6+
---
7+
module: find
8+
short_description: Return a list of paths based on specific criteria
9+
description:
10+
- Return a list of paths based on specific criteria
11+
author:
12+
- Manala (@manala)
13+
'''
14+
15+
EXAMPLES = '''
16+
- name: Find
17+
manala.path.find:
18+
'''
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
3+
- name: Find
4+
vars:
5+
path: /tmp/integration/find
6+
block:
7+
- name: Find | Setup root
8+
ansible.builtin.file: # noqa: risky-file-permissions
9+
path: "{{ path }}"
10+
state: "{{ item }}"
11+
loop: [absent, directory]
12+
- name: Find | Setup paths
13+
manala.path.path:
14+
path: "{{ [path, item.path] | ansible.builtin.path_join }}"
15+
content: "{{ item.content }}"
16+
loop:
17+
- path: foo
18+
content: foo
19+
- path: bar
20+
content: bar
21+
- name: Find | Converge
22+
manala.path.find:
23+
path: "{{ path }}"
24+
register: paths
25+
- name: Find | Verify
26+
vars:
27+
expected:
28+
- { path: foo, state: file, user: lazy, group: lazy, mode: "0644" }
29+
- { path: bar, state: file, user: lazy, group: lazy, mode: "0644" }
30+
ansible.builtin.assert:
31+
that:
32+
- (paths.paths | ansible.builtin.difference(expected)) | length == 0

0 commit comments

Comments
 (0)