[
{
"Hostname": "cavia",
"Farm": "paolo",
"Cluster": "paperino",
"Gateway": "10.14.35.1",
"Dns": "172.26.5.110,172.26.5.111,172.26.16.11,172.26.16.12",
"Routes": "0",
"Network": [
{
"MAC":"00:50:56:b6:c0:db",
"Vlan":"107 - 10.14.8.0/24 - BE WW TF",
"Scope": "Production",
"IP": "10.14.35.9",
"MASK": "255.255.255.224"
},
{
"MAC":"50:56:b6:19:0c",
"Vlan":"5 - 10.4.81.0/24 - BE WW TF",
"Scope": "BE",
"IP": "10.4.5.9",
"MASK": "255.255.255.224"
},
{
"MAC":"00:50:56:b6:19:aa",
"Vlan":"4 - 0.14.81.0/24 - BE WW TF",
"Scope": "NFS",
"IP": "172.10.0.5",
"MASK": "255.255.0.0"
},
{
"MAC":"00:50:b6:19:0c",
"Vlan":"10087 - 10.14.81.0/24 - BE WW TF",
"Scope": "Backup",
"IP": "10.4.96.28",
"MASK": "255.255.254.0"
}
],
"Disk": [
{
"ID": "36000c29ccb2f18976786181535e88772",
"Scope": "New",
"DiskFs": "/prova"
},
{
"ID": "36000c29ccb2f18976786181535e86553",
"Scope": "New",
"DiskFs": "/pippo"
}
]
}
]
这是我在剧本中写的:
---
- name: "Phase 4 : Filesystem Configuration"
hosts: just_parsed
vars:
data: "{{ lookup('file', '../data/data.json') }}"
gather_facts: true
tasks:
- set_fact:
disklen: "{{ data[0].Disk | length }}"
- debug:
msg: "disklen length is {{ disklen }}"
- name: Collecting mapped disks
set_fact:
localdisks: "{{ ansible_devices.keys() | select('match','sd+') | list }}"
- debug:
var: localdisks
我需要:
1)在ansible_devices.<disk>.links.ids.scsi-<id>
上查找Disk.ID。例如,从ansible_devices中获取sdb磁盘:
"sdb": {
"holders": [],
"host": "Serial Attached SCSI controller: VMware PVSCSI SCSI Controller (rev 02)",
"links": {
"ids": [
"scsi-36000c293b2abdd05935792f86fa53bb9",
"wwn-0x6000c293b2abdd05935792f86fa53bb9"
],
"labels": [],
"masters": [],
"uuids": []
},
"model": "Virtual disk",
"partitions": {
"sdb1": {
"holders": [
"vgvar-lvvar"
],
"links": {
"ids": [
"lvm-pv-uuid-lOyio6-WTDV-vcQx-CE2h-TbZk-zsH0-HuMqIc",
"scsi-36000c293b2abdd05935792f86fa53bb9-part1",
"wwn-0x6000c293b2abdd05935792f86fa53bb9-part1"
],
"labels": [],
"masters": [
"dm-3"
],
"uuids": []
},
"sectors": "20971519",
"sectorsize": 512,
"size": "10.00 GB",
"start": "1",
"uuid": null
}
}
我应该看看json文件中的ID之一是否与ansible_devices.sdb.links.ids.scsi-36000c293b2abdd05935792f86fa53bb9
匹配,但是它们是2个循环,一个来自ID,一个来自ansible_devices。
您是否有建议比较这些元素?
谢谢!!
答案 0 :(得分:0)
下面的任务可能会完成这项工作(假设JSON数据存储在 hw_info 中)。
- debug:
msg: "{{ item.0.dev }} mounted to device {{ item.1.dev }}"
loop: "{{ hw_info.0.Disk|
json_query('[].{dev: DiskFs, id: ID}')|
product(
ansible_facts.devices|dict2items|
json_query('[].{dev: key, ids: value.links.ids}'))|
list }}"
when: item.1.ids|map('search', item.0.id) is any
要调试过滤器,请查看product过滤器中使用的列表。
- debug:
var: item
loop: "{{ hw_info.0.Disk|
json_query('[].{dev: DiskFs, id: ID}') }}"
- debug:
var: item
loop: "{{ ansible_facts.devices|dict2items|
json_query('[].{dev: key, ids: value.links.ids}') }}"
Q:仅当范围为“ New”时,才应执行此操作
A:将scope
项目添加到字典中,并在何时条件下使用它。例如
- debug:
msg: "{{ item.0.dev }} mounted to device {{ item.1.dev }}"
loop: "{{ hw_info.0.Disk|
json_query('[].{dev: DiskFs, id: ID, scope: Scope}')|
product(
ansible_facts.devices|dict2items|
json_query('[].{dev: key, ids: value.links.ids}'))|
list }}"
when:
- item.1.ids|map('search', item.0.id) is any
- item.0.scope == "New"