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

1 2 3 4 5 6
% cf-agent -KIf ./example_canonify_list.cf
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
  <div class="gist-meta">
    <a href="https://gist.github.com/nickanderson/5271023/raw/d57a43831308cf31f0a5fe237b3d3436ed3d3049/Output" style="float:right">view raw</a> <a href="https://gist.github.com/nickanderson/5271023#file-output" style="float:right; margin-right:10px; color:#666;">Output</a> <a href="https://gist.github.com/nickanderson/5271023">This Gist</a> is brought to you using <a href="http://en.bainternet.info/2011/simple-gist-embed"><small>Simple Gist Embed</small></a>.
  </div>
</div>

<div class="gist-file">
  <div class="gist-data gist-syntax">
    <div class="file-data">
      <table cellpadding="0" cellspacing="0" class="lines highlight">
        <tr>
          <td class="line-numbers">
            <span class="line-number" id="file-example_canonify_list-cf-L1" rel="file-example_canonify_list-cf-L1">1</span> <span class="line-number" id="file-example_canonify_list-cf-L2" rel="file-example_canonify_list-cf-L2">2</span> <span class="line-number" id="file-example_canonify_list-cf-L3" rel="file-example_canonify_list-cf-L3">3</span> <span class="line-number" id="file-example_canonify_list-cf-L4" rel="file-example_canonify_list-cf-L4">4</span> <span class="line-number" id="file-example_canonify_list-cf-L5" rel="file-example_canonify_list-cf-L5">5</span> <span class="line-number" id="file-example_canonify_list-cf-L6" rel="file-example_canonify_list-cf-L6">6</span> <span class="line-number" id="file-example_canonify_list-cf-L7" rel="file-example_canonify_list-cf-L7">7</span> <span class="line-number" id="file-example_canonify_list-cf-L8" rel="file-example_canonify_list-cf-L8">8</span> <span class="line-number" id="file-example_canonify_list-cf-L9" rel="file-example_canonify_list-cf-L9">9</span> <span class="line-number" id="file-example_canonify_list-cf-L10" rel="file-example_canonify_list-cf-L10">10</span> <span class="line-number" id="file-example_canonify_list-cf-L11" rel="file-example_canonify_list-cf-L11">11</span> <span class="line-number" id="file-example_canonify_list-cf-L12" rel="file-example_canonify_list-cf-L12">12</span> <span class="line-number" id="file-example_canonify_list-cf-L13" rel="file-example_canonify_list-cf-L13">13</span> <span class="line-number" id="file-example_canonify_list-cf-L14" rel="file-example_canonify_list-cf-L14">14</span> <span class="line-number" id="file-example_canonify_list-cf-L15" rel="file-example_canonify_list-cf-L15">15</span> <span class="line-number" id="file-example_canonify_list-cf-L16" rel="file-example_canonify_list-cf-L16">16</span> <span class="line-number" id="file-example_canonify_list-cf-L17" rel="file-example_canonify_list-cf-L17">17</span> <span class="line-number" id="file-example_canonify_list-cf-L18" rel="file-example_canonify_list-cf-L18">18</span> <span class="line-number" id="file-example_canonify_list-cf-L19" rel="file-example_canonify_list-cf-L19">19</span> <span class="line-number" id="file-example_canonify_list-cf-L20" rel="file-example_canonify_list-cf-L20">20</span> <span class="line-number" id="file-example_canonify_list-cf-L21" rel="file-example_canonify_list-cf-L21">21</span> <span class="line-number" id="file-example_canonify_list-cf-L22" rel="file-example_canonify_list-cf-L22">22</span> <span class="line-number" id="file-example_canonify_list-cf-L23" rel="file-example_canonify_list-cf-L23">23</span> <span class="line-number" id="file-example_canonify_list-cf-L24" rel="file-example_canonify_list-cf-L24">24</span> <span class="line-number" id="file-example_canonify_list-cf-L25" rel="file-example_canonify_list-cf-L25">25</span> <span class="line-number" id="file-example_canonify_list-cf-L26" rel="file-example_canonify_list-cf-L26">26</span> <span class="line-number" id="file-example_canonify_list-cf-L27" rel="file-example_canonify_list-cf-L27">27</span> <span class="line-number" id="file-example_canonify_list-cf-L28" rel="file-example_canonify_list-cf-L28">28</span> <span class="line-number" id="file-example_canonify_list-cf-L29" rel="file-example_canonify_list-cf-L29">29</span> <span class="line-number" id="file-example_canonify_list-cf-L30" rel="file-example_canonify_list-cf-L30">30</span> <span class="line-number" id="file-example_canonify_list-cf-L31" rel="file-example_canonify_list-cf-L31">31</span> <span class="line-number" id="file-example_canonify_list-cf-L32" rel="file-example_canonify_list-cf-L32">32</span> <span class="line-number" id="file-example_canonify_list-cf-L33" rel="file-example_canonify_list-cf-L33">33</span> <span class="line-number" id="file-example_canonify_list-cf-L34" rel="file-example_canonify_list-cf-L34">34</span> <span class="line-number" id="file-example_canonify_list-cf-L35" rel="file-example_canonify_list-cf-L35">35</span> <span class="line-number" id="file-example_canonify_list-cf-L36" rel="file-example_canonify_list-cf-L36">36</span> <span class="line-number" id="file-example_canonify_list-cf-L37" rel="file-example_canonify_list-cf-L37">37</span> <span class="line-number" id="file-example_canonify_list-cf-L38" rel="file-example_canonify_list-cf-L38">38</span> <span class="line-number" id="file-example_canonify_list-cf-L39" rel="file-example_canonify_list-cf-L39">39</span>
          </td>

          <td class="line-data">
            <pre class="line-pre"><div class="line" id="file-example_canonify_list-cf-LC1">

body common control

{
bundlesequence => { "main" };
inputs => { "cfengine_stdlib.cf", };
}
 
bundle agent main
{
vars:
"apache_config[www.example.com][DocumentRoot]" string => "/var/www/www.example.com";
"apache_config[www.example.com][ServerAdmin]" string => "webmaster@example2.com";
"apache_config[www.example2.com][DocumentRoot]" string => "/var/www/www.example2.com";
"apache_config[www.example2.com][ServerAdmin]" string => "webmaster@example3.com";
 
methods:
"Testing" usebundle => test("main.apache_config");
}
 
bundle agent test(config)
{
vars:
# 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";
 
reports:
cfengine::
"canonified site: $(site_names_canonified_list)";
"alternate canonified site: $(site_names_canonified_list2)";
 
}
  <div class="gist-meta">
    <a href="https://gist.github.com/nickanderson/5271023/raw/b86b207ec0c8b89513a5882ce08330a97dedd513/example_canonify_list.cf" style="float:right">view raw</a> <a href="https://gist.github.com/nickanderson/5271023#file-example_canonify_list-cf" style="float:right; margin-right:10px; color:#666;">example_canonify_list.cf</a> <a href="https://gist.github.com/nickanderson/5271023">This Gist</a> is brought to you using <a href="http://en.bainternet.info/2011/simple-gist-embed"><small>Simple Gist Embed</small></a>.
  </div>
</div>