【问题标题】:Capture size with ansible_facts使用 ansible_facts 捕获大小
【发布时间】:2020-02-24 17:35:48
【问题描述】:

我想滚动浏览这个 ansible_facts 变量的输出,直到找到磁盘大小值 C:\ 使用 ansible 属性选择,有人可以帮助我吗?

似乎字符串中的某些内容正在转义并在搜索中产生错误

退出 ansible_fact:

ok: [192.168.56.103] => {
    "ansible_facts.disks": [
        {
            "bootable": true,
            "bus_type": "SATA",
            "clustered": false,
            "firmware_version": "1.0",
            "friendly_name": "VBOX HARDDISK",
            "guid": null,
            "location": "Integrated : Adapter 0 : Port 0",
            "manufacturer": null,
            "model": "VBOX HARDDISK",
            "number": 0,
            "operational_status": "Online",
            "partition_count": 2,
            "partition_style": "MBR",
            "partitions": [
                {
                    "access_paths": [
                        "C:\\",
                        "\\\\?\\Volume{e98535da-0000-0000-0000-501f00000000}\\"
                    ],
                    "active": false,
                    "drive_letter": "C",
                    "guid": null,
                    "hidden": false,
                    "mbr_type": 7,
                    "number": 2,
                    "offset": 525336576,
                    "shadow_copy": false,
                    "size": 53160706048,
                    "transition_state": 1,
                    "type": "IFS"
        }
    ]
}

我的尝试

test : '{{ ansible_facts.disks | selectattr("partitions.drive_letter", "search", "^C$")| map(attribute="size") | list }}'

输出

"VARIABLE IS NOT DEFINED!"

Examples

【问题讨论】:

    标签: python ansible ansible-facts


    【解决方案1】:

    我不知道selectattr 过滤器,但我用json_query 管理它:

      - set_fact:
          c_size: "{{ ansible_facts.disks | json_query( partition_query ) }}"
        vars:
          partition_query: "[*].partitions[?drive_letter=='C'].size"
    
      - debug:
          var: c_size
    
      - debug:
          msg: "{{ c_size }}"
    

    给出这个输出:

    TASK [debug] *************************************************************
    ok: [192.168.124.8] => 
      c_size:
      - - 53160706048
    
    TASK [debug] *************************************************************
    ok: [192.168.124.8] => 
      msg: ':[[53160706048]]:'
    

    看起来它仍然是原始列表中嵌入的几个级别,但它就在那里。你可以通过使用c_size[0][0]c_size.0.0 来摆脱它。当然,您可以使用另一个set_factc_size.0.0 分配给另一个变量。

    【讨论】:

    • 我刚刚发现您可以像这样退出列表:c_size: "{{ ansible_facts.disks | json_query( partition_query ) | first | first }}",而不是 c_size.0.0
    猜你喜欢
    • 2011-08-25
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-15
    • 1970-01-01
    • 2016-01-07
    相关资源
    最近更新 更多