4

I have main file, that can initiate different individual roles.

---
- hosts: all
  gather_facts: False
  roles:
    - role: "{{ rnames }}"
...

when I run individual roles, it runs fine

ansible-playbook -i inv initiate.yml -e rnames=install_packages

But what I prefer is to run multiple roles, by providing a list of roles to rname variable

ansible-playbook -i inv initiate.yml -e rnames=install_packages,config_merge

I tried with_items & loop constructs, but can't seem to figure out.

1
  • wouldn't a playbook that calls exactly these roles solve that? Commented Oct 8, 2022 at 10:52

1 Answer 1

3

Use include roles in a loop. For example, you'll have to split the comma-separated list of roles

  tasks:
    - include_role:
        name: "{{ item }}"
      loop: "{{ rnames.split(',') }}"
2
  • man! that's creative. thanks a bunch.. btw import_role did not work in my case, giving error: ERROR! You cannot use loops on 'import_role' statements. You should use 'include_role' instead. so I used include_role
    – Sollosa
    Commented Oct 9, 2022 at 11:01
  • 1
    You're welcome. Right, in the loop, it can be included only. I fixed the code. Thank you for the comment. Commented Oct 9, 2022 at 11:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .