有下面的 Json。如何提取某个值,例如名称,即 VLAN 或值 (12321321412a-10)。尝试过使用
- name: Read data from Json
set_fact:
Value: "{{ lookup('file', 'POST.json') | from_json }}"
- name: Get the value from JSON
debug:
msg: "The value is {{ value.value }}"
我得到以下输出
TASK [Get the value from JSON] ************************
致命:失败! => {“msg”:“该任务包含一个带有未定义变量的选项。错误是:'值'未定义
示例 Json
{
"fulfilmentOrderItems": [
{
"customerDates": {
},
"instanceCharacteristics": [
{
"name": "VLAN",
"value": "24",
"action": "Cancel"
}
],
"orderLineId": {
"value": "123456-10",
"source": "AS"
}
}
],
"orderId": {
"identifier": {
"value": "1234567-96_11",
"source": "AS"
},
"version": 1
}
}
update_fact
模块 – 更新当前设置的事实来创建其他数据结构。
一个最小的示例手册
---
- hosts: localhost
become: false
gather_facts: false
tasks:
- include_vars:
file: sample.json
name: readFromFile
- debug:
msg: "{{ readFromFile }}"
- debug:
msg: "{{ ((readFromFile.fulfilmentOrderItems | first).instanceCharacteristics | first).value }}"
- name: Update the fact
ansible.utils.update_fact:
updates:
- path: readFromFile.fulfilmentOrderItems[0].instanceCharacteristics[0].value
value: '0'
register: updated
- debug:
msg: "{{ updated.readFromFile }}"
将产生
的输出TASK [include_vars] ***********
ok: [localhost]
TASK [debug] *****************
ok: [localhost] =>
msg:
fulfilmentOrderItems:
- customerDates: {}
instanceCharacteristics:
- action: Cancel
name: VLAN
value: '24'
orderLineId:
source: AS
value: 123456-10
orderId:
identifier:
source: AS
value: 1234567-96_11
version: 1
TASK [debug] *****************
ok: [localhost] =>
msg: '24'
TASK [Update the fact] *******
changed: [localhost]
TASK [debug] *****************
ok: [localhost] =>
msg:
fulfilmentOrderItems:
- customerDates: {}
instanceCharacteristics:
- action: Cancel
name: VLAN
value: '0'
orderLineId:
source: AS
value: 123456-10
orderId:
identifier:
source: AS
value: 1234567-96_11
version: 1
进一步阅读