在Ansible中设置变量值时如何使用算术? [英] How to use arithmetic when setting a variable value in Ansible?
问题描述
我想将系统事实用于主机乘以数字/百分比作为变量的基础.我要专门做的是使用ansible_memtotal_mb
值并将其乘以.80以得到ramsize,然后将其用于设置Couchbase值.我一直在尝试以下行的不同变体.我不确定是否有可能,但是任何帮助都将不胜感激.
I would like to use a system fact for a host times a number/percentage as a base for a variable. What I am trying to do specifically is use the ansible_memtotal_mb
value and multiply it by .80 to get a ramsize to then use in setting a Couchbase value. I have been trying different variations of the line below. I'm not ever sure that it is possible, but any help would be appreciated.
vars:
ramsize: '"{{ ansible_memtotal_mb }}" * .80'
推荐答案
您真的很亲密!我使用计算来设置一些默认的Java内存大小,这与您正在执行的操作类似.这是一个示例:
You're really close! I use calculations to set some default java memory sizes, which is similar to what you are doing. Here's an example:
{{ (ansible_memtotal_mb*0.8-700)|int|abs }}
这说明了两件事-首先,它使用 jinja数学 ,{{ jinja }}
内部的计算也是如此.其次,int
和abs
执行您所期望的操作-确保结果是无符号整数.
That shows a couple of things- first, it's using jinja math, so do the calculations inside the {{ jinja }}
. Second, int
and abs
do what you'd expect- ensure the result is an unsigned integer.
您的情况下,正确的代码应为:
In your case, the correct code would be:
vars:
ramsize: "{{ ansible_memtotal_mb * 0.8 }}"
这篇关于在Ansible中设置变量值时如何使用算术?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!