How to execute a loop of actions on differents hosts?

Now you’re talkin’! This is beautiful.

Well, as a request for help, it’s beautiful compared to your first attempt. :slight_smile: 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:

# 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:

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

2 Likes