Skip to content

Instantly share code, notes, and snippets.

@tzz
Last active October 20, 2015 15:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tzz/e7829d0ea6e52a2daec9 to your computer and use it in GitHub Desktop.
Save tzz/e7829d0ea6e52a2daec9 to your computer and use it in GitHub Desktop.
mapdata() example with CFEngine
bundle agent main
{
methods:
"test";
vars:
"test_state" data => bundlestate(test);
"test_string" string => storejson(test_state);
reports:
"$(this.bundle): state of things = $(test_string)";
}
bundle agent test
{
vars:
"load" data => parsejson('{ "a": { "a1": "V1", "a2": "V2" }, "b": { "b1": "V1", "b2": "V2" }, "c": [ 100, 200, 300 ] }');
"load2[mykey]" slist => { "a", "b", "c" };
"load3" data => '[ {"k": "v1"}, {"k": "v2"}]';
"jsonspec" string => '{ "key": "$(this.k)", "key2": "$(this.k[1])", "value": "$(this.v)" }';
"users" data => '
[
{ "user": "joe",
"hash": "*KRB*",
"uid" : "1357",
"promisee": "sysadmin team",
},
{ "user": "bob",
"hash": "*LCK*",
"uid" : "1354",
"promisee": "sysadmin team",
}
]
';
"userkeys" data => mapdata("none", '$(users[$(this.k)][user])', users);
"userkeys_slist" slist => getvalues(userkeys);
"mapdata_json" data => mapdata("json", $(jsonspec), load);
"mapdata_json2" data => mapdata("json", $(jsonspec), load2);
"mapdata_json3" data => mapdata("none", "$(this.v)", load3);
}
output:
R: main: state of things = {
"jsonspec": "{ \"key\": \"$(this.k)\", \"key2\": \"$(this.k[1])\", \"value\": \"$(this.v)\" }",
"load": {
"a": {
"a1": "V1",
"a2": "V2"
},
"b": {
"b1": "V1",
"b2": "V2"
},
"c": [
100,
200,
300
]
},
"load2[mykey]": [
"a",
"b",
"c"
],
"load3": [
{
"k": "v1"
},
{
"k": "v2"
}
],
"mapdata_json": [
{
"key": "a",
"key2": "a1",
"value": "V1"
},
{
"key": "a",
"key2": "a2",
"value": "V2"
},
{
"key": "b",
"key2": "b1",
"value": "V1"
},
{
"key": "b",
"key2": "b2",
"value": "V2"
},
{
"key": "c",
"key2": "$(this.k[1])",
"value": "100"
},
{
"key": "c",
"key2": "$(this.k[1])",
"value": "200"
},
{
"key": "c",
"key2": "$(this.k[1])",
"value": "300"
}
],
"mapdata_json2": [
{
"key": "mykey",
"key2": "$(this.k[1])",
"value": "a"
},
{
"key": "mykey",
"key2": "$(this.k[1])",
"value": "b"
},
{
"key": "mykey",
"key2": "$(this.k[1])",
"value": "c"
}
],
"mapdata_json3": [
"v1",
"v2"
],
"userkeys": [
"joe",
"bob"
],
"userkeys_slist": [
"joe",
"bob"
],
"users": [
{
"hash": "*KRB*",
"promisee": "sysadmin team",
"uid": "1357",
"user": "joe"
},
{
"hash": "*LCK*",
"promisee": "sysadmin team",
"uid": "1354",
"user": "bob"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment