summaryrefslogtreecommitdiffstats
path: root/README.md
blob: 260c31460b4881ef7c88a25a21091b704bd249d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
ghetto_json
===========

Need a quick way to edit a JSON file in Ansible?  Ansible has great built-in support for
 ini files, but a number of more modern applications are using JSON for config files.

ghetto_json lets you make some types of edits to JSON files, and remains simple enough that
  it's hopefully easier just to extend than to switch to a different module, and you won't feel
  too guilty just copy-pasting it into your codebase.

If [commentjson](https://pypi.python.org/pypi/commentjson/) is available, it will be used to
 *read* the file, but comments will be lost on save.  This is useful for making edits to default
 configurations shipped with some applications; where the application itself supports comments in
 JSON, but they're not required.


Installation
------------

Drop ``ghetto_json`` into your playbook's ``library`` folder,
 which can be [configured](https://docs.ansible.com/ansible/intro_configuration.html#library)
 but defaults to ``./library`` inside a playbook.

Synopsis
--------

Make in-place changes to simple JSON documents,
 without having to resort to ``replace``.


Requirements
------------

[commentjson](https://pypi.python.org/pypi/commentjson/) will be used, if available,
 but is explictly not required.

Python 2.7 may be required for some ``shlex`` functionality
 (like working Unicode), which you probably don't care about.


Options
-------

#### path:

The file on the target to edit.

#### all other options:

A very simple object notation for the location of the property to edit,
 and its new value.

Mandatory automatic conversion will be applied. Supported conversions are
listed below.  Everything else will be left as a string:

 * integers (``5``, ``-17``)
 * ``true`` / ``false``
 * ``null``
 * ``unset`` will delete the key


Examples
--------

For the example JSON document ``/foo/bar.json`` containing:
````
{ "a": 5, "b": {"c": 6, "d": "hello" } }
````

...you can run an invocation like:
````
 - ghetto_json:
     path=/foo/bar.json
     a=7
     b.c=yellow
     b.d=unset
````

...and the file will be left looking like:

````
{
  "a": 7,
  "b": {
    "c": "yellow"
  }
}
````