41

Is it possible to call a role multiple times in a loop like this:

vars:
  my_array:
    - foo
    - bar
    - baz 
roles:
  - role: foobar
    with_items: my_array

How can we do this?

the
  • 21,007
  • 11
  • 68
  • 101
hewo
  • 786
  • 1
  • 8
  • 17

5 Answers5

60

Now supported as of Ansible 2.3.0:

- name: myrole
  with_items:
    - "aone"
    - "atwo"
  include_role:
    name: myrole
  vars:
    thing: "{{ item }}"
Jakub Bujny
  • 4,400
  • 13
  • 27
ritzk
  • 761
  • 6
  • 3
  • 4
    @tomasbedrich can you verify that? It doesn't work for me. – Simon Woodside Feb 08 '17 at 05:40
  • 8
    Note that if you use any loops in "myrole", you need to add `loop_control: loop_var: foo` to avoid colliding with inner loops. – Zhuoyun Wei May 23 '17 at 12:01
  • Is there any way to provide a whole dictionary as vars per loop iteration? – Migsi Aug 06 '20 at 15:15
  • You should rather link to the docs here https://docs.ansible.com/ansible/latest/collections/ansible/builtin/include_role_module.html instead of to the ticket which was reported on version 2.3.0, as the the feature was already introduced in **version 2.2.0** – Stefan Horning Sep 22 '21 at 10:19
7

There's no way to loop over a role currently but as mentioned in that Google Group discussion you can pass a list or dict to the role and then loop through that internally.

So instead you could do something like:

# loop_role/tasks/main.yml

- name: debug item
  debug: var="{{ item }}"
  with_items: my_array

And then use it like this:

- hosts: all
  vars:
    my_array:
      - foo
      - bar
      - baz 
  roles:
    - { role: loop_role, my_array: "{{ my_array }}" }
ydaetskcoR
  • 53,225
  • 8
  • 158
  • 177
6

You can do so using the include_role module. See docs

According to the docs it was introduced in Ansible 2.2 already (not in 2.3 as others have stated).

The code would then look like

- name: Use role in loop
  ansible.builtin.include_role:
    name: my-role
  vars:
    some_role_variable: '{{ loop_var }}'
  loop:
    - '{{ roleinput1 }}'
    - '{{ roleinput2 }}'
  loop_control:
    loop_var: loop_var
Stefan Horning
  • 1,117
  • 13
  • 17
5

I used something like below on Ansible version 2.8

tasks:
  - name: looping role to create multiple filesystem
    include_role:
      name: /opt/ansible/playbook/app_filesystem
    vars:
      vgname: "{{ item.vgname }}"
      lvname: "{{ item.lvname }}"
      lvsize: "{{ item.lvsize }}"
      mountpoint: "{{ item.mountpoint }}"
    loop:
      - { vgname: 'vgapp', lvname: 'lvapp', lvsize: '30g', mountpoint: '/app' }
      - { vgname: 'vgapp', lvname: 'lvappzk', lvsize: '64g', mountpoint: '/app/z' }
      - { vgname: 'vgapp', lvname: 'lvappdatazk', lvsize: '+100%FREE', mountpoint: '/app/data/zookeeper' }

tasks:
  - name: looping role to create multiple filesystem
    include_role:
      name: /opt/ansible/playbook/app_filesystem
    vars:
      vgname: "{{ item.vgname }}"
      lvname: "{{ item.lvname }}"
      lvsize: "{{ item.lvsize }}"
      mountpoint: "{{ item.mountpoint }}"
    loop:
      - { vgname: 'vgapp', lvname: 'lvapp', lvsize: '30g', mountpoint: '/app' }
      - { vgname: 'vgapp', lvname: 'lvappzk', lvsize: '64g', mountpoint: '/app/zookeeper' }
      - { vgname: 'vgapp', lvname: 'lvappdatazk', lvsize: '+100%FREE', mountpoint: '/app/data/zookeeper' }
David Buck
  • 3,752
  • 35
  • 31
  • 35
Dheeraj
  • 51
  • 1
  • 1
0

Here is a code sample for using include_role looping on my_array:

- name: Use role in loop
  include_role:
    name: myrole
  loop: "{{ my_array }}"
kalaolani
  • 323
  • 1
  • 16