Forum: CFEngine Help
Subject: Re: Problem accessing list variables in hash
Author: zzamboni
Link to topic: https://cfengine.com/forum/read.php?3,26268,26270#msg-26270

Hi Mike,

Due to the way variable convergence works in CFEngine, looping over 
multi-dimensional arrays doesn't work as one would expect. I've encountered 
this many times myself, and need to remind myself that CFEngine is *not* a 
conventional programming language.

One way to achieve what you want is to move the loop over the users to the 
caller bundle, i.e. call the bundle one per user. This way, the inner bundle is 
only dealing with a plain array. This snippet works:

body common control
{
        bundlesequence => { "test" };
}

bundle agent test
{
  vars:
      "users"   string => "Web server user";
      "users"    string => "/var/lib/www";
      "users"   string => "www-user";
      "users"  slist => { "fred", "bill", "mary" };

      # User loop moved from the inner bundle to the caller
      "userlist" slist => getindices("users");

  methods:
      # Bundle called once per user, with the username and its corresponding 
part of the array
      "users" usebundle => create_system_user("$(userlist)", 
"test.users[$(userlist)]");
}

bundle agent create_system_user(u, p)
{
  vars:
      "keys"  slist => { "@($(p))" };
  reports:
    cfengine::
      "user: $(u)";
      "keys: $(keys)";
}


_______________________________________________
Help-cfengine mailing list
Help-cfengine@cfengine.org
https://cfengine.org/mailman/listinfo/help-cfengine

Reply via email to