Skip to content

Commit 730333e

Browse files
authored
net_put: Handle template in src field (#103)
net_put: Handle template in src field Reviewed-by: Nathaniel Case <this.is@nathanielca.se> https://github.com/Qalthos
1 parent 9067b2c commit 730333e

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
bugfixes:
3+
- net_put - Restore missing function removed when action plugin stopped inheriting NetworkActionBase

plugins/action/net_put.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ def run(self, tmp=None, task_vars=None):
7777

7878
if mode == "text":
7979
try:
80-
self._handle_template(convert_data=False)
81-
except ValueError as exc:
80+
self._handle_src_option(convert_data=False)
81+
except AnsibleError as exc:
8282
return dict(failed=True, msg=to_text(exc))
8383

8484
# Now src has resolved file write to disk in current diectory for scp
@@ -216,6 +216,46 @@ def _get_working_path(self):
216216
cwd = self._task._role._role_path
217217
return cwd
218218

219+
def _handle_src_option(self, convert_data=True):
220+
src = self._task.args.get("src")
221+
working_path = self._get_working_path()
222+
223+
if os.path.isabs(src) or urlsplit("src").scheme:
224+
source = src
225+
else:
226+
source = self._loader.path_dwim_relative(
227+
working_path, "templates", src
228+
)
229+
if not source:
230+
source = self._loader.path_dwim_relative(working_path, src)
231+
232+
if not os.path.exists(source):
233+
raise AnsibleError("path specified in src not found")
234+
235+
try:
236+
with open(source, "r") as f:
237+
template_data = to_text(f.read())
238+
except IOError as e:
239+
raise AnsibleError(
240+
"unable to load src file {0}, I/O error({1}): {2}".format(
241+
source, e.errno, e.strerror
242+
)
243+
)
244+
245+
# Create a template search path in the following order:
246+
# [working_path, self_role_path, dependent_role_paths, dirname(source)]
247+
searchpath = [working_path]
248+
if self._task._role is not None:
249+
searchpath.append(self._task._role._role_path)
250+
if hasattr(self._task, "_block:"):
251+
dep_chain = self._task._block.get_dep_chain()
252+
if dep_chain is not None:
253+
for role in dep_chain:
254+
searchpath.append(role._role_path)
255+
searchpath.append(os.path.dirname(source))
256+
self._templar.environment.loader.searchpath = searchpath
257+
self._task.args["src"] = self._templar.template(template_data)
258+
219259
def _get_network_os(self, task_vars):
220260
if "network_os" in self._task.args and self._task.args["network_os"]:
221261
display.vvvv("Getting network OS from task argument")

plugins/action/network.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ def run(self, task_vars=None):
4141
if config_module and self._task.args.get("src"):
4242
try:
4343
self._handle_src_option()
44-
except AnsibleError as e:
45-
return {"failed": True, "msg": e.message, "changed": False}
44+
except AnsibleError as exc:
45+
return dict(failed=True, msg=to_text(exc))
4646

4747
result = super(ActionModule, self).run(task_vars=task_vars)
4848

0 commit comments

Comments
 (0)