# Loop a play over several group sets

**URL:** <https://forum.ansible.com/t/loop-a-play-over-several-group-sets/29834>\
**Category:** Ansible Project\
**Created:** [March 6, 2019, 4:07pm UTC](https://forum.ansible.com/t/loop-a-play-over-several-group-sets/29834 "2019-03-06T16:07:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Doug\_OLeary](https://avatars.discourse-cdn.com/v4/letter/d/c0e974/32.png) [@Doug\_OLeary](https://forum.ansible.com/u/Doug_OLeary)\
**Post date:** [March 6, 2019, 4:07pm UTC](https://forum.ansible.com/t/loop-a-play-over-several-group-sets/29834/1 "2019-03-06T16:07:20Z")

</div>

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:

```auto
- 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

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [March 6, 2019, 6:15pm UTC](https://forum.ansible.com/t/loop-a-play-over-several-group-sets/29834/2 "2019-03-06T18:15:22Z")

</div>

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  
`

---

<div class="post-metadata">

**Author:** ![Doug\_OLeary](https://avatars.discourse-cdn.com/v4/letter/d/c0e974/32.png) [@Doug\_OLeary](https://forum.ansible.com/u/Doug_OLeary)\
**Post date:** [March 6, 2019, 6:37pm UTC](https://forum.ansible.com/t/loop-a-play-over-several-group-sets/29834/3 "2019-03-06T18:37:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Doug\_OLeary](https://avatars.discourse-cdn.com/v4/letter/d/c0e974/32.png) [@Doug\_OLeary](https://forum.ansible.com/u/Doug_OLeary)\
**Post date:** [March 6, 2019, 7:08pm UTC](https://forum.ansible.com/t/loop-a-play-over-several-group-sets/29834/4 "2019-03-06T19:08:43Z")

</div>

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:
