Assigning an integer variable to another variable results in unexpected behavior #55891
Labels
affects_2.9
This issue/PR affects Ansible v2.9
bug
This issue/PR relates to a bug.
support:core
This issue/PR relates to code supported by the Ansible Engineering Team.
SUMMARY
When an integer variable is assigned to another variable using a string template, that variable still behaves internally as an integer, but is displayed as a string, resulting in unexpected behavior.
ISSUE TYPE
COMPONENT NAME
ANSIBLE VERSION
CONFIGURATION
OS / ENVIRONMENT
(not relevant)
STEPS TO REPRODUCE
test.yaml
:Run with:
EXPECTED RESULTS
I would expect
result_1
to equal"84"
and all others to equal"4242"
, as they are all string multiplications.ACTUAL RESULTS
result_3
equals "84", even thoughtest_var_3
is displayed as a string ("42"
). Internally it still behaves as an integer. Although it can be desirable for an integer value to stay as integer when assigned to another variable, the syntax to do so is misleading. Moreover, if an arithmetic operation is done on the initial value when assigning, the resulting value is a string (test_var_4
), and if the internally-integer-value is assigned to another variable, the resulting value is also a string (test_var_5
).As explicit is better than implicit, I think that whenever a string value is assigned to a variable, the resulting value should be a string, whatever the contents of the string. To use that value as an integer, an explicit conversion should be needed:
(some_string_variable | int)
. If it should be possible to assign an integer variable to another variable, while keeping the integer type, then another special syntax (without quotes if possible) is needed.The text was updated successfully, but these errors were encountered: