How to dynamically end a loop in Ansible

Due to the fact that we cannot run loop on block with Ansible, therefore I have to do include_tasks in the loop where the referenced file contains more than 1 task.

What if I need to dynamically end the loop earlier (lets say 5 iterations instead of 10 as defined by the loop: statements). trying to use when: condition but that only take in pre-defined/statically defined variables, if I have to update the variable that will be evaluated in when: for each iteration, I have to do register: or set_fact in a task before that, which is not possible when the loop task is running…

Also in the included_tasks I can set meta: end_play to end the loop and the play execution but that is not what I want, the play should continue after the loop.

You can't end it. You can only skip the rest. For example,

    - command: "echo {{ item }}"
      with_sequence: end=5
      register: out
      when: out.stdout|d(0)|int < 3

gives (on localhost)

  changed: [localhost] => (item=1)
  changed: [localhost] => (item=2)
  changed: [localhost] => (item=3)
  skipping: [localhost] => (item=4)
  skipping: [localhost] => (item=5)

See "Ansible loop interface redesign #140"
https://github.com/ansible/proposals/issues/140