Ansible - 奇怪的舍入行为
Ansible - strange rounding behaviour
我想计算内存大小并需要舍入功能 - 但我的 ansible 的行为并不像文档声称的那样。这是我的剧本:
- debug:
msg: "need to configure swap for {{ ansible_memtotal_mb }} megabytes -> {{ ansible_memtotal_mb/1024 }} GB -> the file will have {{ ansible_memtotal_mb / 1024.0 | round(2, 'floor') }} gigabytes"
这是我得到的输出:
"msg": "need to configure swap for 1987 megabytes -> 1.9404296875 GB -> the file will have 1.9404296875 gigabytes"
我预计最后一个值为 1.94 GB。为什么圆形过滤器根本不受尊重?
Jan 提供了正确答案:
Probably need to add parantheses: (ansible_memtotal_mb / 1024.0) |
round(2, 'floor'). Otherwise only 1024.0 will get rounded to 2 decimal
digits (which does nothing).
我只是把它放在这里是为了将此问题标记为已回答,以便其他人更容易找到。
我想计算内存大小并需要舍入功能 - 但我的 ansible 的行为并不像文档声称的那样。这是我的剧本:
- debug: msg: "need to configure swap for {{ ansible_memtotal_mb }} megabytes -> {{ ansible_memtotal_mb/1024 }} GB -> the file will have {{ ansible_memtotal_mb / 1024.0 | round(2, 'floor') }} gigabytes"
这是我得到的输出:
"msg": "need to configure swap for 1987 megabytes -> 1.9404296875 GB -> the file will have 1.9404296875 gigabytes"
我预计最后一个值为 1.94 GB。为什么圆形过滤器根本不受尊重?
Jan 提供了正确答案:
Probably need to add parantheses: (ansible_memtotal_mb / 1024.0) | round(2, 'floor'). Otherwise only 1024.0 will get rounded to 2 decimal digits (which does nothing).
我只是把它放在这里是为了将此问题标记为已回答,以便其他人更容易找到。