Ansible - 奇怪的舍入行为

问题描述 投票:0回答:2

我想计算内存大小并需要舍入功能 - 但我的 ansible 的行为并不像文档声称的那样。这是我的剧本:

 - 调试:
      msg:“需要配置 {{ ansible_memtotal_mb }} MB -> {{ ansible_memtotal_mb/1024 }} GB -> 文件将有 {{ ansible_memtotal_mb / 1024.0 | round(2, 'floor') }} GB”

这是我得到的输出:

"msg": "need to configure swap for 1987 megabytes -> 1.9404296875 GB -> the file will have 1.9404296875 gigabytes"

我预计最后一个值为 1.94 GB。为什么圆形过滤器根本不受尊重?

ansible rounding
2个回答
2
投票

Jan 提供了正确答案:

可能需要添加括号:(ansible_memtotal_mb / 1024.0) | 圆(2,'地板')。否则只有 1024.0 会四舍五入到小数点后 2 位 数字(什么也不做)。

我只是把它放在这里是为了将此问题标记为已回答,以便其他人更容易找到。


0
投票

Round 接受两个参数。第一个参数是精度,第二个参数是要使用的舍入方法。 当不指定参数时,精度为0,方法很常见,就像这样。

[jzhang@centos9 playbooks]$ ansible localhost -m debug -a "msg={{ (123546 / 1024.0) | round(0, 'common') }}" 本地主机 |成功=> { “味精”:“121.0” }

精度参数告诉舍入要舍入多少位小数。第二个参数是舍入方法,可以是普通(向上或向下舍入)、ceil(总是向上舍入) 或地板(始终向下舍入)。

[jzhang@centos9 playbooks]$ ansible localhost -m debug -a "msg={{ (123546 / 1024.0) | round(2, 'floor') }}" 本地主机 |成功=> { “味精”:“120.65” } [jzhang@centos9 playbooks]$ ansible localhost -m debug -a "msg={{ (123546 / 1024.0) | round(2, 'ceil') }}" 本地主机 |成功=> { “味精”:“120.66” } [jzhang@centos9 playbooks]$ ansible localhost -m debug -a "msg={{ (123546 / 1024.0) | round(2, 'common') }}" 本地主机 |成功=> { “味精”:“120.65” }

© www.soinside.com 2019 - 2024. All rights reserved.