Canonifying a list with CFEngine 3

John Sanders Glass_pour_section_on_restaurant_wine_listshared a pattern for canonifying lists in #CFEngine on yesterday. It’s a useful reference so I thought I would replicate it here.

1 2 3 4 5 6
% cf-agent -KIf ./
R: canonified site: www_example_com
R: canonified site: www_example2_com
R: alternate canonified site: www_example_com
R: alternate canonified site: www_example2_com
!! Method invoked repairs
view raw Output This Gist is brought to you using Simple Gist Embed.
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
body common control
bundlesequence => { "main" };
inputs => { "", };
bundle agent main
"apache_config[][DocumentRoot]" string => "/var/www/";
"apache_config[][ServerAdmin]" string => "[email protected]";
"apache_config[][DocumentRoot]" string => "/var/www/";
"apache_config[][ServerAdmin]" string => "[email protected]";
"Testing" usebundle => test("main.apache_config");
bundle agent test(config)
# build a list from the first index on the config array
"site_names" slist => getindices("$(config)");
# Build a new array mapping the original index to a canonified version of itself
"site_names_canonified_map[$(site_names)]" string => canonify("$(site_names)");
# Build a list of just the canonified versions
"site_names_canonified_list" slist => maplist("$(site_names_canonified_map[$(this)])", "site_names");
# alternate way to build list of canonified values
# due to a bug in the way getindices/getvalues works this needs to happen on the pass after the map array is generated
"site_names_canonified_list2" slist => getvalues("site_names_canonified_map"), policy => "free";
"canonified site: $(site_names_canonified_list)";
"alternate canonified site: $(site_names_canonified_list2)";


In lines 9-12 of the example policy we build an array with values that need to be canonified. We use getindices to get the list of the first index. You could just as easily start with a list of values that need canonified. John used the maplist function to construct a new array with the original index values and their canonified versions. Another way to do this is to use getvalues. Currently (as of CFEngine 3.4.4) getindices and getvalues are not able to get the index of a generated array on the same pass, so its something to be aware of if your using that approach. However Johns approach using maplist is able to work on the same pass because it does not rely on getting the index of the newly generated map array.


No Comments

Leave a Reply

Your email is never shared.Required fields are marked *

To submit your comment, click the image below where it asks you to...
Clickcha - The One-Click Captcha