我有一個 API 呼叫會輸出以下 JSON 資料:
"data": [
{
"vrfId": "1",
"name": "test",
"rd": null,
"description": null,
"sections": "3",
"editDate": null,
"customer_id": "1"
},
{
"vrfId": "2",
"name": "vrf-XYZ-01",
"rd": null,
"description": "test vrf for XYZ",
"sections": "3;4",
"editDate": "2022-06-28 13:03:47",
"customer_id": null
},
{
"vrfId": "3",
"name": "vrf-ABC-01",
"rd": null,
"description": "test vrf for ABC ",
"sections": "3;4",
"editDate": "2022-06-28 13:04:03",
"customer_id": null
},
{
"vrfId": "4",
"name": "vrf-KLM-01",
"rd": null,
"description": "test vrf for KLIM ",
"sections": "3;4",
"editDate": null,
"customer_id": null
}
]
我想用 Ansible 任務遍歷它,然后搜索名稱值中是否有 3 個字母代碼(ABC、KLM、XYZ ..),并保存相應的 vrfId
為了嘗試除錯任務,我做了以下操作:
- name: debug vrfs with tenant name
debug:
msg: "{{ item }}"
with_items: "{{ vrf.json.data }}"
when: "{{ tenant }}" in "{{ item.name }}"
其中 vrf.json.data 是上面的 JSON 資料。變數租戶是搜索字串,在本例中為 XYZ。這給了我以下錯誤:
Syntax Error while loading YAML.
did not find expected key
The error appears to be in '/runner/project/playbooks/nsx-t/roles/ipam_modify_subnet/tasks/main.yml': line 27, column 24, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
with_items: "{{ vrf.json.data }}"
when: "{{ tenant }}" in "{{ item.name }}"
^ here
我
uj5u.com熱心網友回復:
首先,您的when
陳述句中有語法錯誤:when
陳述句是 jinja 運算式,這意味著您從不使用{{...}}
模板標記。您希望您的任務如下所示:
- name: debug vrfs with tenant name
debug:
msg: "{{ item }}"
with_items: "{{ vrf.json.data }}"
when: tenant in item.name
假設您的樣本資料在vrf
變數中可用,我們可以使用以下劇本測驗該更改:樣本資料:
- hosts: localhost
gather_facts: false
vars:
tenant: XYZ
tasks:
- debug:
msg: "{{ item }}"
loop: "{{ vrf.json.data }}"
when: tenant in item.name
運行此輸出:
TASK [debug] ******************************************************************************************************************************************************************************************************
skipping: [localhost] => (item={'vrfId': '1', 'name': 'test', 'rd': None, 'description': None, 'sections': '3', 'editDate': None, 'customer_id': '1'})
ok: [localhost] => (item={'vrfId': '2', 'name': 'vrf-XYZ-01', 'rd': None, 'description': 'test vrf for XYZ', 'sections': '3;4', 'editDate': '2022-06-28 13:03:47', 'customer_id': None}) => {
"msg": {
"customer_id": null,
"description": "test vrf for XYZ",
"editDate": "2022-06-28 13:03:47",
"name": "vrf-XYZ-01",
"rd": null,
"sections": "3;4",
"vrfId": "2"
}
}
skipping: [localhost] => (item={'vrfId': '3', 'name': 'vrf-ABC-01', 'rd': None, 'description': 'test vrf for ABC ', 'sections': '3;4', 'editDate': '2022-06-28 13:04:03', 'customer_id': None})
skipping: [localhost] => (item={'vrfId': '4', 'name': 'vrf-KLM-01', 'rd': None, 'description': 'test vrf for KLIM ', 'sections': '3;4', 'editDate': None, 'customer_id': None})
請注意,您還可以使用 Ansible 過濾器從資料中提取所需的記錄。例如,使用json_query
過濾器對您的資料執行jmespath查詢:
- debug:
msg: "{{ vrf.json.data|json_query('[?contains(name, `XYZ`)]') }}"
這輸出:
TASK [debug] ********************************************************************************************
ok: [localhost] => {
"msg": [
{
"customer_id": null,
"description": "test vrf for XYZ",
"editDate": "2022-06-28 13:03:47",
"name": "vrf-XYZ-01",
"rd": null,
"sections": "3;4",
"vrfId": "2"
}
]
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/497242.html
上一篇:序列化后接收雙引號