Loop a play over several group sets

27 views
Skip to first unread message

Doug OLeary

unread,
Mar 6, 2019, 11:07:20 AM3/6/19
to Ansible Project
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

Vladimir Botka

unread,
Mar 6, 2019, 1:15:22 PM3/6/19
to Ansible Project

On Wednesday, March 6, 2019 at 5:07:20 PM UTC+1, Doug OLeary wrote:
[...]
I'd like to loop at the play level rather than the task level.

 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

Doug OLeary

unread,
Mar 6, 2019, 1:37:32 PM3/6/19
to Ansible Project
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.

Doug OLeary

unread,
Mar 6, 2019, 2:08:43 PM3/6/19
to Ansible Project
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:

---
- name: Main_play
  include_tasks: subtasks.yaml
  loop:
    - admins
    - ms1s
    - ms2s

With subtasks being just a straight debug:

---
- name: subtask1
  debug:
    msg: "running on {{inventory_hostname}}"

include_tasks was added in 2.4.4 and I'm running 2.7.7 so that should be good.  Looking at the docs for include_tasks, it looks like that has to be defined in the tasks section of the playbook.  The only way I was able to get this working is by changing it to:

---
- name: Main_play
  hosts: all

  tasks:
  - include_tasks: subtasks.yaml
    loop:
      - admins
      - ms1s
      - ms2s

but that kills the whole goal of what I'm trying to accomplish.  

$ ansible-playbook prime_tasks.yaml --list-tasks

playbook: prime_tasks.yaml

  play #1 (all): Main_play      TAGS: []
    tasks:
      include_tasks     TAGS: []


Did I misunderstand your reply?  

Thanks

Doug O'Leary

On Wednesday, March 6, 2019 at 12:15:22 PM UTC-6, Vladimir Botka wrote:
Reply all
Reply to author
Forward
0 new messages