New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add info about loop based on jinja2 loop var #42134

Merged
merged 11 commits into from Dec 7, 2018

Conversation

Projects
None yet
5 participants
@sivel
Member

sivel commented Jun 29, 2018

SUMMARY

This is a proof of concept PR that adds a ansible_loop "variable" that follows the semantics of the loop variable accessible in the jinja2 {% for %} loop construct (http://jinja.pocoo.org/docs/dev/templates/#for)

Adds:

  • ansible_loop.items - list of all items in the loop
  • ansible_loop.index - The current iteration of the loop. (1 indexed)
  • ansible_loop.index0 - The current iteration of the loop. (0 indexed)
  • ansible_loop.first - True if first iteration
  • ansible_loop.last - True if last iteration
  • ansible_loop.length - The number of items in the sequence
  • ansible_loop.previtem - The item from the previous iteration of the loop. Undefined during the first iteration.
  • ansible_loop.nextitem - The item from the following iteration of the loop. Undefined during the last iteration.
ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

lib/ansible/executor/task_executor.py

ANSIBLE VERSION
2.7
ADDITIONAL INFORMATION

@guoqiao

This comment has been minimized.

Contributor

guoqiao commented Jul 31, 2018

Thanks @sivel I want this feature, too. It's the nature way to do loop.

@bcoca

bcoca approved these changes Sep 14, 2018

@sivel

This comment has been minimized.

Member

sivel commented Sep 14, 2018

FWIW, I just added the extended keyword, because I was getting timeouts in CI. Trying to evaluate why that is happening.

Keeping the extended keyword, otherwise this could add a lot more info than expected.

@ansibot ansibot added docs and removed stale_ci labels Nov 13, 2018

@sivel sivel changed the title from Add info about loop based on jinja2 loop var to [WIP] Add info about loop based on jinja2 loop var Nov 13, 2018

@ansibot ansibot added the WIP label Nov 13, 2018

@sivel sivel changed the title from [WIP] Add info about loop based on jinja2 loop var to Add info about loop based on jinja2 loop var Nov 13, 2018

@ansibot ansibot removed the WIP label Nov 13, 2018

@bcoca

This comment has been minimized.

@sivel

This comment has been minimized.

Member

sivel commented Dec 7, 2018

bot_status

@ansibot

This comment has been minimized.

Contributor

ansibot commented Dec 7, 2018

Components

changelogs/fragments/loop-info.yaml
support: community
maintainers:

docs/docsite/rst/reference_appendices/special_variables.rst
support: core
maintainers: acozine

docs/docsite/rst/user_guide/playbooks_loops.rst
support: core
maintainers: acozine

lib/ansible/executor/task_executor.py
support: core
maintainers:

lib/ansible/playbook/loop_control.py
support: core
maintainers:

Metadata

waiting_on: ansible
changes_requested_by: null
needs_info: False
needs_revision: False
needs_rebase: False
merge_commits: []
too many files or commits: False
mergeable_state: clean
shippable_status: success
maintainer_shipits (module maintainers): 0
community_shipits (namespace maintainers): 0
ansible_shipits (core team members): 1
shipit_actors (maintainers or core team members): sivel
shipit_actors_other: []
automerge: automerge shipit test failed

click here for bot help

@ansibot ansibot added core_review and removed needs_revision labels Dec 7, 2018

@@ -325,7 +325,7 @@ def _run_loop(self, items):
if extended:
task_vars['ansible_loop'] = {
'items': items,
'allitems': items,

This comment has been minimized.

@bcoca

bcoca Dec 7, 2018

Member

bikeshed: 'all' would be enough?

This comment has been minimized.

@sivel

sivel Dec 7, 2018

Member

I'm not sure. I lean more towards allitems, but also thought of ansible_loop.loop but that felt oddly redundant.

@bcoca

bcoca approved these changes Dec 7, 2018

@sivel sivel merged commit 9007dbe into ansible:devel Dec 7, 2018

1 check passed

Shippable Run 97223 status is SUCCESS.
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment