Show notes: The agent is in - Episode 35 - Automatic grouping of hosts with select_class

Posted by Nick Anderson
March 28, 2024

Have you ever wanted some hosts to organize themselves into different groups, but without spending time to worry about assigning a specific group?

Cody, Craig, and Nick talk about using the select_class feature in CFEngine. Nick implements automatic assignment of a maintainer for each host:

  • You want to assign a maintainer to each host that should be the primary point of contact.
  • Some hosts should may have a specific maintainer, but generally, you don’t care about the relationship between the person and the actual function of the machine.
  • It’s okay if the groups are not perfectly balanced.

First we provide a list of maintainers to select from.

bundle agent auto_maintainer
{
  vars:
    "maintainers" slist => { "Alvin", "Simon", "Theodore" };

Next we generate classes for each maintainer in the form maintainer_Name.

"maintainer_options"
  slist => maplist( "maintainer_$(this)", @(maintainers) );

Then if a maintainer has been automatically selected we define a variable to hold the maintainers name, tagged for inventory so that it’s easy to visualize in Mission Portal.

maintainer_automatically_selected::
  "maintainer"
    string => "$(maintainers)",
    if => "maintainer_$(maintainers)",
    meta => { "inventory", "attribute_name=Maintainer" };

Finally, we define the maintainer_automatically_selected class along with the selected maintainer class if a maintainer has not already been defined in the data:variables.maintainer variable.

classes:
  "maintainer_automatically_selected"
    select_class => { @(maintainer_options) },
    if => not( isvariable( "data:variables.maintainer" ) );
}

We saw that we got a pretty even distribution of maintainers without perseverating about each assignment.

Video

The video recording is available on YouTube:

At the end of every webinar, we stop the recording for a nice and relaxed, off-the-record chat with attendees. Join the next webinar to not miss this discussion.

Post show chit-chat

  • Is there an API or functionality to queue up new hosts for later trust acceptance?
    • No, not currently, but please consider commenting on CFE-4362
  • PSA: Check out the Hacker News post for CFEngine’s Star Trek and AI Origins (2023)
  • You can log promise outcomes with log_kept, log_repaired and log_failed attributes in action bodies.1, 2, 3
  • You can automatically attach a default body to all promises not using that body with default bodies.4
  • You can direct reports promises output to a file instead of standard out with the report_to_file attribute.5