我的json包含以下唯一键列表。
{
"image_names": [
{
"name": "sles-11-factory-os-ready"
},
{
"name": "sles-11-factory-base-vg00"
},
{
"name": "sles-11-factory-base"
},
{
"name": "rhel-7-factory-os-ready"
},
{
"name": "sles-12-factory-os-ready"
},
{
"name": "rhel-6-factory-os-ready"
},
{
"name": "rhel-7-factory-base-vg00"
},
{
"name": "sles-12-factory-base-vg00"
},
{
"name": "rhel-6-factory-base-vg00"
},
{
"name": "rhel-7-factory-base"
},
{
"name": "rhel-6-factory-base"
},
{
"name": "sles-12-factory-base"
}
]
}
我还有另一个带有上述键的json,但是具有不同的值。
{
"rhel-7-factory-os-ready" : [
{
"id": "79334504-3e48-4411-b602-afe920e32e14",
"updated": "2019-05-07T15:51:00Z"
},
{
"id": "79334504-3e48-4411-b602-afe920e32e14",
"updated": "2019-05-07T15:51:00Z"
},
{
"id": "79334504-3e48-4411-b602-afe920e32e14",
"updated": "2019-05-07T15:51:00Z"
}
],
"rhel-6-factory-os-ready" : [
{
"id": "79334504-3e48-4411-b602-afe920e32e14",
"updated": "2019-05-07T15:51:00Z"
},
{
"id": "79334504-3e48-4411-b602-afe920e32e14",
"updated": "2019-05-07T15:51:00Z"
},
{
"id": "79334504-3e48-4411-b602-afe920e32e14",
"updated": "2019-05-07T15:51:00Z"
}
]
}
我正在尝试通过遍历每张图像来按其更新的日期时间值检索最旧的图像。最终,当按updated
图片的rhel-6-factory-base
字段排序时,我期望下面的旧图片数据集
skipping: [localhost] => (item={'updated': u'2019-05-07T15:20:39Z', 'name': u'rhel-6-factory-base', 'id': u'30109a53-4aab-4fb5-87ff-7650e75731b0'})
ok: [localhost] => (item={'updated': u'2019-05-07T12:21:36Z', 'name': u'rhel-6-factory-base', 'id': u'294821b3-e9df-4b12-ae39-8deb8f9c72ff'}) => {
"msg": "The images to be deleted are: 294821b3-e9df-4b12-ae39-8deb8f9c72ff"
}
ok: [localhost] => (item={'updated': u'2019-04-29T14:04:20Z', 'name': u'rhel-6-factory-base', 'id': u'068ae5f0-d7a0-4419-829e-18eb25551c94'}) => {
"msg": "The images to be deleted are: 068ae5f0-d7a0-4419-829e-18eb25551c94"
}
我尝试过下面的方法,但是没有运气!
- name: Print the old images
debug:
msg: "The images to be deleted are: {{ item.id }}"
when:
- item|length > 1
- index != 0
- item.id is defined
# loop: "{{ grouped_by_name['rhel-6-factory-base'] }}"
with_subelements:
- "{{ image_names | map(attribute='name') | list }}"
- grouped_by_name
loop_control:
index_var: index
在上面的代码段中,image_names
和grouped_by_name
var分别是前两个数据集。
如何通过grouped_by_name
var传递的键来进行image_names
的双重循环?
如果问题不清楚,请随时发表评论,我将尝试重新表述。
答案 0 :(得分:1)
如果我正确理解了您的问题,那么实际上您根本不需要嵌套循环。您的目标是为词典中的每个键找到最旧的图像。我们可以这样:
- hosts: localhost
gather_facts: false
vars:
images:
rhel-7-factory-os-ready:
- id: 79334504-3e48-4411-b602-afe920e32e14
updated: '2019-05-04T15:51:00Z'
- id: 79334504-3e48-4411-b602-afe920e32e14
updated: '2019-05-01T15:51:00Z'
- id: 79334504-3e48-4411-b602-afe920e32e14
updated: '2019-05-07T15:51:00Z'
rhel-6-factory-os-ready:
- id: 79334504-3e48-4411-b602-afe920e32e14
updated: '2019-05-09T15:51:00Z'
- id: 79334504-3e48-4411-b602-afe920e32e14
updated: '2019-05-03T15:51:00Z'
- id: 79334504-3e48-4411-b602-afe920e32e14
updated: '2019-05-01T15:51:00Z'
thing-with-a-single-image:
- id: 12345678-1234-1234-1234-123456789123
updated: '2019-05-01T15:51:00Z'
tasks:
- debug:
msg:
image_name: "{{ item.key }}"
image_to_delete: "{{ item.value|sort(attribute='updated')|first }}"
when: item.value|length > 1
loop: "{{ images|dict2items }}"
loop_control:
label: "{{ item.key }}"
然后使用when
语句来确保如果图像是列表中的唯一项,则我们不会将其删除。
这将产生:
TASK [debug] **********************************************************************************
ok: [localhost] => (item=rhel-6-factory-os-ready) => {
"msg": {
"image_name": "rhel-6-factory-os-ready",
"image_to_delete": {
"id": "79334504-3e48-4411-b602-afe920e32e14",
"updated": "2019-05-01T15:51:00Z"
}
}
}
ok: [localhost] => (item=rhel-7-factory-os-ready) => {
"msg": {
"image_name": "rhel-7-factory-os-ready",
"image_to_delete": {
"id": "79334504-3e48-4411-b602-afe920e32e14",
"updated": "2019-05-01T15:51:00Z"
}
}
}
skipping: [localhost] => (item=thing-with-a-single-image)