Augeas JSON example?

2,460 views
Skip to first unread message

John Simpson

unread,
Nov 1, 2013, 2:57:25 PM11/1/13
to puppet...@googlegroups.com
Greetings.

I'm trying to find a few concrete examples of how to use augeas within a Puppet module, to make changes to a JSON file. I've been googling for the past hour and a half, and while I have found several pages which provide reference-type documentation, I have yet to see a single usable example. All of the augeas examples seem to demonstrate how to maintain a file for which some other lens applies, such as /etc/hosts or /etc/ssh/sshd_config.

My situation: I have a system-wide JSON file which contains a series of key-value pairs, like so:

{
"svc_host" : "server.domain.xyz" ,
"svc_port" : "12345" ,
"svc_user" : "username" ,
"svc_pass" : "password" ,

"versions" : [ "1.0" , "1.0.1" , "1.0.2" ] ,

"agent_installed" : "1383331588" ,

"comment" : "This is a gratuitous comment."
}

I know that JSON is probably not be the best format for the file, but it's human readable, and it saves us having to write and debug configuration file parsing routines in seven different languages on four platforms.

I would like to be able to use Puppet modules to maintain the file. My first thought, of course, was to use a template, but there are several different components of the system which all store data in the one file, so different modules need to maintain different sets of "keys" within the file.

Is anybody doing something similar, and if so, would you mind sharing the relevant bits of your manifests? Or point me to an example of a WORKING augeas resource which adds or

--
John Simpson <jm...@voalte.com>
Unix System/VM Developer and Engineering Operations, Voalte
+1 (941) 312-2830 x148

David Lutterkort

unread,
Nov 13, 2013, 2:56:46 PM11/13/13
to puppet...@googlegroups.com
I don't have any examples handy, but the easiest way to try out managing the file with Puppet is to store it somewhere, say /tmp/file.json and then play around with it in augtool:

augtool -A -t 'Json incl /tmp/file.json'
augtool> match /files/tmp/file.json/dict/entry[. = 'svc_port']/*
/files/tmp/file.json/dict/entry[2]/string = 12345
augtool> set /files/tmp/file.json/dict/entry[. = 'svc_port']/string 4242
augtool> rm /files/tmp/file.json/dict/entry[. = 'svc_pass']
rm : /files/tmp/file.json/dict/entry[. = 'svc_pass'] 2
augtool> save
Saved 1 file(s)
augtool> quit

> cat /tmp/file.json
...

David

John Simpson

unread,
Nov 14, 2013, 3:59:50 PM11/14/13
to puppet...@googlegroups.com

On 2013-11-13, at 1456, David Lutterkort <lut...@puppetlabs.com> wrote:

> I don't have any examples handy, but the easiest way to try out managing the file with Puppet is to store it somewhere, say /tmp/file.json and then play around with it in augtool:

That's what I eventually ended up doing, and after several hours of tinkering with it I was able to come up with most of what I need - I have puppet templates which store strings and numbers in the top-level "dict" within the JSON file.

The last part is being able to maintain an array of dictionaries within the top-level "dict" (in particular, an array of LDAP DN's.) In the file, it looks like this:

{
"server" : "hostname.domain.xyz" ,
"port" : 12345 ,
"groups" : [
{ "dn" : "cn=Users,dc=domain,dc=xyz" } ,
{ "dn" : "cn=Admins,dc=domain,dc=xyz" }
]
}

In augtool, I am able to do the following:

set /augeas/load/Json/lens Json.lns
set /augeas/load/Json/incl /tmp/file.json
load
set /files/tmp/file.json/dict/entry[last()+1] 'groups'
touch /files/tmp/file.json/dict/entry[last()]/array

However, when I try to do it in Puppet:

augeas { "groups_array" :
incl => '/tmp/file.json' ,
lens => 'Json.lns' ,
changes => [
"set dict/entry[last()+1] 'groups'" ,
"touch dict/entry[last()]/array" ,
] ,
onlyif => "match dict/entry[*][.='groups'] size == 0" ,
require => File['/tmp/file.json'] ,
}

I get the following error:

Error: /Stage[main]/Module::Groups/Augeas[groups_array]: Could not evaluate: Unknown command touch

Does the Puppet "augeas" module not support the "touch" command at all? If not, is there a way to declare an array like this, without declaring a member of the array at the same time? The class itself declares the array, and it also defines a template that another class will call one or more times to add items to the array. Different machines will need different numbers of items in the array, and some machines may not need any items at all.

The most frustrating thing to me is how little documentation there seems to be out there, about using the JSON lens. The augeas web site doesn't list it with the other lenses, and on the "list of core lenses" page it's there but the link goes to a page which doesn't exist. I think it's amazing that I was able to cobble together what I already have, and I think once I get this all figured out, I'll have to write a page on my personal web site explaining it.
Reply all
Reply to author
Forward
0 new messages