There is a known bug where getindices is unable to get the index Workaroundof a dynamically generated array until the next pass. This comes up from time to time so I thought I would post an example using a pattern shared on the CFEngine help mailing list. The policy below shows 3 working examples. They are mostly similar, example0 shows the simplest workaround, example1 and example2 in addition show ways to suppress the “Duplicate selection of value for variable” messages.

1 2 3 4 5 6 7 8 9 10 11 12 13 14
cf-agent -KIf ./test_getindices_dynamic_array.cf
!! Duplicate selection of value for variable "idx" in scope example0
!! Rule from ./test_getindices_dynamic_array.cf at/before line 28
!! Duplicate selection of value for variable "idx" in scope example0
!! Rule from ./test_getindices_dynamic_array.cf at/before line 28
R: example0 item1
R: example0 item2
R: example0 item3
R: example1 item1
R: example1 item2
R: example1 item3
R: example2 item1
R: example2 item2
R: example2 item3
  <div class="gist-meta">
    <a href="https://gist.github.com/nickanderson/5154392/raw/0173ced1600e7bb221dafb5661ff516f14e6929b/output.txt" style="float:right">view raw</a> <a href="https://gist.github.com/nickanderson/5154392#file-output-txt" style="float:right; margin-right:10px; color:#666;">output.txt</a> <a href="https://gist.github.com/nickanderson/5154392">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-test_getindices_dynamic_array-cf-L1" rel="file-test_getindices_dynamic_array-cf-L1">1</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L2" rel="file-test_getindices_dynamic_array-cf-L2">2</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L3" rel="file-test_getindices_dynamic_array-cf-L3">3</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L4" rel="file-test_getindices_dynamic_array-cf-L4">4</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L5" rel="file-test_getindices_dynamic_array-cf-L5">5</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L6" rel="file-test_getindices_dynamic_array-cf-L6">6</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L7" rel="file-test_getindices_dynamic_array-cf-L7">7</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L8" rel="file-test_getindices_dynamic_array-cf-L8">8</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L9" rel="file-test_getindices_dynamic_array-cf-L9">9</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L10" rel="file-test_getindices_dynamic_array-cf-L10">10</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L11" rel="file-test_getindices_dynamic_array-cf-L11">11</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L12" rel="file-test_getindices_dynamic_array-cf-L12">12</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L13" rel="file-test_getindices_dynamic_array-cf-L13">13</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L14" rel="file-test_getindices_dynamic_array-cf-L14">14</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L15" rel="file-test_getindices_dynamic_array-cf-L15">15</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L16" rel="file-test_getindices_dynamic_array-cf-L16">16</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L17" rel="file-test_getindices_dynamic_array-cf-L17">17</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L18" rel="file-test_getindices_dynamic_array-cf-L18">18</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L19" rel="file-test_getindices_dynamic_array-cf-L19">19</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L20" rel="file-test_getindices_dynamic_array-cf-L20">20</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L21" rel="file-test_getindices_dynamic_array-cf-L21">21</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L22" rel="file-test_getindices_dynamic_array-cf-L22">22</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L23" rel="file-test_getindices_dynamic_array-cf-L23">23</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L24" rel="file-test_getindices_dynamic_array-cf-L24">24</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L25" rel="file-test_getindices_dynamic_array-cf-L25">25</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L26" rel="file-test_getindices_dynamic_array-cf-L26">26</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L27" rel="file-test_getindices_dynamic_array-cf-L27">27</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L28" rel="file-test_getindices_dynamic_array-cf-L28">28</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L29" rel="file-test_getindices_dynamic_array-cf-L29">29</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L30" rel="file-test_getindices_dynamic_array-cf-L30">30</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L31" rel="file-test_getindices_dynamic_array-cf-L31">31</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L32" rel="file-test_getindices_dynamic_array-cf-L32">32</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L33" rel="file-test_getindices_dynamic_array-cf-L33">33</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L34" rel="file-test_getindices_dynamic_array-cf-L34">34</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L35" rel="file-test_getindices_dynamic_array-cf-L35">35</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L36" rel="file-test_getindices_dynamic_array-cf-L36">36</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L37" rel="file-test_getindices_dynamic_array-cf-L37">37</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L38" rel="file-test_getindices_dynamic_array-cf-L38">38</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L39" rel="file-test_getindices_dynamic_array-cf-L39">39</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L40" rel="file-test_getindices_dynamic_array-cf-L40">40</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L41" rel="file-test_getindices_dynamic_array-cf-L41">41</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L42" rel="file-test_getindices_dynamic_array-cf-L42">42</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L43" rel="file-test_getindices_dynamic_array-cf-L43">43</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L44" rel="file-test_getindices_dynamic_array-cf-L44">44</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L45" rel="file-test_getindices_dynamic_array-cf-L45">45</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L46" rel="file-test_getindices_dynamic_array-cf-L46">46</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L47" rel="file-test_getindices_dynamic_array-cf-L47">47</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L48" rel="file-test_getindices_dynamic_array-cf-L48">48</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L49" rel="file-test_getindices_dynamic_array-cf-L49">49</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L50" rel="file-test_getindices_dynamic_array-cf-L50">50</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L51" rel="file-test_getindices_dynamic_array-cf-L51">51</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L52" rel="file-test_getindices_dynamic_array-cf-L52">52</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L53" rel="file-test_getindices_dynamic_array-cf-L53">53</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L54" rel="file-test_getindices_dynamic_array-cf-L54">54</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L55" rel="file-test_getindices_dynamic_array-cf-L55">55</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L56" rel="file-test_getindices_dynamic_array-cf-L56">56</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L57" rel="file-test_getindices_dynamic_array-cf-L57">57</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L58" rel="file-test_getindices_dynamic_array-cf-L58">58</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L59" rel="file-test_getindices_dynamic_array-cf-L59">59</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L60" rel="file-test_getindices_dynamic_array-cf-L60">60</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L61" rel="file-test_getindices_dynamic_array-cf-L61">61</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L62" rel="file-test_getindices_dynamic_array-cf-L62">62</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L63" rel="file-test_getindices_dynamic_array-cf-L63">63</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L64" rel="file-test_getindices_dynamic_array-cf-L64">64</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L65" rel="file-test_getindices_dynamic_array-cf-L65">65</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L66" rel="file-test_getindices_dynamic_array-cf-L66">66</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L67" rel="file-test_getindices_dynamic_array-cf-L67">67</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L68" rel="file-test_getindices_dynamic_array-cf-L68">68</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L69" rel="file-test_getindices_dynamic_array-cf-L69">69</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L70" rel="file-test_getindices_dynamic_array-cf-L70">70</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L71" rel="file-test_getindices_dynamic_array-cf-L71">71</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L72" rel="file-test_getindices_dynamic_array-cf-L72">72</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L73" rel="file-test_getindices_dynamic_array-cf-L73">73</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L74" rel="file-test_getindices_dynamic_array-cf-L74">74</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L75" rel="file-test_getindices_dynamic_array-cf-L75">75</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L76" rel="file-test_getindices_dynamic_array-cf-L76">76</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L77" rel="file-test_getindices_dynamic_array-cf-L77">77</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L78" rel="file-test_getindices_dynamic_array-cf-L78">78</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L79" rel="file-test_getindices_dynamic_array-cf-L79">79</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L80" rel="file-test_getindices_dynamic_array-cf-L80">80</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L81" rel="file-test_getindices_dynamic_array-cf-L81">81</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L82" rel="file-test_getindices_dynamic_array-cf-L82">82</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L83" rel="file-test_getindices_dynamic_array-cf-L83">83</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L84" rel="file-test_getindices_dynamic_array-cf-L84">84</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L85" rel="file-test_getindices_dynamic_array-cf-L85">85</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L86" rel="file-test_getindices_dynamic_array-cf-L86">86</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L87" rel="file-test_getindices_dynamic_array-cf-L87">87</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L88" rel="file-test_getindices_dynamic_array-cf-L88">88</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L89" rel="file-test_getindices_dynamic_array-cf-L89">89</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L90" rel="file-test_getindices_dynamic_array-cf-L90">90</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L91" rel="file-test_getindices_dynamic_array-cf-L91">91</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L92" rel="file-test_getindices_dynamic_array-cf-L92">92</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L93" rel="file-test_getindices_dynamic_array-cf-L93">93</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L94" rel="file-test_getindices_dynamic_array-cf-L94">94</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L95" rel="file-test_getindices_dynamic_array-cf-L95">95</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L96" rel="file-test_getindices_dynamic_array-cf-L96">96</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L97" rel="file-test_getindices_dynamic_array-cf-L97">97</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L98" rel="file-test_getindices_dynamic_array-cf-L98">98</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L99" rel="file-test_getindices_dynamic_array-cf-L99">99</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L100" rel="file-test_getindices_dynamic_array-cf-L100">100</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L101" rel="file-test_getindices_dynamic_array-cf-L101">101</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L102" rel="file-test_getindices_dynamic_array-cf-L102">102</span> <span class="line-number" id="file-test_getindices_dynamic_array-cf-L103" rel="file-test_getindices_dynamic_array-cf-L103">103</span>
          </td>

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

body common control

{
 
bundlesequence => {
"example0",
"example1",
"example2",
};
}
 
bundle agent example0
# This pattern was shared on the CFEngine help mailing list by cyril
# Thanks!
{
vars:
"list" slist => { "1", "2", "3" };
 
# Autogenerate an array instead of typing it out
# "array[item1]" string => "value1";
# "array[item2]" string => "value2";
# "array[item3]" string => "value3";
"array[item$(list)]" string => "value$(list)";
 
!idx_defined::
"idx"
slist => { getindices("array") },
handle => "$(this.bundle)_get_array_index";
 
classes:
"idx_defined" expression => isvariable("idx");
 
reports:
cfengine::
# should report 3 times itemx where x is a list value
"$(this.bundle) $(idx)";
 
}
 
 
bundle agent example1
# This pattern was shared on the CFEngine help mailing list by cyril
# Thanks!
{
vars:
"list" slist => { "1", "2", "3" };
 
# Autogenerate an array instead of typing it out
# "array[item1]" string => "value1";
# "array[item2]" string => "value2";
# "array[item3]" string => "value3";
"array[item$(list)]" string => "value$(list)";
 
list_defined.!idx_defined::
"idx"
slist => { getindices("array") },
handle => "$(this.bundle)_get_array_index";
 
classes:
"list_defined"
expression => isvariable("list"),
comment => "This is one way to work around 'Duplicate selection of
value for variable' messages. Your forcing get_array_index
to not be evaluated until the next pass";
 
"idx_defined" expression => isvariable("idx");
 
reports:
cfengine::
# should report 3 times itemx where x is a list value
"$(this.bundle) $(idx)";
 
}
 
bundle agent example2
# This pattern was shared on the CFEngine help mailing list by cyril
# Thanks!
{
vars:
"list" slist => { "1", "2", "3" };
 
# Autogenerate an array instead of typing it out
# "array[item1]" string => "value1";
# "array[item2]" string => "value2";
# "array[item3]" string => "value3";
"array[item$(list)]" string => "value$(list)";
 
!idx_defined::
"idx"
slist => { getindices("array") },
policy => "free",
handle => "$(this.bundle)_get_array_index",
comment => "Setting policy free is another way to suppress 'Duplicate
selection of value for variable' messages.";
 
classes:
"idx_defined" expression => isvariable("idx");
 
reports:
cfengine::
# should report 3 times itemx where x is a list value
"$(this.bundle) $(idx)";
 
}
  <div class="gist-meta">
    <a href="https://gist.github.com/nickanderson/5154392/raw/d1b746f381b08a8e466820e0152676ab1fbf585a/test_getindices_dynamic_array.cf" style="float:right">view raw</a> <a href="https://gist.github.com/nickanderson/5154392#file-test_getindices_dynamic_array-cf" style="float:right; margin-right:10px; color:#666;">test_getindices_dynamic_array.cf</a> <a href="https://gist.github.com/nickanderson/5154392">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>