[英]Calculate percentage used from List variables using Ansible playbook
I'm new to ansible environment.我是 ansible 环境的新手。 I'm working on some coding stuff and got stuck at below.我正在做一些编码工作,但被困在了下面。
I got below output: enter image description here我得到下面的 output:在此处输入图像描述
I'm looking for the output like below for each aggregate in above list.我正在为上面列表中的每个聚合寻找 output,如下所示。 aggr_name: ag_xxxxx aggr_used%: [int] ==> (used/size *100) aggr_name: ag_xxxxx aggr_used%: [int] ==> (used/size *100)
I tried with multiple options i got in web but no luck.我尝试了在 web 中获得的多个选项,但没有运气。 please help me.请帮我。
Thanks.谢谢。
a solution with %
or not: i am using jinja filters float
and round
是否有%
的解决方案:我正在使用 jinja 过滤器float
和round
- name: "tips1"
hosts: localhost
vars:
aggr_capacity:
- aggr_name: ag1
aggr_space:
aggr_available: 123456
aggr_size: 4227703668736
aggr_used: 616585302016
- aggr_name: ag2
aggr_space:
aggr_available: 123456
aggr_size: 1585388896256
aggr_used: 22686273536
- aggr_name: ag3
aggr_space:
aggr_available: 123456
aggr_size: 1585388896256
aggr_used: 119851876352
tasks:
- name: solution set_fact
set_fact:
result: "{{ result | default([]) + [ { 'aggr_name': name, 'used': used, 'pct': pct ~ '%' } ] }}"
loop: "{{ aggr_capacity }}"
vars:
name: "{{ item.aggr_name }}"
used: "{{ (item.aggr_space.aggr_used / item.aggr_space.aggr_size ) | float | round(2)}}"
pct: "{{ (item.aggr_space.aggr_used / item.aggr_space.aggr_size * 100 )| float | round(2)}}"
- name: debug users
debug:
var: result
result:结果:
ok: [localhost] => {
"result": [
{
"aggr_name": "ag1",
"pct": "14.58%",
"used": "0.15"
},
{
"aggr_name": "ag2",
"pct": "1.43%",
"used": "0.01"
},
{
"aggr_name": "ag3",
"pct": "7.56%",
"used": "0.08"
}
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.