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
with_sequence should provide int (not unicode) values #17852
Comments
This should do what you want:
Im not sure sequence should return an int as the most common use case is to create cloud servers and append the sequence to the name:
|
Yes, that works, thank you. Since the documentation section is headed "Looping over Integer Sequences" I was surprised to get strings rather than integers. You provide this use case:
This could alternatively be done as:
which actually is the approach used in the documentation example:
...rather than However: you probably don't want to break users of the first case. Also I notice the Maybe just clarify in the documentation that the item values are always provided as strings, and you can use the (P.S. For |
Interesting discussion. Its tricky to know where to document this exactly. I believe its really jinja2 that 'wants' to output strings by default, so this is going to be true throught playbooks and templates. I guess in the context of jinja2's origins (as a templating engine for generating html) it makes sense to output strings by default. I hit something like your issue a little while back and in the end settled on using |
Only in the sense that What I'm actually doing is building IP addresses: e.g. I use arithmetic inside a jinja2 expansion quite a lot.
I think this behaviour is specific to |
If you want to submit a pull request we'll be happy to review it |
This may not be specific to with_sequence. I'm doing an assignment out of another variable (and thus invoking jinja2), and it is similarly giving me a string when I am expecting an integer. The workaround of using |int on the arithmetic also works here. Here's my test playbook, showing that the arithmetic works on a simple direct-assigned variable, but fails when the variable is assigned via jinja2: [james.juran@localhost ansible-tests]$ cat datacenter.yml
[james.juran@localhost ansible-tests]$ ansible-playbook --version PLAY [Datacenter Variable Test] ************************************************************************************************************************************************** TASK [debug] ********************************************************************************************************************************************************************* TASK [debug] ********************************************************************************************************************************************************************* TASK [debug] ********************************************************************************************************************************************************************* TASK [debug] ********************************************************************************************************************************************************************* TASK [debug] ********************************************************************************************************************************************************************* I expected the last statement to also produce "101". |
(Mentioning #17992 here to keep track of related issues) |
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 |
* Clarify return value of the sequence lookup Fixes #17852 * Mention range filter
ISSUE TYPE
COMPONENT NAME
with_sequence
ANSIBLE VERSION
CONFIGURATION
No changes
OS / ENVIRONMENT
N/A
SUMMARY
with_sequence
sets item to a series of unicode values instead of integer values. This makes it impossible to do arithmetic with them.STEPS TO REPRODUCE
This fails:
This works:
EXPECTED RESULTS
"hello 11" to "hello 19"
ACTUAL RESULTS
But using
with_items
instead ofwith_sequence
it's fine:The text was updated successfully, but these errors were encountered: