register variables and use these as role name

61 views
Skip to first unread message

Denis J. Cirulis

unread,
Oct 15, 2014, 2:14:58 AM10/15/14
to ansible...@googlegroups.com
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 ?

Seth Edwards

unread,
Oct 15, 2014, 10:46:13 PM10/15/14
to ansible...@googlegroups.com
 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? 

James Martin

unread,
Oct 16, 2014, 11:28:37 AM10/16/14
to ansible...@googlegroups.com
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.  

Michael Hoglan

unread,
Oct 16, 2014, 12:54:28 PM10/16/14
to ansible...@googlegroups.com
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

Denis J. Cirulis

unread,
Oct 17, 2014, 3:42:00 AM10/17/14
to ansible...@googlegroups.com
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. 



четверг, 16 октября 2014 г., 19:54:28 UTC+3 пользователь Michael Hoglan написал:
Reply all
Reply to author
Forward
0 new messages