# register variables and use these as role name

**URL:** <https://forum.ansible.com/t/register-variables-and-use-these-as-role-name/16512>\
**Category:** Ansible Project\
**Created:** [October 15, 2014, 6:14am UTC](https://forum.ansible.com/t/register-variables-and-use-these-as-role-name/16512 "2014-10-15T06:14:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Denis\_J\_Cirulis](https://avatars.discourse-cdn.com/v4/letter/d/ac8455/32.png) [@Denis\_J\_Cirulis](https://forum.ansible.com/u/Denis_J_Cirulis)\
**Post date:** [October 15, 2014, 6:14am UTC](https://forum.ansible.com/t/register-variables-and-use-these-as-role-name/16512/1 "2014-10-15T06:14:58Z")

</div>

Hello,

let’s suppose I have multiple groups of virtual guests, all are split to different environments and are running 2 kinds of applications but with different configurations.  
In /etc/environment there env variables APPENV and APPNAME which contain belonging to env like APPENV=test,APPENV=stage,APPENV=dev,APPNAME=app1,APPNAME=app2 and so on.  
There is one common role ‘common’ where all the provisioning and initial configuration tasks and files are stored (this works good).  
I want to populate roles list in top main.yml playbook with registering these env variables as Ansible vars and use it in

roles:

- { common }
- { $appenv\_$appname }

Is it normal or I do have to think of something different ?

---

<div class="post-metadata">

**Author:** ![Seth\_Edwards](https://avatars.discourse-cdn.com/v4/letter/s/a88e57/32.png) [@Seth\_Edwards](https://forum.ansible.com/u/Seth_Edwards)\
**Post date:** [October 16, 2014, 2:46am UTC](https://forum.ansible.com/t/register-variables-and-use-these-as-role-name/16512/2 "2014-10-16T02:46:13Z")

</div>

I wouldn’t do this. You’re playbooks should be very explicit. Are you just trying to reduce the number of playbooks you have in your directory?

---

<div class="post-metadata">

**Author:** ![James\_Martin1](https://avatars.discourse-cdn.com/v4/letter/j/0ea827/32.png) [@James\_Martin1](https://forum.ansible.com/u/James_Martin1)\
**Post date:** [October 16, 2014, 3:28pm UTC](https://forum.ansible.com/t/register-variables-and-use-these-as-role-name/16512/3 "2014-10-16T15:28:37Z")

</div>

Why create a role that is specific to the environment? And why make it variable? Seems like you’d just use the same role, but give each host group different group inventory variables to override the role defaults.

---

<div class="post-metadata">

**Author:** ![Michael\_Hoglan](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@Michael\_Hoglan](https://forum.ansible.com/u/Michael_Hoglan)\
**Post date:** [October 16, 2014, 4:54pm UTC](https://forum.ansible.com/t/register-variables-and-use-these-as-role-name/16512/4 "2014-10-16T16:54:28Z")

</div>

I had a kind of similar thing I did yesterday, I wanted the ability to use a variable in the role parameters, all the examples online always show static values being based to the role, and show roles being done in one line syntax.

You can specify a role in a longer (not one line) form that allows it.

Playbook

> * * *
> 
> - name: Test Role Vars
> 
> hosts: localhost
> 
> user: root
> 
> vars:
> 
> role: testrole
> 
> var1: 1
> 
> var2: 2
> 
> roles:
> 
> - role: “{{ role }}”
> 
> roleVar1: “{{ var1 }}”
> 
> roleVar2: “{{ var2 }}”

Then have a role

> * * *
> 
> - debug: var=roleVar1
> 
> - debug: var=roleVar2

Then output

> PLAY [Test Role Vars] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> GATHERING FACTS \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> ok: [localhost]
> 
> TASK: [testrole | debug var=roleVar1] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> ok: [localhost] =\> {
> 
> “roleVar1”: “1”
> 
> }
> 
> TASK: [testrole | debug var=roleVar2] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> ok: [localhost] =\> {
> 
> “roleVar2”: “2”
> 
> }
> 
> PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> localhost : ok=3 changed=0 unreachable=0 failed=0

Enjoy 🙂

Thanks!  
Michael

---

<div class="post-metadata">

**Author:** ![Denis\_J\_Cirulis](https://avatars.discourse-cdn.com/v4/letter/d/ac8455/32.png) [@Denis\_J\_Cirulis](https://forum.ansible.com/u/Denis_J_Cirulis)\
**Post date:** [October 17, 2014, 7:42am UTC](https://forum.ansible.com/t/register-variables-and-use-these-as-role-name/16512/5 "2014-10-17T07:42:00Z")

</div>

Thank you all for suggestions, I’ve played a bit with Michaels playbook and considered it legit.  
Tried to simulate all possible bad configurations and deceided to go with more explicit playbooks.
