How to read a value from JSON using Ansible?

Have the below Sample JSON:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{
"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
}
}
</code>
<code>{ "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 } } </code>
{
"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
    }
}

How do I extract a certain value like the name i.e VLAN or the value (12321321412a-10)?

Tried using

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>- 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 }}"
</code>
<code>- 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 }}" </code>
- 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 }}"

I get the below output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>TASK [Get the value from JSON] ************************
fatal:FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'value' is undefined
</code>
<code>TASK [Get the value from JSON] ************************ fatal:FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'value' is undefined </code>
TASK [Get the value from JSON] ************************
fatal:FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'value' is undefined

2

Q: “How do we replace the value i.e 24? I need to update the value and pass the JSON.

It is not possible to update variables in places, however, you could use update_fact module – Update currently set facts to create an other data structure.

A minimal example playbook

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>---
- 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 }}"
</code>
<code>--- - 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 }}" </code>
---
- 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 }}"

will result into an output of

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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

Further Readings

  • How to read JSON file using Ansible?
  • Ansible – Alter Registered Variable
  • How slicing in Python works

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật