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

teach ansible about loop_control/break_when #55677

Open
wants to merge 1 commit into
base: devel
from

Conversation

Projects
None yet
3 participants
@larsks
Copy link
Contributor

commented Apr 23, 2019

SUMMARY

this introduces the break_when loop_control attribute, which permits a
task loop to stop prematurely if the specified condition is true. This
can save time and reduce the amount of extraneous output if the
execution of a large loop can short circuited.

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

Loops

ADDITIONAL INFORMATION

This PR includes tests and documentation.

Here's a simple example of this feature in practice. The playbook can create an arbitrary number of containers (five by default). If you run it a second time with container_count less than the previous value of container_count, it needs to remove any existing containers above container_count created by the previous run. Using break_when lets the loop exit as soon as there are no more containers to remove:

---
- hosts: localhost
  gather_facts: false
  vars:
    container_count: 5
    max_container_count: 100

  tasks:
    - name: create containers
      docker_container:
        name: "service-{{ item }}"
        state: present
        image: alpine
        command: /bin/sh
        tty: true
        interactive: true
        detach: true
      loop: "{{ range(container_count|int)|list }}"

    - name: remove containers created by previous runs
      docker_container:
        name: "service-{{ item }}"
        state: absent
      register: result
      loop: "{{ range(container_count|int, max_container_count)|list }}"
      loop_control:
        break_when: result is not changed
@ansibot

This comment has been minimized.

Copy link
Contributor

commented Apr 23, 2019

@larsks larsks force-pushed the larsks:feature/break_when branch from 4ebfb3b to 17c5776 Apr 24, 2019

@ansibot ansibot added core_review and removed needs_revision labels Apr 24, 2019

@larsks larsks force-pushed the larsks:feature/break_when branch from 17c5776 to 76fe152 Apr 24, 2019

@bcoca bcoca removed the needs_triage label Apr 25, 2019

@bcoca bcoca requested review from bcoca and sivel Apr 25, 2019

teach ansible about loop_control/break_when
this introduces the break_when loop_control attribute, which permits a
task loop to stop prematurely if the specified condition is true. This
can save time and reduce the amount of extraneous output if the
execution of a large loop can short circuited.

@larsks larsks force-pushed the larsks:feature/break_when branch from 76fe152 to 5981075 Apr 26, 2019

@ansibot ansibot added the stale_ci label May 6, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.