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 ?
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?
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.
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
hosts: localhost
user: root
vars:
role: testrole
var1: 1
var2: 2
roles:
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
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.