Dynamic role list

Hi!

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

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).

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 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…

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

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.

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.

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.