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

[WIP] Block looping #55761

Open
wants to merge 1 commit into
base: devel
from

Conversation

Projects
None yet
3 participants
@agaffney
Copy link
Contributor

commented Apr 25, 2019

SUMMARY

This implements block looping as syntactic sugar for include_tasks with loop.

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

block

ADDITIONAL INFORMATION

Test playbook:

- hosts: localhost
  gather_facts: no
  tasks:
    - debug:
        msg: in foo.yml

    - name: block
      loop: '{{ range(1, 3) | list }}'
      block:
        - debug:
            msg: in block {{ item }}

        - debug:
            msg: in block again {{ item }}

        - name: inner block
          loop: '{{ range(1, 3) | list }}'
          loop_control:
            loop_var: item2
          block:
            - debug:
                msg: in inner block {{ item }} - {{ item2 }}

Output:

$ ansible-playbook /tmp/ansible_test/block_loop/foo.yml

PLAY [localhost] **************************************************************************************************************************************************************************************************************************************************************

TASK [debug] ******************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "in foo.yml"
}

TASK [include_tasks] **********************************************************************************************************************************************************************************************************************************************************
included: /tmp/tmpihr1a8 for localhost
included: /tmp/tmpihr1a8 for localhost

TASK [debug] ******************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "in block 1"
}

TASK [debug] ******************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "in block again 1"
}

TASK [include_tasks] **********************************************************************************************************************************************************************************************************************************************************
included: /tmp/tmpvsTo52 for localhost
included: /tmp/tmpvsTo52 for localhost

TASK [debug] ******************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "in inner block 1 - 1"
}

TASK [debug] ******************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "in inner block 1 - 2"
}

TASK [debug] ******************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "in block 2"
}

TASK [debug] ******************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "in block again 2"
}

TASK [include_tasks] **********************************************************************************************************************************************************************************************************************************************************
included: /tmp/tmpC6Ej_z for localhost
included: /tmp/tmpC6Ej_z for localhost

TASK [debug] ******************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "in inner block 2 - 1"
}

TASK [debug] ******************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "in inner block 2 - 2"
}

PLAY RECAP ********************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=15   changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

@agaffney agaffney force-pushed the agaffney:block_loop branch from cf4a542 to fa51032 Apr 25, 2019

@agaffney

This comment has been minimized.

Copy link
Contributor Author

commented Apr 25, 2019

So, this works, but it creates a UX issue, since blocks with rescue/always and includes don't really play well together, which is just going to confuse users.

@bcoca bcoca removed the needs_triage label Apr 25, 2019

@ansibot ansibot added the stale_ci label May 3, 2019

@ansibot

This comment has been minimized.

Copy link
Contributor

commented May 24, 2019

The test ansible-test sanity --test pep8 [explain] failed with 2 errors:

lib/ansible/playbook/block.py:39:1: E303 too many blank lines (3)
lib/ansible/playbook/block.py:137:17: E265 block comment should start with '# '

click here for bot help

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.