Read a JSON file into a variable - Ansible lookup plugin file
How to automate the reading of example.json file on Ansible host, assign to a variable and use in your Ansible Playbook code.


How to Read a JSON file into a variable on the host with Ansible?
The JSON (JavaScript Object Notation) is an open standard file format used a lot for data interchange. I’m going to show you a live demo with some simple Ansible code. I’m Luca Berton, and welcome to today’s episode of Ansible Pilot.
Ansible reads a JSON file into a variable
ansible.builtin.file
- read file contentsfrom_json filter
- converts the variable to JSON.
Let’s dive deep into the Ansible lookup plugin file.
Plugins are a way to expand the Ansible functionality. With lookup plugins specifically, you can load variables or templates with information from external sources.
The full name is ansible.builtin.file
; it’s part of ansible-core
and is included in all Ansible installations.
The purpose of the file
lookup plugin is to read file contents.
The “from_json” is an Ansible-specific filter to convert the input to JSON.
Let’s combine the result of the file
lookup plugin with the from_json
filter for our use case.
The Best Resources For Ansible
Certifications
Video Course
Printed Book
eBooks
Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Ansible Cookbook: A Comprehensive Guide to Unleashing the Power of Ansible via Best Practices, Troubleshooting, and Linting Rules with Luca Berton
Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
demo
How to read the example.json JSON file, assign it to a variable and use it in your Ansible Playbook code.
code
- example.json
{
"name": "John",
"age": 30
}
- read_json.yml
---
- name: json read demo
hosts: localhost
vars:
jsondata: "{{ lookup('file', 'example.json') | from_json }}"
tasks:
- name: Print variable
ansible.builtin.debug:
var: jsondata
execution
$ ansible-playbook read_json.yml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the
implicit localhost does not match 'all'
PLAY [json read demo] *******************************************************************
TASK [Gathering Facts] ******************************************************************
ok: [localhost]
TASK [Print variable] *******************************************************************
ok: [localhost] => {
"jsondata": {
"age": 30,
"name": "John"
}
}
PLAY RECAP ******************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
idempotency
$ ansible-playbook read_json.yml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the
implicit localhost does not match 'all'
PLAY [json read demo] *******************************************************************
TASK [Gathering Facts] ******************************************************************
ok: [localhost]
TASK [Print variable] *******************************************************************
ok: [localhost] => {
"jsondata": {
"age": 30,
"name": "John"
}
}
PLAY RECAP ******************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Recap
Now you know how to read a JSON file into a variable on the host with Ansible. Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.
Academy
Learn the Ansible automation technology with some real-life examples in my
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