mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-28 13:21:25 -07:00
Expose loop_control.loop_var as ansible_loop_var (#54240)
* Expose the loop_var as ansible_loop_var * Add docs * fix assert test * Indicate version added
This commit is contained in:
parent
a5a7c7cb80
commit
6996926d89
6 changed files with 26 additions and 0 deletions
2
changelogs/fragments/expose-loop-var-name.yml
Normal file
2
changelogs/fragments/expose-loop-var-name.yml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
minor_changes:
|
||||||
|
- loop - expose loop var name as ``ansible_loop_var``
|
|
@ -28,6 +28,9 @@ ansible_limit
|
||||||
ansible_loop
|
ansible_loop
|
||||||
A dictionary/map containing extended loop information when enabled via ``loop_control.extended``
|
A dictionary/map containing extended loop information when enabled via ``loop_control.extended``
|
||||||
|
|
||||||
|
ansible_loop_var
|
||||||
|
The name of the value provided to ``loop_control.loop_var``. Added in ``2.8``
|
||||||
|
|
||||||
ansible_play_batch
|
ansible_play_batch
|
||||||
List of active hosts in the current play run limited by the serial, aka 'batch'. Failed/Unreachable hosts are not considered 'active'.
|
List of active hosts in the current play run limited by the serial, aka 'batch'. Failed/Unreachable hosts are not considered 'active'.
|
||||||
|
|
||||||
|
|
|
@ -400,6 +400,14 @@ Variable Description
|
||||||
loop_control:
|
loop_control:
|
||||||
extended: yes
|
extended: yes
|
||||||
|
|
||||||
|
.. versionadded:: 2.8
|
||||||
|
|
||||||
|
As of Ansible 2.8 you can get the name of the value provided to ``loop_control.loop_var`` using the ``ansible_loop_var`` variable
|
||||||
|
|
||||||
|
For role authors, writing roles that allow loops, instead of dictating the required ``loop_var`` value, you can gather the value via::
|
||||||
|
|
||||||
|
"{{ lookup('vars', ansible_loop_var) }}"
|
||||||
|
|
||||||
.. _migrating_to_loop:
|
.. _migrating_to_loop:
|
||||||
|
|
||||||
Migrating from with_X to loop
|
Migrating from with_X to loop
|
||||||
|
|
|
@ -319,6 +319,8 @@ class TaskExecutor:
|
||||||
no_log = False
|
no_log = False
|
||||||
items_len = len(items)
|
items_len = len(items)
|
||||||
for item_index, item in enumerate(items):
|
for item_index, item in enumerate(items):
|
||||||
|
task_vars['ansible_loop_var'] = loop_var
|
||||||
|
|
||||||
task_vars[loop_var] = item
|
task_vars[loop_var] = item
|
||||||
if index_var:
|
if index_var:
|
||||||
task_vars[index_var] = item_index
|
task_vars[index_var] = item_index
|
||||||
|
@ -376,6 +378,7 @@ class TaskExecutor:
|
||||||
# now update the result with the item info, and append the result
|
# now update the result with the item info, and append the result
|
||||||
# to the list of results
|
# to the list of results
|
||||||
res[loop_var] = item
|
res[loop_var] = item
|
||||||
|
res['ansible_loop_var'] = loop_var
|
||||||
if index_var:
|
if index_var:
|
||||||
res[index_var] = item_index
|
res[index_var] = item_index
|
||||||
if extended:
|
if extended:
|
||||||
|
|
|
@ -6,6 +6,7 @@ ok: [localhost] => (item=item_A)
|
||||||
|
|
||||||
TASK [assert] ******************************************************************
|
TASK [assert] ******************************************************************
|
||||||
ok: [localhost] => (item=item_A) => {
|
ok: [localhost] => (item=item_A) => {
|
||||||
|
"ansible_loop_var": "item",
|
||||||
"changed": false,
|
"changed": false,
|
||||||
"item": "item_A",
|
"item": "item_A",
|
||||||
"msg": "All assertions passed"
|
"msg": "All assertions passed"
|
||||||
|
|
|
@ -325,3 +325,12 @@
|
||||||
loop_control:
|
loop_control:
|
||||||
extended: true
|
extended: true
|
||||||
when: item == 'banana'
|
when: item == 'banana'
|
||||||
|
|
||||||
|
- name: Validate the loop_var name
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- ansible_loop_var == 'alvin'
|
||||||
|
loop:
|
||||||
|
- 1
|
||||||
|
loop_control:
|
||||||
|
loop_var: alvin
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue