summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Gaffney <andrew@agaffney.org>2016-08-31 07:59:43 -0600
committerBrian Coca <bcoca@users.noreply.github.com>2016-08-31 09:59:43 -0400
commitf65a3ce547aa0a86de681f8b51c5f42d6d6b9322 (patch)
tree03c65a17c2be6337a656a9ce1bd40dc8cf3e5a47
parent61e7c3af1a7dffd0258a8ae94ccb95f3054e08eb (diff)
downloadansible-f65a3ce547aa0a86de681f8b51c5f42d6d6b9322.tar.gz
Support for specifying item label in a loop (#17294)
-rw-r--r--lib/ansible/executor/task_executor.py6
-rw-r--r--lib/ansible/playbook/loop_control.py1
-rw-r--r--lib/ansible/plugins/callback/__init__.py2
3 files changed, 9 insertions, 0 deletions
diff --git a/lib/ansible/executor/task_executor.py b/lib/ansible/executor/task_executor.py
index 295116209f..85fc9d06b8 100644
--- a/lib/ansible/executor/task_executor.py
+++ b/lib/ansible/executor/task_executor.py
@@ -234,9 +234,11 @@ class TaskExecutor:
task_vars = self._job_vars
loop_var = 'item'
+ label = None
if self._task.loop_control:
# the value may be 'None', so we still need to default it back to 'item'
loop_var = self._task.loop_control.loop_var or 'item'
+ label = self._task.loop_control.label or ('{{' + loop_var + '}}')
if loop_var in task_vars:
display.warning("The loop variable '%s' is already in use. You should set the `loop_var` value in the `loop_control` option for the task to something else to avoid variable collisions and unexpected behavior." % loop_var)
@@ -266,6 +268,10 @@ class TaskExecutor:
res[loop_var] = item
res['_ansible_item_result'] = True
+ if not label is None:
+ templar = Templar(loader=self._loader, shared_loader_obj=self._shared_loader_obj, variables=self._job_vars)
+ res['_ansible_item_label'] = templar.template(label, fail_on_undefined=False)
+
self._rslt_q.put(TaskResult(self._host.name, self._task._uuid, res), block=False)
results.append(res)
del task_vars[loop_var]
diff --git a/lib/ansible/playbook/loop_control.py b/lib/ansible/playbook/loop_control.py
index 9cdd18ffd9..6a7d9f9b39 100644
--- a/lib/ansible/playbook/loop_control.py
+++ b/lib/ansible/playbook/loop_control.py
@@ -29,6 +29,7 @@ from ansible.playbook.base import Base
class LoopControl(Base):
_loop_var = FieldAttribute(isa='str')
+ _label = FieldAttribute(isa='str')
def __init__(self):
super(LoopControl, self).__init__()
diff --git a/lib/ansible/plugins/callback/__init__.py b/lib/ansible/plugins/callback/__init__.py
index c009cf0574..19ced6d49a 100644
--- a/lib/ansible/plugins/callback/__init__.py
+++ b/lib/ansible/plugins/callback/__init__.py
@@ -174,6 +174,8 @@ class CallbackBase:
def _get_item(self, result):
if result.get('_ansible_no_log', False):
item = "(censored due to no_log)"
+ elif result.get('_ansible_item_label', False):
+ item = result.get('_ansible_item_label')
else:
item = result.get('item', None)