AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Transforming JSON Data with Ansible and Jinja2

By Luca Berton · Published 2024-01-01 · Category: troubleshooting

Learn how to automate JSON data transformations with Ansible and Jinja2 templates. This guide walks you through a practical example, including troubleshooting.

Introduction

In the world of IT automation, JSON data manipulation is a common task. Whether it’s reformatting data for API consumption or simplifying configurations, efficient handling of JSON is crucial. This article delves into leveraging Ansible and Jinja2 templates to transform JSON data dynamically.

We’ll tackle a specific example: renaming a JSON key while preserving the nested structure. By the end of this guide, you’ll be equipped to handle similar transformations in your automation workflows.

---

See also: Generate Clean YAML Output from Ansible Facts

The Problem Statement

Here’s the source JSON we want to transform:

{
  "primary_network": {
    "aa:bb:cc": "xyz"
  }
}

The goal is to transform it into:

{
  "network": {
    "aa:bb:cc": "xyz"
  }
}

This involves renaming the top-level key from primary_network to network, keeping the data structure intact.

---

The Solution: Using Ansible and Jinja2

Ansible, combined with Jinja2 templates, provides a clean and reusable approach to solving this problem.

Ansible Playbook

Below is an example playbook that defines the input JSON and applies a Jinja2 template to transform it.

- name: Transform JSON with Jinja2
  hosts: localhost
  gather_facts: no
  vars:
    source_json:
      primary_network:
        "aa:bb:cc": "xyz"
  tasks:
    - name: Transform JSON
      template:
        src: transform_template.j2
        dest: transformed_output.json

- name: Display the transformed JSON debug: msg: "{{ lookup('file', 'transformed_output.json') }}"

---

Jinja2 Template

Create a Jinja2 template named transform_template.j2 to perform the transformation.

{
  "network": {
    {% for key, value in source_json.primary_network.items() %}
    "{{ key }}": "{{ value }}"
    {% if not loop.last %}, {% endif %}
    {% endfor %}
  }
}

---

See also: Ansible Troubleshooting: Fix Jinja2 Syntax & Inventory Errors

Explanation of the Process

Access the Source JSON: • The vars section in the playbook defines the source JSON under the variable source_json. Jinja2 Transformation: • The template accesses primary_network and iterates over its key-value pairs using items(). • It dynamically constructs the network JSON object while ensuring there are no trailing commas. Output: • The transformed JSON is written to transformed_output.json.

---

Running the Playbook

To execute the playbook:

ansible-playbook transform_json.yml

Once executed, the playbook generates the transformed JSON in the specified file.

---

See also: Ansible Dynamic Data Construction: Build Variables at Runtime (Guide)

Troubleshooting and Tips

Common Errors

Too Many Values to Unpack: • Ensure that primary_network is a dictionary. If it’s a list or another data type, adjust the template accordingly. • Trailing Commas: • JSON doesn’t allow trailing commas. Use {% if not loop.last %} to handle this issue.

---

Final Output

The resulting file, transformed_output.json, will contain:

{
  "network": {
    "aa:bb:cc": "xyz"
  }
}

---

Why Use Ansible and Jinja2?

Automation: Reduces manual effort and ensures consistency. • Flexibility: Handles complex data transformations efficiently. • Integration: Seamlessly integrates with Ansible workflows for broader automation.

---

Conclusion

Transforming JSON data with Ansible and Jinja2 is a robust approach for automating data manipulation. By mastering these tools, you can simplify configuration management, streamline data workflows, and enhance your DevOps practices.

Try it out and take your automation skills to the next level!

Related Articles

the Ansible template module referencelooping in Ansible playbooks

Category: troubleshooting

Browse all Ansible tutorials · AnsiblePilot Home