pieterlexis.json_file
Ansible Library: json_file
This is a module that lets you change JSON files.
Requirements
None
Dependencies
None
Usage Examples
- hosts: all
tasks:
- name: Change value of 'foo' to 'bar'
json_file:
dest: /etc/file.conf
key: 'foo'
value: 'bar'
This module can handle nested JSON using a dot ('.') in the key. For example, given this file:
{ "foo": {
"bar": "buzz"
}
}
To change the value of "bar" to "whatever", do this:
- hosts: all
tasks:
- name: Change value of 'bar' to 'whatever'
json_file:
dest: /etc/file.conf
key: 'foo.bar'
value: 'whatever'
If a key name has a dot, you need to escape it:
{ "foo.bar": "buzz" }
You can change it like this:
- hosts: all
tasks:
- name: Change value of 'foo.bar' to 'whatever'
json_file:
dest: /etc/file.conf
key: 'foo\.bar'
value: 'whatever'
Setting a value to null
is tricky because of a bug in Ansible.
To set it to null, use None
:
- hosts: all
tasks:
- name: Set 'foo' to null
json_file:
dest: /etc/file.conf
key: 'foo'
value: None
Sometimes, you need to save a number as a string in the JSON file.
Set the as_string
argument to 'yes' for this:
- hosts: all
tasks:
- name: Set 'foo' to "25"
json_file:
dest: /etc/file.conf
key: 'foo'
value: 25
as_string: yes
This module supports all options available in the Ansible file module, like 'owner', 'group', and 'mode'.
Acknowledgements
This library is based on the ini_file core module and @FauxFaux's ghetto_json library.
Limitations
json_file
only works with JSON files that are dictionaries (i.e., have a top-level '{').
It does not work with lists.
License
MIT
Author Information
Pieter Lexis (@lieter_)
Library module to manipulate JSON files without the use of templates or files
ansible-galaxy install pieterlexis.json_file