Working around getindices with dynamically generated arrays in CFEngine 3

There is a known bug where getindices is unable to get the index Workaroundof a dynam­i­cally gen­er­ated array until the next pass. This comes up from time to time so I thought I would post an exam­ple using a pat­tern shared on the CFEngine help mail­ing list. The pol­icy below shows 3 work­ing exam­ples. They are mostly sim­i­lar, example0 shows the sim­plest workaround, example1 and example2 in addi­tion show ways to sup­press the “Dupli­cate selec­tion of value for vari­able” 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
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
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)";
 
}

 

 

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