New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
role variables defined in vars aren't unique #2796
Comments
Roles were never meant to namespace variables, they are an automation around multiple vars_files includes. |
Sure, I guess my initial confusion was around separating roles out; in this case, there's no real benefit from a variable perspective to having an "nginx" role with $version=1.4.0 broken out from a "tomcat" role with $version=7.0.29 as opposed to having a single common role with variables like nginx_version=1.4.0 and tomcat_version=7.0.29; if vars were automatically associated with the appropriate role, then roles would be much more effective being shared between projects, between organizations, etc. Hence my point that from a documentation standpoint, re-affirming that might be useful so that it encourages people to independently namespace their variables (me_nginx__version vs you_tomcat__version) to avoid side effects. |
Yes, you can't name the variable 'version' in each. I don't document everyone's possible confusions as that keeps the docs We do talk about how it is a macro for vars_files, and that doesn't say On Sat, Apr 27, 2013 at 2:00 PM, John Knight notifications@github.comwrote:
|
Just got bitten badly by this. like @knightlabs I find this unintuitive, though I understand that The use case I was attempting is to reuse a task include for different roles, Ended up having an intermediate include file, called from main with a vars key, roleFoo/tasks/main.yml
roleFoo/tasks/secondary.yml
|
y-p – i ended up working around this for multiple roles by simply namespacing the variables as rolename__var in each vars/main.yml It was a little chaotic to start with, but it actually makes the entire system more readable when I know I'm using service1__version v. service2__path |
That does look more readable, but doesn't solve my problem unless that namespacing I wanted to use task includes to reduce code duplication across roles, that means Also, It's difficult to share an include file among roles due to the directory structure, |
I believe this is a major problem because of the increasing probability of variable name collisions and debugging difficulties over time (I got bitten by this one too). Lack of var isolation means that adding a new role (from yourself in the future, or some other team, or from some 3rd party) requires making sure there are not variable name clashes, which is a pain. Please, please make it possible to have role vars that do not affect other role vars. P.S. thank you so much for making Ansible! |
Speaking as someone who has come from Chef and now learning to use Ansible (joined a team that was using it so I must adapt), I'm immediately seeing the importance of the convention to namespace variables. I'm caught off guard by some things:
In Chef cookbooks, you would define your default node attributes in
These settings would usually be sane defaults for whatever package or thing the I'm very happy to find that in Ansible, there seems to be a concept of default settings which can be set inside a role under In Ansible, it seems that the variable scoping rules and namespace conventions are pretty unclear to me at the moment. This is probably just due to insufficient effort in exploring how it works, but my initial intuition is that it feels somewhat loose and not as safe to choose any old variable name in a role or playbook. I feel like I should explore variable scoping a bit more to see if I'm correct. I don't mean to post this to say that this is the only way, or the right way, but speaking as a Chef user who is now also using Ansible... it's something I noticed that I found sadly missing since I was used to using it so extensively in Chef. I just thought I'd throw the idea out there and see what sort of consensus the Ansible community has about this. |
I run a software development company and we use Ansible to create vagrant images and magage AWS resources and I've been happy with it for the most part, but as our projects grow in complexity I'd have to agree that namespacing varialbes would make the playbooks much more manageable and easier to extend. Perhaps I'm misusing Ansible, as we're using it for configuration and a company wide package manager so I'd love to have roles as follows: - {role: ruby, version: 2.2.2}
- {role: php, version: 7} Where version is namespaced to the role. |
Create two roles, for instance "nginx" and "tomcat"; define variable files in each role with a 'version' variable, set to match expected versions of nginx and tomcat; create a playbook which associates both 'nginx' and 'tomcat' roles to a set of hosts.
Depending on order of execution, it appears that ansible overwrites the tomcat:version property with the nginx:version property, and then attempts to install tomcat-{{nginx:version}} which fails.
Either documentation should be updated to reflect the idea that roles maintain variables in a global namespace, or roles should be updated to auto-namespace vars files inside them, such that similarly named vars can co-exist between roles.
The text was updated successfully, but these errors were encountered: