@@ -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" )
0 commit comments