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
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.
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 ******************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
```
[c