2

Is there any difference in the way the ifvarclass => 'class' construct vs class:: construct work? Can I use both interchangeably?

Te Ri
  • 177
  • 1
  • 5

1 Answers1

3

Not exactly, but almost. Note that any time you use a class expression (expression::) that context restriction applies until the next expression or until the next promise type so it may apply to more than one promise at a time where ifvarclass applies only to one promise at a time. Also since 3.7.0 there is an if alias to ifvarclass which is nicer to type IMHO.

For simple class based constraints yes, they can be used interchangeably.

bundle agent main
{
  vars:
    "classes" slist => { "linux", "windows" };

  reports:

    "I am a Linux box"
      ifvarclass => "linux";

    linux::
      "I am still a Linux box";
}

R: I am a Linux box
R: I am still a Linux box

You can also use simple expressions interchangeably.

bundle agent main
{
  vars:
    "classes" slist => { "linux", "windows" };

  reports:

    "I am a Linux box"
      ifvarclass => "linux.64_bit";

    linux.64_bit::
      "I am still a Linux box";
}

R: I am a Linux box
R: I am still a Linux box

Until 3.7.0 you could not use variables in traditional context class expressions ($(my_variable)::) and ifvarclass was useful for being able to use a canonified variable as a constraint.

For example until 3.7 you had to do this:

bundle agent main
{
  vars:
    "classes" slist => { "linux", "windows" };

  reports:

    "I am $(classes) box"
      ifvarclass => "$(classes)";
}

R: I am linux box

As of 3.7 you can use variable class expressions:

bundle agent main
{
  vars:
    "classes" slist => { "linux", "windows" };

  reports:

    "$(classes)"::
      "I am $(classes) box";
}

R: I am linux box

That works fine until the list of things your checking contains characters that are not valid in classes (like dashes). This is where ifvarclass is most useful as it allows you to transform a string on the fly or even use a function that returns boolean like isvariable().

Here is an example showing ifvarclass in use with canonify().

bundle agent main
{
  classes:
    # A class that was derived from a string containing invalid characters like special-class
    "special_class" expression => "any";

  vars:
    "classes" slist => { "linux", "windows", "special-class" };

  reports:

    "I am $(classes) box"
      ifvarclass => canonify( $(classes) );
}

R: I am linux box
R: I am special-class box
Nick Anderson
  • 316
  • 1
  • 4