使用 ansible_facts 捕获大小
Capture size with ansible_facts
我想滚动浏览此 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!"
我不知道 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_fact
将c_size.0.0
赋值给另一个变量。
我想滚动浏览此 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!"
我不知道 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_fact
将c_size.0.0
赋值给另一个变量。