Loop a play over several group sets

Hi;

I read through the docs on loops and am not seeing what I’m looking for. Basically, loops allow a task to run across a set. From the docs:

- name: add several users
  user:
    name: "{{ item }}"
    state: present
    groups: "wheel"
  loop:
     - testuser1
     - testuser2

That would create users testuser1 and testuser2. I’m looking for a way to iterate a set of tasks over a set of groups. For example, my inventory has groups like:

$ grep ‘^[’ hosts
[admins]
[ms1s]
[ms2s]
[ms3s]
[ms4s]
[others]

I currently have separate plays to reboot the hosts in each group, then wait 10 min before the next one kicks off:

  • name: reboot admins
    hosts: admins
    gather_facts: no
    remote_user: root
    tasks:

  • name: reboot admins
    reboot:
    reboot_timeout: 300
    tags: rebootadm

  • name: sleep 10m
    pause:
    minute: 10
    tags: wait10

  • name: reboot ms1s
    hosts: ms1s
    gather_facts: no
    remote_user: root

tasks:

  • name: reboot ms1s
    reboot:
    reboot_timeout: 300
    tags: rebootm1

  • name: sleep 10m
    pause:
    minute: 10
    tags: wait10

What I’d like to do is have that play set up to loop over the groups above - something like loop at the play level rather than the task level:

  • name: reboot admins
    hosts: {{ item }}
    gather_facts: no
    remote_user: root
    loop:
  • admins
  • ms1s
  • ms2s

tasks:

  • name: reboot admins
    reboot:
    reboot_timeout: 300
    tags: rebootadm

  • name: sleep 10m
    pause:
    minute: 10
    tags: wait10

Anyone know of a way to do that?

Thanks

Doug O’Leary

To loop over a set of tasks put the tasks into a file set-of-tasks.yml and use include_tasks

`

  • name: Loop tasks
    include_tasks: set-of-tasks.yml
    loop:

  • admins

  • ms1s

  • ms2s
    `

First; thanks for replying. I appreciate it.

I’ve used include_tasks in a different context so am a little familiar with it. I would have just the reboot and wait10 tasks in set-of-tasks.yml. The tasks would only operate against the current host group? Intriguing. I’ll give that a try.

Thanks again for the tip. I appreciate it.

Hey;

I copied what you have and am getting an error stating:

$ ansible-playbook prime_tasks.yaml --list-tasks
ERROR! ‘include_tasks’ is not a valid attribute for a Play

The entire playbook looks like: