@ -31,7 +31,7 @@ from ansible.utils.listify import listify_lookup_plugin_terms
from ansible . utils . unsafe_proxy import to_unsafe_text , wrap_var
from ansible . vars . clean import namespace_facts , clean_facts
from ansible . utils . display import Display
from ansible . utils . vars import combine_vars , isidentifier
from ansible . utils . vars import combine_vars
display = Display ( )
@ -332,6 +332,13 @@ class TaskExecutor:
( self . _task , tmp_task ) = ( tmp_task , self . _task )
( self . _play_context , tmp_play_context ) = ( tmp_play_context , self . _play_context )
res = self . _execute ( variables = task_vars )
if self . _task . register :
# Ensure per loop iteration results are registered in case `_execute()`
# returns early (when conditional, failure, ...).
# This is needed in case the registered variable is used in the loop label template.
task_vars [ self . _task . register ] = res
task_fields = self . _task . dump_attrs ( )
( self . _task , tmp_task ) = ( tmp_task , self . _task )
( self . _play_context , tmp_play_context ) = ( tmp_play_context , self . _play_context )
@ -657,9 +664,6 @@ class TaskExecutor:
# update the local copy of vars with the registered value, if specified,
# or any facts which may have been generated by the module execution
if self . _task . register :
if not isidentifier ( self . _task . register ) :
raise AnsibleError ( " Invalid variable name in ' register ' specified: ' %s ' " % self . _task . register )
vars_copy [ self . _task . register ] = result
if self . _task . async_val > 0 :