adding roles at run time and include_role

21 views
Skip to first unread message

Spike

unread,
Nov 22, 2016, 12:07:15 PM11/22/16
to Ansible Project
Hi there,

first post here so just a quick thank you to everybody who has contributed to ansible, it'a fantastic piece of software.

I'm trying to specify hosts and roles at run time from cli while using a template playbook like this:

# ideal playbook

---
- hosts: "{{var_hosts}}"
  roles:
    - { role: {{item}} }
  with_items: "{{var_roles}}"

# cli call

ansible-playbook play.yml -e "var_hosts=host, var_roles=[role1, role2]"

This however did not work for me, it just seems it's not possible to use with_items on "roles".

Since I'm on 2.2 I thought I may use include_role, so I tried this:
---

- hosts: "{{var_hosts}}"
  tasks:
  - name: include roles
    include_role:
      name: "{{item}}"
    with_items: {{var_roles}}

this however failed with ERROR! 'item' is undefined

I thought I might not be able to use vars there, but if I define a var such as "role" and use it instead of "item" it works:

- hosts: "{{var_hosts}}"
  vars:
    role: test
  tasks:
  - name: include roles
    include_role:
      name: "{{role}}"

am I doing something wrong? is there any way I can pass roles from cli and load them dynamically?

thanks,

Spike
Reply all
Reply to author
Forward
0 new messages