Search code examples
ansiblerounding

Ansible - strange rounding behaviour


I want to calculate memory sizes and need rounding functionality - but my ansible does not behave as documentation claims it should. This is my playbook:

  - 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"

And this is the output I get:

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

I'd expect the last value to be 1.94 GB. Why is the round filter not respected at all?


Solution

  • Jan provided the correct answer:

    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).

    I just put it here to mark this question as answered and thus for others easier to find.