Maybe if I discuss what I'm trying to do there will be a better solution so that variable scope doesn't matter.
I have an SOA architecture (40 python web services) and want each one defined as their own role because they can be deployed on their own nodes or on shared nodes. So my tree structure looks like this: http://paste.ofcode.org/aWr2A77wxXezkanhWHm5nC For every role I have a line in my group_vars/all file that defines metadata about the role like what nginx port they use, if they use requirements.txt, etc. This looks like this: $ cat group_vars/all.yml --- services: anonweb: repo: anonweb version: upgrade_latest_packages port: 8500 nginx_port: 6005 paths: - / addressbookweb: repo: AddressBookWeb version: develop port: 8765 nginx_port: 6014 paths: - /addressbook <snip> and so if I want to deploy the usersvc role to 3 nodes in the staging environment I do the following: ansible-playbook -i nova.py -u monkey deploy.yml --extra-vars "group=mc3-usersvc role=usersvc" but then after I get them deployed I need to run my loadbalancer role to grab the hosts from my dynamic inventory file and each of the nodes to the load balancer configuration, which means it needs to grab the nginx port and everything that it will be proxying to on each node. All this works as expected because I've defined 1 specific role I want to deploy so the var is globally scoped and can be used every where but if I want to update the load balancer for *all* roles I have no way of getting what the "current" role is, so all my current configuration fails. Which brings us to where we are now, I tried to define role/<role>/vars/ file with one setting role=<role> that way all the configuration would still run. But I forgot to to set this in 3 roles and they ended up getting configuration for the role that ran before them because the variable scope was contained to where I defined it (like it would be in standard python without the global keyword). This caused mayhem because now my load balancer is routing to the wrong nodes and it isn't very clear *why*. I want it to fail and alert me that I forgot to define a variable. ---------------- I understand having the ability to set a variable at a more global scope is important but I think by default it should not. So with my minor example, the pythonapp is generic configuration and one of the checks it does is "when: use_req_text is defined" and each role can decide if it should use a requirements.txt to install. My problem now is that only half of my 40 projects use requirements.txt but since Ansible is leaking the information it tries to use requirements.txt for all of them. Is my only option to have to explicitly set this var in every role and accept the fact that forgetting to do so will have bad consequences? On Tuesday, July 1, 2014 7:52:59 AM UTC-7, Michael DeHaan wrote: > > It doesn't leak information in any way that is a problem, but rather the > variable is still in scope. > > Variables from roles are available in other roles, but always guaranteed > to be used WITHIN that role. > > Thus if you set in one role X, "a: 42" > > and another role Y, "a: 44" > > ansible is so written that role X always gets 42 and tasks in role Y > always get 44. > > They won't clobber one another. > > Having one role being able to define variables for another is however > important, for instance, a role might define presensce in a particular > datacenter and define a server address used in other roles. > > > > > > On Tue, Jul 1, 2014 at 6:17 AM, John Anderson <[email protected] > <javascript:>> wrote: > >> I have the following structure: >> >> roles/pythonapp/ >> roles/billingsvc/ >> roles/usersvc/ >> >> >> pythonapp is a generic set of instructions like cloning and installing a >> python package, billingsvc and usersvc both include it in their tasks. >> >> I have a variable that I set in billingsvc/vars/main.yml and I don't want >> usersvc to see this var but as soon as billingsvc finishes it runs usersvc >> and the var is there. >> >> Is there anyway to prevent this leaking of information? I've defined the >> var in the role vars section specifically to keep it isolated from >> everything else. >> >> -- >> 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 [email protected] <javascript:>. >> To post to this group, send email to [email protected] >> <javascript:>. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/ansible-project/d96b6d38-e618-4c8a-b06a-5843a3cf36df%40googlegroups.com >> >> <https://groups.google.com/d/msgid/ansible-project/d96b6d38-e618-4c8a-b06a-5843a3cf36df%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> For more options, visit https://groups.google.com/d/optout. >> > > -- 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 [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7133b4d5-74e7-48ef-b78a-1dca39e607fa%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
