Skip to content

Commit d2cda5c

Browse files
authored
Merge branch 'main' into feat/find-module
2 parents 10f6041 + 83cd32d commit d2cda5c

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

CHANGELOG.md

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

1010
### Added
1111

12+
- Add a "file" parameter to "path" module
1213
- Add a "find" module
1314

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

plugins/action/path.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def run(self, tmp=None, task_vars=None):
2020
'path': {'type': 'path', 'required': True},
2121
'state': {'type': 'str', 'choices': ['present', 'absent', 'file'], 'default': 'present'},
2222
'content': {'type': 'str'},
23+
'file': {'type': 'path'},
2324
'template': {'type': 'path'},
2425
'vars': {'type': 'dict', 'default': {}},
2526
'user': {'type': 'str'},
@@ -28,7 +29,7 @@ def run(self, tmp=None, task_vars=None):
2829
'validate': {'type': 'str'},
2930
},
3031
mutually_exclusive=(
31-
['content', 'template'],
32+
['content', 'file', 'template'],
3233
),
3334
)
3435

@@ -66,6 +67,28 @@ def run(self, tmp=None, task_vars=None):
6667
shared_loader_obj=self._shared_loader_obj,
6768
).run(task_vars=combine_vars(task_vars, args['vars']))
6869
)
70+
elif (args['state'] in ['present', 'file']) and args['file']:
71+
display.v('Use "ansible.builtin.copy" to ensure "%s" file.' % args['path'])
72+
task = self._task.copy()
73+
task.args = {
74+
'dest': args['path'],
75+
'src': args['file'],
76+
'owner': args['user'],
77+
'group': args['group'],
78+
'mode': args['mode'],
79+
'validate': args['validate'],
80+
}
81+
result.update(
82+
self._shared_loader_obj.action_loader.get(
83+
'ansible.builtin.copy',
84+
task=task,
85+
connection=self._connection,
86+
play_context=self._play_context,
87+
loader=self._loader,
88+
templar=self._templar,
89+
shared_loader_obj=self._shared_loader_obj,
90+
).run(task_vars=task_vars)
91+
)
6992
elif (args['state'] in ['present', 'file']) and args['template']:
7093
display.v('Use "ansible.builtin.template" to ensure "%s" template.' % args['path'])
7194
task = self._task.copy()
@@ -89,7 +112,7 @@ def run(self, tmp=None, task_vars=None):
89112
).run(task_vars=combine_vars(task_vars, args['vars']))
90113
)
91114
elif args['state'] == 'file':
92-
raise AnsibleActionFail('one of the following is required: content, template')
115+
raise AnsibleActionFail('one of the following is required: content, file, template')
93116
elif args['state'] == 'present':
94117
display.v('Use "ansible.builtin.file" to ensure "%s" is a directory.' % args['path'])
95118
result.update(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
file

tests/integration/targets/path/tasks/main.yaml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,45 @@
108108
- stats.results[0].stat.exists is true
109109
- contents.results[0].content | b64decode == 'content'
110110

111+
########
112+
# File #
113+
########
114+
115+
- name: File
116+
tags: file
117+
vars:
118+
path: /tmp/integration/path/file
119+
block:
120+
- name: File | Setup path
121+
ansible.builtin.file: # noqa: risky-file-permissions
122+
path: "{{ path }}"
123+
state: "{{ item }}"
124+
loop: [absent, directory]
125+
- name: File | Converge
126+
manala.path.path:
127+
path: "{{ [path, item.path] | ansible.builtin.path_join }}"
128+
file: "{{ item.file }}"
129+
loop:
130+
- path: file
131+
file: file
132+
- name: File | Stats
133+
ansible.builtin.stat:
134+
path: "{{ [path, item.path] | ansible.builtin.path_join }}"
135+
register: stats
136+
loop:
137+
- path: file
138+
- name: File | Contents
139+
ansible.builtin.slurp:
140+
src: "{{ [path, item.path] | ansible.builtin.path_join }}"
141+
register: contents
142+
loop:
143+
- path: file
144+
- name: File | Verify
145+
ansible.builtin.assert:
146+
that:
147+
- stats.results[0].stat.exists is true
148+
- contents.results[0].content | b64decode == 'file'
149+
111150
############
112151
# Template #
113152
############

0 commit comments

Comments
 (0)