Introduction

Handling JSON data is a common task in IT automation workflows. Ansible simplifies this process with filters like from_json, json_query, and selectattr. This guide demonstrates how to:

1. Parse JSON data into a Python dictionary.

2. Transform the data into a structured list.

3. Search for specific criteria (e.g., if a name has a guestState of "running").

---

Use Case: Verify If a Guest Is Running

Imagine receiving JSON data from an API or file, containing details about virtual machine states. Your task is to:

1. Transform this JSON data into a structured format.

2. Check if a specific name has its guestState set to "running".

3. Return true or false based on the search.

---

The Ansible Playbook

Here’s how to achieve this:

Playbook Example

``yaml

---

  • hosts: localhost

gather_facts: false

vars:

search_name: "baz" # The name to search for

tasks:

- name: Define Raw JSON String (Example)

set_fact:

raw_json_string: >

{

"results": [

{ "instance": { "guest": { "guestState": "running" } }, "item": "foo" },

{ "instance": { "guest": { "guestState": "running" } }, "item": "bar" },

{ "instance": { "guest": { "guestState": "running" } }, "item": "baz" },

{ "instance": { "guest": { "guestState": "running" } }, "item": "qux" },

{ "instance": { "guest": { "guestState": "running" } }, "item": "quux" }

]

}

- name: Parse JSON string to dictionary

set_fact:

json_data: "{{ raw_json_string | from_json }}"

- name: Transform JSON to formatted list

set_fact:

formatted_list: "{{ json_data.results | json_query('[].{name: item, guestState: instance.guest.guestState}') }}"

- name: Check if guestState is running for a specific name

set_fact:

guest_running: "{{ formatted_list | selectattr('name', 'equalto', search_name) | selectattr('guestState', 'equalto', 'running') | list | length > 0 }}"

- name: Display the result

debug:

msg: "Is guest running? {{ guest_running }}"

`

---

Explanation

Key Sections

1. Define Raw JSON String:

- A sample JSON string is defined using set_fact. In real-world scenarios, this data may come from an API or file.

2. Parse JSON to Dictionary:

- The from_json filter converts the JSON string into a Python dictionary for further processing.

3. Transform JSON to Formatted List:

- The json_query filter restructures the JSON into a list of dictionaries with name and guestState attributes.

4. Search in the List:

- The selectattr filter dynamically searches for items in the list:

- selectattr('name', 'equalto', search_name): Filters items with the specified name.

- selectattr('guestState', 'equalto', 'running'): Filters items with a guestState of "running"`.

- The result is converted to a list and checked for le