ansible icon indicating copy to clipboard operation
ansible copied to clipboard

loop_control "early exit" feature

Open bcoca opened this issue 5 years ago • 10 comments

ISSUE TYPE
  • Feature Pull Request

posisbly fixes #46203 by providing 'until for includes'

COMPONENT NAME

core

bcoca avatar Sep 11 '19 18:09 bcoca

cc @acozine click here for bot help

ansibot avatar Sep 11 '19 18:09 ansibot

Looks like a very nice way to handle these kind of loops!

karstenjakobsen avatar Mar 14 '20 11:03 karstenjakobsen

Not sure that these changes would fix #46203 I tested @bcoca's branch locally with this playbook:

- hosts: localhost
  gather_facts: false
  tasks:
  - name: testing block looping
    block:
      - debug:
          msg: "{{ item }}"
    loop:
      - 1
      - 2
      - 3
    loop_control:
      until: item > 2

Which yields:

ansible-playbook test/integration/targets/loop_control/block_play.yml
[WARNING]: You are running the development version of Ansible. You should only run Ansible from "devel" if you are modifying the
Ansible engine, or trying out features under development. This is a rapidly changing source of code and can become unstable at any
point.
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
ERROR! 'loop' is not a valid attribute for a Block

The error appears to be in '/Users/jeffmartin/dev/ansible/test/integration/targets/loop_control/block_play.yml': line 4, column 5, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  tasks:
  - name: testing block looping
    ^ here

Am I missing something?

nodeselector avatar Apr 18 '20 21:04 nodeselector

Ah I see what I was missing, this fix would only apply to includes not block. This works great:

- hosts: localhost
  gather_facts: false
  tasks:
  - name: testing inlcude looping
    include_tasks: some_tasks.yml
    loop:
      - 1
      - 2
      - 3
    loop_control:
      until: item > 2

nodeselector avatar Apr 18 '20 22:04 nodeselector

Can you guys please fix this, the people are really hoping for this feature.

abdulrabbani00 avatar Oct 29 '20 21:10 abdulrabbani00

I've had some success using when as described here. This is not exactly an until or break feature, but it can be used for the same purpose.

pini-gh avatar Jun 19 '21 20:06 pini-gh

@bcoca is this still intended to be made a core feature? would be very useful to have, it makes a lot of sense to be able to break out of a loop based on any condition.

Mohammad-Atif-Khan avatar Apr 25 '23 22:04 Mohammad-Atif-Khan

probably, but not a priority right now

bcoca avatar Apr 26 '23 03:04 bcoca

Is this still being worked on?

LennertMertens avatar Jan 31 '24 12:01 LennertMertens

@LennertMertens it is still on my list, but it is not planned to make it in anytime soon

bcoca avatar Feb 01 '24 15:02 bcoca