在 Ansible 中设置变量值时如何使用算术? [英] How to use arithmetic when setting a variable value in Ansible?
问题描述
我想使用主机的系统事实乘以数字/百分比作为变量的基础.我特别想做的是使用 ansible_memtotal_mb
值并将其乘以 0.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屋!