peerleader

Table of Contents

Prototype: peerleader(filename, regex, groupsize)

Return type: string

Description: Returns the current host's partition peer leader.

So given groupsize 3 and the file

a
b
c
# this is a comment d
e

The peer leader of host b will be host a.

Given a list of host names in filename, one per line, and excluding comment lines starting with the unanchored regular expression regex, CFEngine partitions the host list into groups of up to groupsize. Each group's peer leader is the first host in the group.

The current host (unqualified or fully qualified) should belong to this file if it is expected to interact with the others. The function fails otherwise.

If the current host name (fully qualified or unqualified) is the peer leader, the string localhost is used instead of the host name.

Arguments:

  • filename: string, in the range: "?(/.*)
  • regex: regular expression, in the range: .*
  • groupsize: int, in the range: 2,64

groupsize must be between 2 and 64 to avoid nonsensical promises.

Example:

Prepare:

echo alpha > /tmp/cfe_hostlist
echo beta >> /tmp/cfe_hostlist
echo gamma >> /tmp/cfe_hostlist
echo "Set HOSTNAME appropriately beforehand"
echo "$HOSTNAME" >> /tmp/cfe_hostlist
echo "Delta Delta Delta may I help ya help ya help ya"
echo delta1 >> /tmp/cfe_hostlist
echo delta2 >> /tmp/cfe_hostlist
echo delta3 >> /tmp/cfe_hostlist
echo may1.I.help.ya >> /tmp/cfe_hostlist
echo may2.I.help.ya >> /tmp/cfe_hostlist
echo may3.I.help.ya >> /tmp/cfe_hostlist

Run:

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

bundle agent peers
{
  vars:

      "mygroup" slist => peers("/tmp/cfe_hostlist","#.*",4);

      "myleader" string => peerleader("/tmp/cfe_hostlist","#.*",4);

      "all_leaders" slist => peerleaders("/tmp/cfe_hostlist","#.*",4);

  reports:

      # note that the current host name is fourth in the host list, so
      # its peer group is the first 4-host group, minus the host itself.
      "/tmp/cfe_hostlist mypeer $(mygroup)";
      # note that the current host name is fourth in the host list, so
      # the peer leader is "alpha"
      "/tmp/cfe_hostlist myleader $(myleader)";
      "/tmp/cfe_hostlist another leader $(all_leaders)";
}

Output:

R: /tmp/cfe_hostlist mypeer alpha
R: /tmp/cfe_hostlist mypeer beta
R: /tmp/cfe_hostlist mypeer gamma
R: /tmp/cfe_hostlist myleader alpha
R: /tmp/cfe_hostlist another leader alpha
R: /tmp/cfe_hostlist another leader delta1
R: /tmp/cfe_hostlist another leader may2.I.help.ya