AnsiblePilot — Master Ansible Automation
AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 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 "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, 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.
Ansible Read JSON File: lookup file Plugin & from_json (Guide) — Video Tutorial
How to read JSON files into variables in Ansible using lookup file plugin and from_json filter. Load config files, parse data, and access nested values.
What You'll Learn
- How to Read a JSON file into a variable on the host with Ansible?
- Ansible reads a JSON file into a variable
- Playbook
- code
- execution
- idempotency
- Conclusion
- Read Local JSON File
- Read JSON Relative to Playbook
- Read Remote JSON File
Full Tutorial Content
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 Playbook 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 contents
- `from_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.
Playbook
How to read the example.json JSON file, assign it to a variable and use it in your Ansible Playbook code.
code
- example.json
```json
{
"name": "John",
"age": 30
}
```
- read_json.yml
```yaml
---
- name: json read Playbook
hosts: localhost
vars:
jsondata: "{{ lookup('file', 'example.json') | from_json }}"
tasks:
- name: Print variable
ansible.builtin.debug:
var: jsondata
```
execution
```bash
$ 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 Playbook] *******************************************************************
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
```bash
$ 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 Playbook] *******************************************************************
TASK [Gathering Facts] ******************************************************************
ok: [localhost]
TASK [Print variable] *******************************************************************
ok: [localhost] => {
"jsondata": {
"age": 30,
"name": "John"
}
}
PLAY RECAP ******************************************************************
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 6 min
- Category: installation
Read the full written article: Ansible Read JSON File: lookup file Plugin & from_json (Guide)
Related Video Tutorials
- Ansible File Lookup: Read File Contents into a Variable (Examples) — How to read file contents into an Ansible variable using the file lookup plugin. Read local files, SSH keys, certificates, and config files with practical examples.
- Creating a Custom Ansible Lookup Plugin in Python for Reading a File — Create a custom Ansible lookup plugin in Python to extend capabilities for reading file contents on the Ansible controller, enhancing file retrieval flexibility.
- Creating a Custom Ansible Lookup Plugin in Python for retrieving API token — Learn how to create an Ansible lookup plugin to fetch API tokens, with a complete example of the token.py plugin code and step-by-step explanations.
- Executing Custom Lookup Plugins in the Ansible Automation Platform — How to execute two custom Ansible Lookup Plugins to interact with API and read the contents of files in the local Ansible Controller.
- Ansible Delete Files in Directory: find & file Modules (Examples) — How to delete only files inside a directory in Ansible using find and file modules. Clean up logs, temp files, and old backups by age, pattern, and size.
- Ansible Read Environment Variable: env Lookup Plugin (Examples) — How to read environment variables in Ansible using the env lookup plugin. Access PATH, HOME, CI variables, and pass environment to tasks with practical examples.