Splitting light from dark and data from policies

23 views
Skip to first unread message

Martin Simons

unread,
Oct 5, 2017, 3:02:53 PM10/5/17
to help-cfengine
Dear CFEngineer,

Some of you who know me, know I'm fond of arrays.

To be honest, I am puzzled. I thought this would work, where do I go wrong?

bundle agent common {
vars:
  "index"                        slist => getindices($(data.array));
methods:
  ""                         usebundle => usevar( $(data.test) );
  ""                         usebundle => use_array( $(data.array) );
reports:
  "$(this.bundle): $(data.test)";
  "$(this.bundle): $(index)";
}
bundle agent usevar(var) {
reports:
  "$(this.bundle): $(var)";
}
bundle agent use_array(array) {
vars:
  "index"                        slist => getindices( $(array) );
reports:
  "$(this.bundle): $(index)";
  "$(this.bundle): $(array[$(index)])";
}

bundle agent data {
vars:
  "test"                        string => "Test variable for common";
  "array[one]"                  string => "one";
  "array[two]"                  string => "two";
}
body common control
{
      bundlesequence => { common, };
      inputs => { "/root/promises.cf", };
}

Output:
root@testbox(semi production in fact):~# cf-agent -KI -f ./promises.cf 
R: usevar: Test variable for common
R: use_array: $(index)
R: use_array: $(array[$(index)])
R: common: Test variable for common
R: common: $(index)

Hope to see you in LA, December 11th.

Kind regards,
Martin.

Nick Anderson

unread,
Oct 5, 2017, 3:15:55 PM10/5/17
to Martin Simons, help-cfengine

Martin Simons <mjcm....@gmail.com> writes:

> Dear CFEngineer,
>
> Some of you who know me, know I'm fond of arrays.
>
> To be honest, I am puzzled. I thought this would work, where do I go wrong?

I made a few of minor modifications to your policy:

  • Introduced bundle agent main instead of body common control (non functional change)
  • Use name of variable with getindices not its expansion
  • Pass name of array instead of its expansion.
bundle agent main
{
  methods:
    "common";
}
bundle agent common {
  vars:

    # Get the index from the named variable, not its expansion.
      "index"                        slist => getindices("data.array");

  
methods:
      ""                         usebundle => usevar( $(data.test) )
;

    # Pass the NAME of the variable, not its expansion.
      ""                         usebundle => use_array( "data.array" );

  reports:
      "$(this.bundle): data.test = $(data.test)";
      "$(this.bundle): index value = $(index)";
}

bundle agent usevar(var) {
  reports:
      "$(this.bundle): $(var)";
}

bundle agent use_array(array) {
  vars:
      "index"                        slist => getindices( $(array) );
  reports:
      "$(this.bundle): $(index)";
      "$(this.bundle): $(array[$(index)])";
}

bundle agent data {
  vars:
      "test"                        string => "Test variable for common";
      "array[one]"                  string => "one";
      "array[two]"                  string => "two";
}
R: usevar: Test variable for common
R: use_array: two
R: use_array: one
R: common: data.test = Test variable for common
R: common: index value = two
R: common: index value = one


Nick Anderson
Doer of things, CFEngine

Martin Simons

unread,
Oct 5, 2017, 6:43:18 PM10/5/17
to help-cfengine
Dear Nick,

Thank you, you put me on the right track.

Working with arrays often is an in depth operation. This is how I meant it to be:
Code:
bundle agent common {
vars:
  "index"                        slist => getindices("data.array");
methods:
  ""                         usebundle => usevar( $(data.test) );
  ""                         usebundle => use_array( "data.array" );
reports:
  "$(this.bundle): $(data.test)";
  "$(this.bundle): $(index)";
}
bundle agent usevar(var) {
reports:
  "$(this.bundle): $(var)";
}
bundle agent use_array(array) {
vars:
  "index"                        slist => getindices( $(array) );
methods:
  ""                         usebundle => array_row("$(index)", "$($(array)[$(index)])");
reports:
  "$(this.bundle): index $(index)";
}
bundle agent array_row(index, array_row) {
vars:
reports:
  "$(this.bundle): array row $(index) $(array_row)";
}

bundle agent data {
vars:
  "test"                        string => "Test variable for common";
  "array[one]"                  string => "first element";
  "array[two]"                  string => "second element";
}
body common control
{
      bundlesequence => { common, };
      inputs => { "/root/promises.cf", };
}

Output:
R: usevar: Test variable for common
R: array_row: array row one first element
R: array_row: array row two second element
R: use_array: index one
R: use_array: index two
R: common: Test variable for common
R: common: one
R: common: two

Kind regards,
Martin.

Op donderdag 5 oktober 2017 20:15:55 UTC+1 schreef Nick Anderson:
Reply all
Reply to author
Forward
0 new messages