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
set_fact integer assignment #4170
Comments
templating in Ansible means "produce this as a string" If you need an integer value later in a template you can use " {{ x | int }}" to cast it. This is because the option line is a string, and that's totally fine. Auto-casting seems unwise. |
I just spent quite a while debuging a problem that turned out to be because of this exact issue. I get that the option line is a string, and "guessing" at what type the value should be is a bad idea, but why not at a 'cast' or 'type' parameter so that the user can specify what type of variable it should be cast to? This seems like the best of all worlds. It is worth noting that the current functionality already is inconsistent with the "auto-caster seems unwise" statement. The documentation specifically states: As of 1.8, Ansible will convert boolean strings ('true', 'false', 'yes', 'no') Interestingly, this 1.8 came out 14 months after this issue was closed. Perhaps this should be revisited? |
+1 to revisiting this issue. |
I also believe this issue should be revisited, as it prevents dynamically generated dictionaries from being unmarshalled as JSON as expected. One example: - name: Set cluster bootstrap size (int)
set_fact:
bootstrap_size: '{{ groups["cluster_servers"] | length | int }}'
- name: Enable server mode (bool)
set_fact:
enable_server: '{{ "True" | bool }}'
- name: Collect servers IP (list)
set_fact:
servers_ip: '{{ (servers_ip | default([])) + [ hostvars[item]["ansible_default_ipv4"]["address"] ] }}'
with_items: '{{ groups["cluster_servers"] }}'
- name: Generate opts dictionary
set_fact:
opts_dictionary: '{{ opts_dictionary | default({}) |
combine( { item: hostvars[inventory_hostname][item] } )
}}'
with_items:
- 'bootstrap_size'
- 'enable_server'
- 'servers_ip'
- name: Debug fact
debug:
var: opts_dictionary Debug output:
If booleans, lists and dicts are converted I don't see why integers shouldn't, at least there should be a way to do this explicitly. |
The issue I described above is actually related to #15249, not to the |
Same error here. I add my version of ansible:
|
The following yields an unicode string:
set_fact: ansible_memtotal_mb={{ansible_local.lxc.memory.memtotal_mb|int}}
Perhaps utils.parse_kv() should be modified to auto cast integers, or it should honor the |int filter.
The text was updated successfully, but these errors were encountered: