summaryrefslogtreecommitdiffstats
path: root/README.md
blob: 9c9c86c923fe3f2e7cbde3a2a1cb1eb8b9c59e57 (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
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.


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
------------

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"
  }
}
````