Roles list variable

1,212 views
Skip to first unread message

Marc Trudel

unread,
Nov 16, 2013, 11:06:16 AM11/16/13
to ansible...@googlegroups.com
Hi,

For one of the playbook I am trying to write, I am trying to allow the input of a dynamic role list.

However, this does not seem to work:

- name: "Apply extra roles (see .myfile.yml)"
  hosts: all
  gather_facts: true
  sudo: true
  user: vagrant
  roles: extra_roles

Does it make sense? Is it supported? Or should I try to do this differently?

Michael DeHaan

unread,
Nov 16, 2013, 1:10:56 PM11/16/13
to ansible...@googlegroups.com
I'm not sure where you expect ".myfile.yml" to be loaded.

Can you elaborate?



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Marc Trudel

unread,
Nov 17, 2013, 2:32:53 AM11/17/13
to ansible...@googlegroups.com
Its a bit tricky to explain... at any rate, my question really is about using a variable as a role list. Is this doable/logical/stupid?

Michael DeHaan

unread,
Nov 17, 2013, 1:40:21 PM11/17/13
to ansible...@googlegroups.com
You can achieve this something like this if you .... really ... want to.

- hosts: all
  tasks:
     -  group_by: key=should-foo-{{ 'foo' in my_roles }}
     -  group_by: key=should-bar-{{ 'bar' in my_roles }}

- hosts: should-foo-True
  roles:
     - foo

- hosts: should-bar-True
  roles:
     - bar

Otherwise you can't because plays affect all hosts and the variables for different hosts would be different.

It's better to leave playbooks as the concept that map hosts to roles and keep it simple.




Evan Kaufman

unread,
Jul 26, 2014, 2:09:37 PM7/26/14
to ansible...@googlegroups.com
I'm trying to accomplish something similar, where I'd have a list of roles defined in the group_vars/all file (where I'd disable certain roles by just commenting them out):

---
install_roles
:
 
- apache
#- nginx
 
- php
 
- mysql
#- mariadb
 
- varnish

And then use that list variable in the provision.yml file:

---
- hosts: local
  gather_facts
: yes

  roles
: install_roles # first try
  roles
: "{{ install_roles }}" # second try
  roles
: { with_items: install_roles } # third try

However, all the variations above fail with "ERROR: value of 'roles:' must be a list", so I'm guessing using a list var for roles is not supported?

Michael DeHaan

unread,
Jul 26, 2014, 3:57:17 PM7/26/14
to ansible...@googlegroups.com
Roles must be a list, but the value of roles is not templated.

It expects this to be in the playbook.

It wouldn't work with inventory variables anyway, so there's not much point.

Inventory variables happen after tasks start getting parsed.




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Mouncif Benziane

unread,
Jun 15, 2017, 7:18:10 PM6/15/17
to Ansible Project
Any solutions for this? I am trying to do the same but no luck!
Reply all
Reply to author
Forward
0 new messages