英文:
How to search for a specific value in a multiple dictionary list, then extract another value from the same list?
问题
以下是您要翻译的代码部分:
After debugging results of a task, I want to be able to get the corresponding uuid just by entering a specific snapshot_name.
This is the output of the "result" variable:
【代码部分省略】
This is my playbook:
【代码部分省略】
Snapname is a variable as I want to be able to be able to enter different snapname values in ansible controller itself, so it is meant to be a changeable variable. Then let ansible search for the snapname value and get its corresponding uuid.
I got this error for the last task in my playbook:
【代码部分省略】
How do I write my playbook in a way that when the value of snapname == "vm snapshot", it will search for "vm snapshot" in "snapshot_name" in all the lists, then get the corresponding uuid?
For example, search which list has "vm_snapshot", then output the uuid, "6f1kc72a-8916-4edd-9639-c2823b57a1cb".
请注意,这是代码的一部分,我已经帮您提取出了要翻译的部分。如果您需要任何其他翻译或有其他问题,请随时告诉我。
英文:
After debugging results of a task, I want to be able to get the corresponding uuid just by entering a specific snapshot_name.
This is the output of the "result" variable:
ok: [localhost] => {
"msg": {
"date": "Mon, 22 May 2023 03:39:34 GMT",
"elapsed": 0,
"expires": "0",
"failed": false,
"json": {
"entities": [
{
"created_time": 1684493611741165,
"deleted": false,
"logical_timestamp": 1,
"snapshot_name": "Snapshot 1",
"uuid": "ffa972f7-25b7-4d0c-8e92-2e7afef863c4"
},
{
"created_time": 1684502215147173,
"deleted": false,
"logical_timestamp": 1,
"snapshot_name": "vm snapshot",
"uuid": "6f1kc72a-8916-4edd-9639-c2823b57a1cb"
},
{
"created_time": 1684725225721634,
"deleted": false,
"logical_timestamp": 1,
"snapshot_name": "new",
"uuid": "ad872bde-3a94-411f-8f89-5f5ad55e4ffa"
}
],
"metadata": {
"grand_total_entities": 3,
"total_entities": 3
}
},
"msg": "OK (unknown bytes)",
"pragma": "no-cache",
"redirected": false,
"server": "envoy"
}
}
<br />
This is my playbook:
- name: debug
debug:
msg: "{{ result }}"
- name: Snapshot name to look for
set_fact:
snapname: "{{ snapname }}"
- name: Getting the uuid of the above snapname
debug:
msg: "{{ result.json.entities[*].uuid }}"
when: result.json.entities[*].snapshot_name == snapname
Snapname is a variable as I want to be able to be able to enter different snapname values in ansible controller itself, so it is meant to be a changeable variable. Then let ansible search for the snapname value and get its corresponding uuid.
I got this error for the last task in my playbook:
fatal: [localhost]: FAILED! => {
"msg": "The conditional check 'result.json.entities[*].snapshot_name == snapname' failed. The error was: template error while templating string: unexpected '*'. String: {% if result.json.entities[*].snapshot_name == snapname %} True {% else %} False {% endif %}\n\nThe error appears to be in '/var/lib/awx/projects/Nutanix/roles/vm_restore/tasks/vm_restore.yml': line 46, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: debug\n ^ here\n"
}
How do I write my playbook in a way that when the value of snapname == "vm snapshot", it will search for "vm snapshot" in "snapshot_name" in all the lists, then get the corresponding uuid?
For example, search which list has "vm_snapshot", then output the uuid, "6f1kc72a-8916-4edd-9639-c2823b57a1cb".
答案1
得分: 1
以下是您请求的部分翻译:
您可以使用 *json_query*
```yaml
- debug:
msg: "{{ result.json.entities|
json_query('[?snapshot_name == `vm snapshot`].uuid') }}"
,或者 selectattr 和 map
- debug:
msg: "{{ result.json.entities|
selectattr('snapshot_name', '==', 'vm snapshot')|
map(attribute='uuid') }}"
两种方法都会产生相同的结果
msg:
- 6f1kc72a-8916-4edd-9639-c2823b57a1cb
下面的代码显示如何替换变量
- debug:
msg: "{{ result.json.entities|json_query(_query) }}"
vars:
snapname: vm snapshot
_query: '[?snapshot_name == `{{ snapname }}`].uuid'
- debug:
msg: "{{ result.json.entities|
selectattr('snapshot_name', '==', snapname)|
map(attribute='uuid') }}"
vars:
snapname: vm snapshot
但是,如果 snapshot_name 的值是唯一的,您可以首先创建一个字典
snapshot_uuid: "{{ result.json.entities|
items2dict(key_name='snapshot_name', value_name='uuid') }}"
会产生
snapshot_uuid:
Snapshot 1: ffa972f7-25b7-4d0c-8e92-2e7afef863c4
new: ad872bde-3a94-411f-8f89-5f5ad55e4ffa
vm snapshot: 6f1kc72a-8916-4edd-9639-c2823b57a1cb
现在搜索变得简单。
测试的完整playbook示例
- hosts: all
vars:
result:
date: Mon, 22 May 2023 03:39:34 GMT
elapsed: 0
expires: '0'
failed: false
json:
entities:
- created_time: 1684493611741165
deleted: false
logical_timestamp: 1
snapshot_name: Snapshot 1
uuid: ffa972f7-25b7-4d0c-8e92-2e7afef863c4
- created_time: 1684502215147173
deleted: false
logical_timestamp: 1
snapshot_name: vm snapshot
uuid: 6f1kc72a-8916-4edd-9639-c2823b57a1cb
- created_time: 1684725225721634
deleted: false
logical_timestamp: 1
snapshot_name: new
uuid: ad872bde-3a94-411f-8f89-5f5ad55e4ffa
metadata:
grand_total_entities: 3
total_entities: 3
msg: OK (unknown bytes)
pragma: no-cache
redirected: false
server: envoy
snapshot_uuid: "{{ result.json.entities|
items2dict(key_name='snapshot_name', value_name='uuid') }}"
tasks:
- debug:
var: snapshot_uuid
- debug:
msg: "{{ result.json.entities|
selectattr('snapshot_name', '==', 'vm snapshot')|
map(attribute='uuid') }}"
- debug:
msg: "{{ result.json.entities|
json_query('[?snapshot_name == `vm snapshot`].uuid') }}"
```
英文:
You can use either json_query
- debug:
msg: "{{ result.json.entities|
json_query('[?snapshot_name == `vm snapshot`].uuid') }}"
, or selectattr and map
- debug:
msg: "{{ result.json.entities|
selectattr('snapshot_name', '==', 'vm snapshot')|
map(attribute='uuid') }}"
Both tasks give the same result
msg:
- 6f1kc72a-8916-4edd-9639-c2823b57a1cb
<hr>
The code below shows how to substitute a variable
- debug:
msg: "{{ result.json.entities|json_query(_query) }}"
vars:
snapname: vm snapshot
_query: '[?snapshot_name == `{{ snapname }}`].uuid'
- debug:
msg: "{{ result.json.entities|
selectattr('snapshot_name', '==', snapname)|
map(attribute='uuid') }}"
vars:
snapname: vm snapshot
<hr>
But, if the values of snapshot_name are unique, you can create a dictionary first
snapshot_uuid: "{{ result.json.entities|
items2dict(key_name='snapshot_name', value_name='uuid') }}"
gives
snapshot_uuid:
Snapshot 1: ffa972f7-25b7-4d0c-8e92-2e7afef863c4
new: ad872bde-3a94-411f-8f89-5f5ad55e4ffa
vm snapshot: 6f1kc72a-8916-4edd-9639-c2823b57a1cb
The search is trivial now.
<hr>
<sup>
Example of a complete playbook for testing
- hosts: all
vars:
result:
date: Mon, 22 May 2023 03:39:34 GMT
elapsed: 0
expires: '0'
failed: false
json:
entities:
- created_time: 1684493611741165
deleted: false
logical_timestamp: 1
snapshot_name: Snapshot 1
uuid: ffa972f7-25b7-4d0c-8e92-2e7afef863c4
- created_time: 1684502215147173
deleted: false
logical_timestamp: 1
snapshot_name: vm snapshot
uuid: 6f1kc72a-8916-4edd-9639-c2823b57a1cb
- created_time: 1684725225721634
deleted: false
logical_timestamp: 1
snapshot_name: new
uuid: ad872bde-3a94-411f-8f89-5f5ad55e4ffa
metadata:
grand_total_entities: 3
total_entities: 3
msg: OK (unknown bytes)
pragma: no-cache
redirected: false
server: envoy
snapshot_uuid: "{{ result.json.entities|
items2dict(key_name='snapshot_name', value_name='uuid') }}"
tasks:
- debug:
var: snapshot_uuid
- debug:
msg: "{{ result.json.entities|
selectattr('snapshot_name', '==', 'vm snapshot')|
map(attribute='uuid') }}"
- debug:
msg: "{{ result.json.entities|
json_query('[?snapshot_name == `vm snapshot`].uuid') }}"
</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论