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

后端 未结 5 2339
遥遥无期
遥遥无期 2020-12-05 04:47

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         


        
5条回答
  •  情深已故
    2020-12-05 05:11

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

提交回复
热议问题