问题描述
尝试使用json_query
来过滤某些外部命令(nvme list -o json
)的JSON输出时,我真是一团糟。
nvme
的输出是通过Ansible捕获的:
- name: Run nvme command
command:
cmd: nvme list -o json
register: nvme_output
become: yes
包含:
- name: nvme_ouput stdout
debug:
var: nvme_output.stdout
TASK [general : nvme_ouput stdout] *********************************************
ok: [dev-0600099bfee3c0f60.internal] => {
"nvme_output.stdout": {
"Devices": [
{
"DevicePath": "/dev/nvme0n1","Firmware": "1.0","Index": 0,"MaximumLBA": 33554432,"ModelNumber": "Amazon Elastic Block Store","PhysicalSize": 17179869184,"ProductName": "Non-Volatile memory controller: vendor 0x1d0f Device 0x8061","SectorSize": 512,"SerialNumber": "vol0e6af89fd1991cwtf","UsedBytes": 0
},{
"DevicePath": "/dev/nvme1n1","Index": 1,"MaximumLBA": 104857600,"PhysicalSize": 53687091200,"SerialNumber": "vol0885625da4a60awtf","UsedBytes": 0
}
]
}
}
我现在需要所有DevicePath
。使用在线测试仪,例如。 https://jmespath.org/,我想到了一个非常简单的查询:Devices[*].DevicePath
尝试在Ansible中使用它:
- name: nvme_ouput json_query var
debug:
msg: "{{ nvme_output.stdout | json_query(jq) }}"
vars:
jq: >-
Devices[*].DevicePath
但是...
TASK [general : nvme_ouput json_query var] *********************
ok: [dev-0600099bfee3c0f60.internal] => {
"msg": ""
}
为什么json_query()
的输出为空?
我正在Debian 10系统上的AWS EC2上运行此命令,
ansible-playbook 2.7.7
config file = /home/alex/sysops-ansible/ansible.cfg
configured module search path = ['/home/alex/.ansible/plugins/modules','/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
executable location = /usr/bin/ansible-playbook
python version = 3.7.3 (default,Jul 25 2020,13:03:44) [GCC 8.3.0]
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)