# Dynamic role list

**URL:** <https://forum.ansible.com/t/dynamic-role-list/14195>\
**Category:** Ansible Project\
**Created:** [December 20, 2013, 2:30pm UTC](https://forum.ansible.com/t/dynamic-role-list/14195 "2013-12-20T14:30:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicolas\_Rodriguez](https://avatars.discourse-cdn.com/v4/letter/n/5e9695/32.png) [@Nicolas\_Rodriguez](https://forum.ansible.com/u/Nicolas_Rodriguez)\
**Post date:** [December 20, 2013, 2:30pm UTC](https://forum.ansible.com/t/dynamic-role-list/14195/1 "2013-12-20T14:30:36Z")

</div>

Hi!

I try to do dynamic role calling in playbook like that :

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [December 20, 2013, 11:32pm UTC](https://forum.ansible.com/t/dynamic-role-list/14195/2 "2013-12-20T23:32:56Z")

</div>

Right now I’m guessing role names aren’t being templated against extra vars

I agree the final one should work with your JSON example, and the first one should work if JMODULES was the name of a single role.

I would file a bug in github to track this… though this is the first time I’ve got the request.

I would generally recommend structuring playbooks to record the desired system configuration, so passing things in and not having them in source control (what hosts get what roles) seems like a bit of an anti-pattern, hence why this is not implemented… but yes, I agree this in principle would be something you might expect to work (however unusual and unexpected).

---

<div class="post-metadata">

**Author:** ![Aaron\_Hunter](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@Aaron\_Hunter](https://forum.ansible.com/u/Aaron_Hunter)\
**Post date:** [December 24, 2013, 10:34pm UTC](https://forum.ansible.com/t/dynamic-role-list/14195/3 "2013-12-24T22:34:06Z")

</div>

In general I agree that this isn’t a great idea. However, I can see a case where one’s infrastructure is more elastic, and shrinks and grows dynamically. VMs have made this case more likely. What do you think about passing a URL for the inventory and main playbook (site.yml) files? If you could pass say, [http://repo.site.com/production.hosts](http://repo.site.com/production.hosts) to the Anisble command line then the files could be generated dynamically by the web server. With that approach you could keep your inventory and roles in an RDBMS and generate the inventory on the fly.

Just an idea…

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [December 24, 2013, 11:13pm UTC](https://forum.ansible.com/t/dynamic-role-list/14195/4 "2013-12-24T23:13:56Z")

</div>

So in that case, you still just have a play that says “for machines in this group, these roles apply”.

This doesn’t imply you would need to template the role names at all.

- hosts: all\_my\_base  
roles:
- are\_belong\_to\_me
- common
- stuff

etc

---

<div class="post-metadata">

**Author:** ![Nicolas\_Rodriguez](https://avatars.discourse-cdn.com/v4/letter/n/5e9695/32.png) [@Nicolas\_Rodriguez](https://forum.ansible.com/u/Nicolas_Rodriguez)\
**Post date:** [December 25, 2013, 9:28am UTC](https://forum.ansible.com/t/dynamic-role-list/14195/5 "2013-12-25T09:28:22Z")

</div>

Actually, I use Ansible to create, delete or update vhosts (apache, nginx, haproxy, php) on the infrastructure and sometimes I only want to talk with servers of a certain role, to update Apache config for example. This avoids executing unneeded tasks.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [December 25, 2013, 5:00pm UTC](https://forum.ansible.com/t/dynamic-role-list/14195/6 "2013-12-25T17:00:14Z")

</div>

Yes, most people would set up different playbooks in that case.

You definitely want a source controlled record of what groups are mapped to what roles and if you just pass in everything with “-e” you’ll not have that.

---

<div class="post-metadata">

**Author:** ![Nicolas\_Rodriguez](https://avatars.discourse-cdn.com/v4/letter/n/5e9695/32.png) [@Nicolas\_Rodriguez](https://forum.ansible.com/u/Nicolas_Rodriguez)\
**Post date:** [December 25, 2013, 5:13pm UTC](https://forum.ansible.com/t/dynamic-role-list/14195/7 "2013-12-25T17:13:24Z")

</div>

> Yes, most people would set up different playbooks in that case.

That’s what I do, I have a playbook for each ‘action’ I want to execute (create, delete, update), and those playbooks include tasks for each part of the infrastructure I want to configure :

Example :

playbook “create”:

- name: create apache vhost  
action : foo

- name: create php vhost  
action : foo

- name: create haproxy vhost  
action : foo

playbook “delete”:

- name: delete apache vhost  
action : foo

- name: delete php vhost  
action : foo

- name: delete haproxy vhost  
action : foo

The mapping “server =\> roles” is stored in database.
