How to use Variables in KOSI Packages
3 minute read
How to use Variables in KOSI Packages
KOSI plugins can produce internal variables during execution, which you can reference in subsequent steps of your package (for example in conditions or output messages). The following guide explains how to use variables set by various KOSI plugins and how to consume them using e.g. the if and fprint plugins (which allow conditional logic and formatted output, respectively).
Plugins that set Variables
Several plugins store their results in named variables. These include:
-
Firewall / Firewalld / IPTables – All three firewall plugins support a key like
getFirewallStatus = "<var>". For example:firewall ( type = "firewalld"; action = "enable"; getFirewallStatus = "status"; );This stores the firewall status (
"running"or"not running") into the variablestatus. You can then reference thisstatusvariable in later steps. -
Hostname – The
hostnameplugin can get the current hostname into a variable or set a new hostname from a variable. Its keys are:get = "<var>"to save the current hostname andsetVar = "<var>"to restore from a saved variable. For example:hostname(get = "oldHostname");This saves the machine’s current hostname into the variable
oldHostname. -
kubeadm – The
kubeadmplugin runs kubeadm commands. ItsoutputVar = "<var>"option captures the command’s output into a variable. For example:kubeadm ( operation = "version"; kubeadmVersion = "version"; outputVar = "kubeadmVersionOutput"; );This saves the output of
kubeadm versioninto the variablekubeadmVersionOutput. -
kubectl – The
kubectlplugin executes kubectl commands. ItsoutputVar = "<var>"captures the command output. For example:kubectl ( operation = "get"; resource = "pods"; flags = "-n kube-system -o wide"; outputVar = "podsOutput"; outputFile = "/root/output.txt"; );This saves the
kubectl get podsoutput intopodsOutput. You can then usepodsOutputin subsequent steps. -
osCheck – The
osCheckplugin detects the OS name and version. It has two keys:getOSVar="<var>"for the OS name andgetOSVersionVar="<var>"for the OS version. For example:osCheck(getOSVar="osName"; getOSVersionVar="osVersion");This stores the OS name in
osNameand the version inosVersion. -
set – The
setplugin lets you define arbitrary variables. Usevariable="<name>"; value="<something>"to create a variable. For example:set(variable = "envType"; value = "production");This creates a variable named
envTypewith value"production". Variables set by thesetplugin are accessed viavars.<variableName>.
Referencing Plugin Variables
KOSI provides two main ways to use these variables:
-
Conditional checks with the
ifplugin: Theifplugin evaluates an expression and branches accordingly. In theconditionstring, you can include plugin variables by name, enclosed in$...$. For example:if(condition = "$oldHostname$ = 'myHost'") then { # ... do something ... }For variables set via the
setplugin, access them as$vars.<name>$, e.g.$vars.envType$. -
Formatted output with the
fprintplugin: Thefprintplugin prints a message and can include plugin-variable values. You provide a list of variable names in itsvariableskey and placeholders{0},{1}, etc. in the message. For example:fprint ( message = "Firewall status is {0}"; variables = "['status']"; );For variables from the
setplugin:set(variable="userName"; value="Alice"); set(variable="userIP"; value="10.0.0.1"); fprint ( message = "User {0} has IP {1}"; variables = "['vars.userName','vars.userIP']"; );
Example Workflow
-
Set or retrieve a variable:
hostname(get = "myHostname"); osCheck(getOSVar = "osName"; getOSVersionVar = "osVersion"); -
Conditionally act on it:
if(condition = "$myHostname$ = 'expectedHost'") then { fprint ( message = "Got expected hostname: {0}"; variables = "['myHostname']"; ); } -
Output or log values:
fprint ( message = "Running on {0} version {1}"; variables = "['osName','osVersion']"; );
Related Plugin Documentation
Plugins mentioned in this How to Guide that set or use variables