# How to execute a loop of actions on differents hosts?

**URL:** <https://forum.ansible.com/t/how-to-execute-a-loop-of-actions-on-differents-hosts/4522>\
**Category:** Get Help\
**Tags:** loop, playbook\
**Created:** [March 22, 2024, 8:05pm UTC](https://forum.ansible.com/t/how-to-execute-a-loop-of-actions-on-differents-hosts/4522 "2024-03-22T20:05:16Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [March 26, 2024, 9:02pm UTC](https://forum.ansible.com/t/how-to-execute-a-loop-of-actions-on-differents-hosts/4522/8 "2024-03-26T21:02:25Z")

</div>

Now you’re talkin’! This is beautiful.

Well, as a request for help, it’s beautiful compared to your first attempt. 🙂 I wouldn’t have guessed this structure in a million years based on the initial description.

As an example of “How To Ansible”, it’s not so beautiful. The big problem — and understand, I’ve only been looking at this for a little while so I could be missing some subtleties — everything seems to be “up a level” above where it ought to be. I’m guessing that your playbooks should probably be roles, and your roles (if you have any) should probably be task files within those roles. Because what you _want_ to do is something like this:

```yaml
# N.B. This won't work!
- import_playbook: ../common/launch_test_retrieve_logs.yml
  vars:
    var_file: "{{ var_file }}"
  loop: '{{ test_format }}'
  loop_control:
    loop_var: test_name

```

But that won’t work, because you can’t loop over “PlaybookInclude” – an internal name apparently for what `import_playbook` is/does. At least, that’s in the error message. I tried it anyway.

Rather, what might accomplish your goal is more like:

```yaml
---
# tests-playbook.yml
- name: Tests playbook
  hosts: localhost
  gather_facts: false
  vars:
    test_format:
      - svsGlissando_10000
      - svsGlissando_20000
      - svsGlissando_30000
      - svsGlissando_40000
      - svsTremolo_10000
      - svsTremolo_20000
  tasks:
    - name: Include the glissando test roles
      ansible.builtin.include_role:
        name: glissando_tests
        tasks_from: main.yml
      loop: '{{ test_format }}'
      when: test_name is search("Glissando")
      loop_control:
        loop_var: test_name

    - name: Include the tremolo test roles
      ansible.builtin.include_role:
        name: tremolo_tests
        tasks_from: main.yml
      loop: '{{ test_format }}'
      when: test_name is search("Tremolo")
      loop_control:
        loop_var: test_name

```

You’ve got more going on than this example solves, but I’m pretty sure your playbooks should be roles. Restructuring everything is not going to be trivial, but I expect it will be worth it. You’re basically shifting from a code-driven flow to a data-driven flow. That’s not trivial, but it is possible with roles; with playbooks – not so much.

Feel free to follow-up with additional questions, which I’m sure will pop up as you dig into the details.

---

_[View the full topic](https://forum.ansible.com/t/how-to-execute-a-loop-of-actions-on-differents-hosts/4522)._
