Introduction
JSON (JavaScript Object Notation) is a common data format for APIs and configuration management. In modern IT operations, querying JSON data is essential for automation. Ansible, a powerful tool for automating IT tasks, offers a robust json_query
filter that simplifies querying and extracting data. In this article, we will demonstrate how to use Ansible to extract a folder value from a JSON structure based on a specific name.
1. Why Automate JSON Queries?
JSON queries can become repetitive and error-prone when done manually. Automating this process with Ansible provides several advantages:
- Consistency: Avoid manual errors by standardizing queries.
- Efficiency: Save time with automated data extraction.
- Flexibility: Adapt quickly to changes in JSON structures.
2. Example Use Case: Extracting Folder Value by Name
Let’s consider a scenario where you need to extract the folder value for a specific name from JSON data.
The JSON Data
Here’s an example JSON structure:
[
{ "name": "foo", "folder": "foo_folder" },
{ "name": "bar", "folder": "bar_folder" }
]
The Goal
Retrieve the folder
value for name: foo
using Ansible.
3. The Playbook
Below is the Ansible playbook for this task:
---
- name: Extract folder value based on name
hosts: localhost
gather_facts: no
vars:
# Sample JSON data
json_data:
- { "name": "foo", "folder": "foo_folder" }
- { "name": "bar", "folder": "bar_folder" }
# Query to extract the folder value for the given name
query: "[?name=='{{ lookup_name }}'].folder | [0]"
# Variable to hold the name for which the folder value is queried
lookup_name: foo
tasks:
- name: Extract folder value for name={{ lookup_name }}
set_fact:
folder_value: "{{ json_data | json_query(query) }}"
- name: Display extracted folder value
debug:
msg: "The folder value for '{{ lookup_name }}' is '{{ folder_value }}'"
4. Understanding the Playbook
JSON Data
The json_data
variable represents the data to be queried. It’s a list of dictionaries with name
and folder
fields.
Query Syntax
The json_query
filter uses JMESPath syntax for querying:
?name=='{{ lookup_name }}'
: Filters objects withname
matchinglookup_name
..folder
: Retrieves thefolder
field from the matched objects.[0]
: Selects the first match.
set_fact
The set_fact
module dynamically sets the folder_value
variable with the query result.
Debugging
The debug
module displays the extracted folder value, verifying the playbook’s success.
5. Running the Playbook
Save the playbook as extract_folder.yml
and execute it with:
ansible-playbook extract_folder.yml
Example Output
TASK [Display extracted folder value] ******************************************
ok: [localhost] => {
"msg": "The folder value for 'foo' is 'foo_folder'"
}
6. Extending the Playbook
- Dynamic Input: Replace
lookup_name
with an external variable for flexibility:ansible-playbook extract_folder.yml -e "lookup_name=bar"
- Handling Multiple Results: To retrieve all matching values, remove
[0]
from the query:query: "[?name=='{{ lookup_name }}'].folder"
Conclusion
Ansible’s json_query
filter simplifies working with JSON data, enabling administrators to extract critical values with precision. By automating this process, you can streamline workflows, minimize errors, and handle complex JSON structures effortlessly.
Integrate this playbook into your automation toolkit to unlock new efficiencies in JSON data management!
Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.Academy
Learn the Ansible automation technology with some real-life examples in my Udemy 300+ Lessons Video Course.
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate