我相信我会在这里得到解决方案。 我在下面有一个可变卷输出,其中 vol_size 键值以字节为单位, 如何获得相同输出的 GB 输出。
ok: [localhost] =>
volumes:
cluster1.demo.netapp.com:
- id: cluster1_vol1
svm: svm1
volume_size: '1073741824'
- id: cluster1_vol3
svm: svm1
volume_size: '2147483648'
- id: silas_vol
svm: svm1
volume_size: '10737418240'
- id: engineering
svm: svm1
volume_size: '10737418240'
cluster2.demo.netapp.com:
- id: cluster2_vol1
svm: svm5
volume_size: '1073741824'
- id: cluster2_vol3
svm: svm5
volume_size: '2147483648'
- id: shoreview_vol
svm: svm5
volume_size: '10737418240'
稍微基于如何应用
map
过滤器到Ansible/Jinja2的字典值,一个最小的示例剧本
---
- hosts: localhost
become: false
gather_facts: false
vars:
volumes:
cluster1.demo.netapp.com:
- id: cluster1_vol1
svm: svm1
volume_size: '1073741824'
- id: cluster1_vol3
svm: svm1
volume_size: '2147483648'
- id: silas_vol
svm: svm1
volume_size: '10737418240'
- id: engineering
svm: svm1
volume_size: '10737418240'
cluster2.demo.netapp.com:
- id: cluster2_vol1
svm: svm5
volume_size: '1073741824'
- id: cluster2_vol3
svm: svm5
volume_size: '2147483648'
- id: shoreview_vol
svm: svm5
volume_size: '10737418240'
tasks:
- debug:
msg: "{{ volumes[item] | map(attribute='volume_size') | map('int') | map('human_readable','unit','G') }}"
loop: "{{ volumes.keys() }}"
将产生
的输出TASK [debug] ****************************************
ok: [localhost] => (item=cluster1.demo.netapp.com) =>
msg:
- 1.00 Gb
- 2.00 Gb
- 10.00 Gb
- 10.00 Gb
ok: [localhost] => (item=cluster2.demo.netapp.com) =>
msg:
- 1.00 Gb
- 2.00 Gb
- 10.00 Gb