-
Notifications
You must be signed in to change notification settings - Fork 24k
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
Impossible to preserve integer type in variables with complex data structure #17992
Comments
@emanation Ansible's underlying template engine is jinja. Jinja's origin as a web templating engine has the characteristic of always returning strings. Prior to 2.x, we always templated values to strings, so the fact that 2.x gives you integers sometimes is an improvement. For example, here's what your testcase outputs in 1.9.6 ...
In 2.x we wrote an "interceptor" of sorts that circumvents sending the variable to jinja if we already know the exact value. If the variable is not "clean", meaning it has a key reference such as "foo.bar" or "foo['bar'", we have to send it to jinja. In that case, we don't know what the original type is and can not preserve it. That is why your staging.count variable becomes a string. To address your primary concern in the description:
That is exactly why parameters to modules should be explicitly declaring the type. We cast expected type in the module argument parser so that the rest of the module code gets what is expected. I wrote a quick module to demonstrate the concept:
I added this task to the end of your example playbook ...
This is the result ...
Having data as a string and passing it to modules that expect other types is -not- a problem, if the data can be cast to the expected type. If a module does not declare the type appropriately and misbehaves, that is a bug with that particular module and should be filed accordingly. |
There is a light at the end of the tunnel. We made a change to Jinja2 so we don't see all variable types changed into strings. See: pallets/jinja#708 |
ISSUE TYPE
COMPONENT NAME
variables handling
ANSIBLE VERSION
CONFIGURATION
OS / ENVIRONMENT
Amazon Linux, MacOS X
SUMMARY
Some modules and APIs require numbers to be integer or float type. Mandatory syntax with quoted variables in YAML has big practical problem. It's impossible to define integers with that.
STEPS TO REPRODUCE
However it works only with straight simple variables but not with complex data structures.
EXPECTED RESULTS
ACTUAL RESULTS
Neither
{{service_vars[service_env].cpu | int}}
filter nor{{service_vars[service_env].ram + 1}}
expression help.However it seems that variable is considered as integer because increment does work but final value is quoted and it's converted to string.
The only way how it may work is to have simple var defined at first level. But then all Environment logic is useless.
Just because of that I have to have similar variable files there just integers are defined explicitly. And that is only difference between var files.
The text was updated successfully, but these errors were encountered: