# Ansible roles

**URL:** <https://forum.ansible.com/t/ansible-roles/18214>\
**Category:** Ansible Project\
**Created:** [May 12, 2015, 6:55pm UTC](https://forum.ansible.com/t/ansible-roles/18214 "2015-05-12T18:55:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phonthip\_Namkaew](https://avatars.discourse-cdn.com/v4/letter/p/eada6e/32.png) [@Phonthip\_Namkaew](https://forum.ansible.com/u/Phonthip_Namkaew)\
**Post date:** [May 12, 2015, 6:55pm UTC](https://forum.ansible.com/t/ansible-roles/18214/1 "2015-05-12T18:55:22Z")

</div>

Hi

There are 100 cassandra hosts ([cass01.example.com](http://cass01.example.com),…,[cass100.example.com](http://cass100.example.com))  
All shall have the same role common and the same role cassandra.  
In addition, 10 out of the 100 cassandra hosts do have additional host-specific configurations  
and to make things worse, these configurations differ across the 10 hosts. The 10 cassandra hosts shall be  
[cass40.example.com](http://cass40.example.com),…,[cass49.example.com](http://cass49.example.com). Finally, the additional host-specific configurations  
should be applied after the role common and role cassandra.

To solve this problem, I created a role for each [cass40.example.com](http://cass40.example.com),…,[cass49.example.com](http://cass49.example.com).  
Each role has the tasks which are specific to that host:

…/roles/common  
cassandra  
[cass40.example.com/tasks/main.yml](http://cass40.example.com/tasks/main.yml)  
…  
[cass49.example.com/tasks/main.yml](http://cass49.example.com/tasks/main.yml)

Inventory file:

(…)  
[cassandra]  
[cass01.example.com](http://cass01.example.com)  
…  
[cass100.example.com](http://cass100.example.com)  
(…)

The playbook has the two roles common and cassandra and an additional third role called “{{ inventory\_hostname }}”  
when a host has a defined variable "has\_own\_role " in a file …/host\_vars/cassxx.example.com. The variable  
“{{ inventory\_hostname }}” is equal to the FQDN written in the inventory file.

- hosts: [cassandra}  
user: ansible  
sudo: yes  
roles:
- common
- cassandra
- { role: “{{ inventory\_hostname }}” , when: has\_own\_role is defined }

The files in …/host\_vars/… are:

cat …/host\_vars/cass40.example.com  
has\_own\_role: True  
…  
cat …/host\_vars/cass49.example.com  
has\_own\_role: True

When the playbook is run, ansilbe complains that there is no role {{ inventory\_hostname }}:

…  
ERROR: cannot find role in /data/ansible/roles/{{ inventory\_hostname }} or /data/ansible/{{ inventory\_hostname }} or /etc/ansible/roles/{{ inventory\_hostname }}  
…

However, if I hard code a fix role (foo), the playbook works as intended :

…

- { role: foo , when: has\_own\_role is defined }  
…

Queston 1:  
Is there a way to provide a role name as a variable in the following line instead of a fixed string (foo) ?

…

- { role: foo , when: has\_own\_role is defined }  
…

Question 2;  
In general, I do not know how to deal with the following (common) situation in Ansible:  
There is a bunch of equal hosts (cassandra) in the data center, but a few of them a some random additional configurations

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [May 12, 2015, 9:02pm UTC](https://forum.ansible.com/t/ansible-roles/18214/2 "2015-05-12T21:02:59Z")

</div>

Hi Phonthip,

Roles are read in at YAML parsing time, so inventory variables as you’re using here are not yet available. Unfortunately the only way to do this in 1.9 and lower would be to create a new role and use “include:” for each given hostname.

- include: [cass40.example.com](http://cass40.example.com)  
when: inventory\_hostname == “[cass40.example.com](http://cass40.example.com)”
- include: [cass41.example.com](http://cass41.example.com)  
when: inventory\_hostname == “[cass41.example.com](http://cass41.example.com)”  
…

In the future (2.0), this will be much easier, as you could do this:

- include: {{inventory\_hostname}}.yml  
when: has\_own\_role is defined

And then each hosts YAML file would be in the tasks/ directory of your special role.

Hope that helps!
