Ansible - Can I use arithmetic when setting a vari

2019-03-17 12:41发布

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'

1条回答
对你真心纯属浪费
2楼-- · 2019-03-17 12:48

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

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 }}"
查看更多
登录 后发表回答