Ansible playbook 将字节转换为 GB

问题描述 投票:0回答:1

我相信我会在这里得到解决方案。 我在下面有一个可变卷输出,其中 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'
ansible
1个回答
0
投票

稍微基于如何应用

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
© www.soinside.com 2019 - 2024. All rights reserved.