Ansible roles

72 views
Skip to first unread message

Phonthip Namkaew

unread,
May 12, 2015, 2:55:22 PM5/12/15
to ansible...@googlegroups.com
Hi

There are 100 cassandra hosts (cass01.example.com,...,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,...,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,...,cass49.example.com.
Each role has the tasks which are specific to that host:

../roles/common 
         cassandra
         ... 

Inventory file:

(..)
[cassandra]
...
(..)


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

James Cammarata

unread,
May 12, 2015, 5:03:02 PM5/12/15
to ansible...@googlegroups.com
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.

  when: inventory_hostname == "cass40.example.com"
  when: inventory_hostname == "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!


James Cammarata
Director, Ansible Core Engineering
github: jimi-c

--
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.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/3d0b1a11-3150-4ab9-a2e2-597c5f5090f7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages