How do I round a value in Jinja?

2,195 views
Skip to first unread message

Mikael Sandström

unread,
Aug 30, 2014, 1:30:41 PM8/30/14
to ansible...@googlegroups.com
Hi,

I've got a template which does a few calculations on memory (to set up hugepages etc), and I need to round the result but I'm not sure how to.

This is the expression:


"{{ (((60/100)* ansible_memtotal_mb)*1024)/2048  }}"




I usually end up with a decimal, but I need to get rid of the decimal:

Result: 600.6 and I'd like to round that to 601

Result: 1201.1 should be rounded to 1201
etc

Any idea on how  to do that?

Thanks!
/M

Matt Martz

unread,
Aug 30, 2014, 1:41:08 PM8/30/14
to ansible...@googlegroups.com
You are looking for the aptly named "round" filter:


some_val|round
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7a9b2161-46d2-4484-b384-3ad52438065f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
ma...@sivel.net
http://sivel.net/

Mikael Sandström

unread,
Aug 30, 2014, 2:00:28 PM8/30/14
to ansible...@googlegroups.com
Doh!

Awesome, that did the trick.

someval |round|int

Thanks!
/M


On Saturday, August 30, 2014 7:41:08 PM UTC+2, Matt Martz wrote:
You are looking for the aptly named "round" filter:


some_val|round


On Saturday, August 30, 2014, Mikael Sandström <ora...@gmail.com> wrote:
Hi,

I've got a template which does a few calculations on memory (to set up hugepages etc), and I need to round the result but I'm not sure how to.

This is the expression:


"{{ (((60/100)* ansible_memtotal_mb)*1024)/2048  }}"




I usually end up with a decimal, but I need to get rid of the decimal:

Result: 600.6 and I'd like to round that to 601

Result: 1201.1 should be rounded to 1201
etc

Any idea on how  to do that?

Thanks!
/M

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.

Dick Davies

unread,
Sep 3, 2014, 3:48:53 AM9/3/14
to ansible list
Have a look at the '//' operator :
http://jinja.pocoo.org/docs/dev/templates/#math

- it rounds down, but saves the extra step.

On 30 August 2014 18:30, Mikael Sandström <ora...@gmail.com> wrote:

> This is the expression:
>
>
> "{{ (((60/100)* ansible_memtotal_mb)*1024)/2048 }}"

> I usually end up with a decimal, but I need to get rid of the decimal:
>
> Result: 600.6 and I'd like to round that to 601
>
> Result: 1201.1 should be rounded to 1201
> etc
>
> Any idea on how to do that?
>
> Thanks!
> /M
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.

Bohdan Sydor

unread,
Sep 3, 2014, 7:32:49 AM9/3/14
to ansible...@googlegroups.com
On Wed, Sep 3, 2014 at 9:48 AM, Dick Davies <di...@hellooperator.net> wrote:
> On 30 August 2014 18:30, Mikael Sandström <ora...@gmail.com> wrote:
>> "{{ (((60/100)* ansible_memtotal_mb)*1024)/2048 }}"
>> Result: 600.6 and I'd like to round that to 601
>>
>> Result: 1201.1 should be rounded to 1201
>> Any idea on how to do that?

Hi,

Please take a look at this example:

innodb_buffer_pool_size = {{
(0.85*ansible_memtotal_mb/1024)|int }}G

As a result I get:
innodb_buffer_pool_size = 120G

--
Best,
b

Mikael Sandström

unread,
Sep 3, 2014, 7:37:05 AM9/3/14
to ansible...@googlegroups.com
Hi,

Yeah it's already sorted, but thanks!

/M
Reply all
Reply to author
Forward
0 new messages