Ansible: How to iterate over a role with an array?

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 }}" }

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' }

Now supported as of Ansible 2.3.0:

- name: myrole
  with_items:
    - "aone"
    - "atwo"
  include_role:
    name: myrole
  vars:
    thing: "{{ item }}"

Tags:

Ansible