Augeas the Missing Manual

Lately I’ve been using Augeas to configure some json files, and phew! it’s not been easy! So here’s a little guide to help other unfortunate souls lost in the Augeas wilderness.

1) First let’s start with a pretty basic json file located at /tmp/test.json:

1
{ "a": 1 }

2) Load

Loading the file with the json lens is easy enough once you know how

1
2
3
4
5
6
7
8
9
augtool> set /augeas/load/Json/lens Json.lns
augtool> set /augeas/load/Json/incl /tmp/test.json
augtool> load
augtool> print /files/tmp/test.json

/files/tmp/test.json
/files/tmp/test.json/dict
/files/tmp/test.json/dict/entry = "a"
/files/tmp/test.json/dict/entry/number = "1"

3) Set

As is changing the existing property

1
2
3
augtool> set /files/tmp/test.json/dict/entry[. = "a"]/number 2
augtool> save
Saved 1 file(s)

resulting in
1
{ "a" : 2 }

4) Set Type

Of course you can change this to a string or boolean property as required

1
2
3
4
augtool> rm /files/tmp/test.json/dict/entry[. = "a"]/number
augtool> set /files/tmp/test.json/dict/entry[. = "a"]/string hello
augtool> save
Saved 1 files(s)

to create
1
{ "a": "hello" }

5) Add Sub-object

And finally adding some subobjects with properties

1
2
3
4
5
6
set /files/tmp/test.json/dict/entry[. = "b"] b
set /files/tmp/test.json/dict/entry[. = "b"]/dict/entry[. = "c"] c
set /files/tmp/test.json/dict/entry[. = "b"]/dict/entry[. = "c"]/dict/entry[. = "d"] d
set /files/tmp/test.json/dict/entry[. = "b"]/dict/entry[. = "c"]/dict/entry[. = "d"]/string "world"
augtool> save
Saved 1 files(s)

to create an json structure with depth:
1
2
3
4
5
6
7
8
{
"a": "hello",
"b": {
"c": {
"d": "world"
}
}
}

Share Comments