Skip to content
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

loop_control "early exit" feature #62151

Merged
merged 8 commits into from
Sep 5, 2024
Merged

loop_control "early exit" feature #62151

merged 8 commits into from
Sep 5, 2024

Conversation

bcoca
Copy link
Member

@bcoca bcoca commented Sep 11, 2019

ISSUE TYPE
  • Feature Pull Request

posisbly fixes #46203 by providing 'until for includes'

COMPONENT NAME

core

@bcoca bcoca changed the title loop_control until feature [WIP] loop_control until feature Sep 11, 2019
@ansibot
Copy link
Contributor

ansibot commented Sep 11, 2019

@ansibot ansibot added WIP This issue/PR is a work in progress. Nevertheless it was shared for getting input from peers. affects_2.10 This issue/PR affects Ansible v2.10 docs This issue/PR relates to or includes documentation. feature This issue/PR relates to a feature request. needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. support:community This issue/PR relates to code supported by the Ansible community. labels Sep 11, 2019
@sivel sivel removed the needs_triage Needs a first human triage before being processed. label Sep 17, 2019
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Sep 23, 2019
@ansibot ansibot removed the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Feb 21, 2020
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Feb 29, 2020
@karstenjakobsen
Copy link
Contributor

karstenjakobsen commented Mar 14, 2020

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

@nodeselector
Copy link

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
Copy link

nodeselector commented Apr 18, 2020

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

@abdulrabbani00
Copy link

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

@ansibot ansibot added pre_azp This PR was last tested before migration to Azure Pipelines. and removed stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. labels Dec 8, 2020
@pini-gh
Copy link

pini-gh commented Jun 19, 2021

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.

@ansibot ansibot added the needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html label Jun 23, 2022
@bcoca bcoca marked this pull request as draft July 15, 2022 15:10
@Mohammad-Atif-Khan
Copy link

@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.

@bcoca
Copy link
Member Author

bcoca commented Apr 26, 2023

probably, but not a priority right now

@ansibot ansibot added the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label Feb 1, 2024
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Feb 15, 2024
@bcoca bcoca changed the title [WIP] loop_control until feature loop_control "early exit" feature Aug 20, 2024
@ansibot ansibot removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. labels Aug 20, 2024
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Sep 1, 2024
@bcoca bcoca marked this pull request as ready for review September 3, 2024 15:08
@s-hertel

This comment was marked as resolved.

This comment was marked as resolved.

@ansibot ansibot removed the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Sep 5, 2024
@ansibot ansibot added the stale_review Updates were made after the last review and the last review is more than 7 days old. label Sep 5, 2024
@s-hertel s-hertel merged commit 4fa5124 into ansible:devel Sep 5, 2024
71 checks passed
@bcoca bcoca deleted the loop_until branch September 5, 2024 15:19
infame-io pushed a commit to infame-io/ansible that referenced this pull request Sep 7, 2024
* add a loop_control break_when directive to break out of a loop after any item

* remove loop var as normal exit would

* example usage:

- name: generate a random password up to 10 times, until it matches the policy
  set_fact:
    password: "{{ lookup('password', '/dev/null', chars=character_set, length=length) }}"
  loop: "{{ range(0, 10) }}"
  loop_control:
    break_when:
      - password is match(password_policy)

Co-authored-by: s-hertel <19572925+s-hertel@users.noreply.github.com>
@ansible ansible locked and limited conversation to collaborators Oct 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.10 This issue/PR affects Ansible v2.10 docs This issue/PR relates to or includes documentation. feature This issue/PR relates to a feature request. has_issue stale_review Updates were made after the last review and the last review is more than 7 days old. support:community This issue/PR relates to code supported by the Ansible community. support:core This issue/PR relates to code supported by the Ansible Engineering Team. WIP This issue/PR is a work in progress. Nevertheless it was shared for getting input from peers.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature: Allow until-loops on blocks or includes
10 participants