hostname-1.7.0

KOSI Plugin hostname Version 1.7.0

Summary

The hostname plugin for KOSI allows users to manage and modify the hostname of a machine dynamically. It provides functionalities to:

  • Retrieve and store the current hostname in an internal variable.
  • Permanently set a new hostname.
  • Use a previously stored hostname variable to restore the hostname.
  • Optionally execute hostname changes with elevated privileges (sudo).

This plugin is particularly useful in automated deployments, system provisioning, or scenarios where hostnames need to be dynamically managed during a KOSI package installation.

Keys

Key Required Description
get Optional* Retrieves the current hostname and saves it in a variable for later use. This variable remains accessible until the KOSI session ends.
set Optional* Sets the machine’s hostname to the specified value.
setVar Optional* Sets the hostname using a previously stored variable from the internal storage.
sudo Optional If set to true, the plugin will execute commands with sudo privileges.
sudoPassword Yes if sudo is true The password required for executing sudo commands.

Note:

  • * One of the keys must be present. (Either get, set or setVar)
  • The sudoPassword is mandatory if sudo is enabled, ensuring the required privileges for modifying system settings.

Examples

Example 1 - Retrieve and Store the Current Hostname

This example retrieves the current hostname and saves it in the internal variable oldHostname. The variable can be used later within the same KOSI session.

languageversion = "1.0.0";
apiversion = "kubernative/kubeops/sina/user/v4";
name = "kosi-example-packagev3";
description = "kosi-example-package";
version = "0.1.0";
docs = "docs.tgz";
logo = "logo.png";

files =
{
    input = "template.yaml";
}

install
{
    hostname
    (
        get = "oldHostname";
        sudo = "true";
        sudoPassword = "YourSecurePassword";
    );
}

Example 2 - Set a New Hostname

This example sets the machine’s hostname to master permanently.

languageversion = "1.0.0";
apiversion = "kubernative/kubeops/sina/user/v4";
name = "kosi-example-packagev3";
description = "kosi-example-package";
version = "0.1.0";
docs = "docs.tgz";
logo = "logo.png";

files =
{
    input = "template.yaml";
}

install
{
    hostname
    (
        set = "master";
        sudo = "true";
        sudoPassword = "YourSecurePassword";
    );
}

After execution, the system’s hostname will be permanently changed to master.

Example 3 - Restore a Previously Stored Hostname

If a hostname was previously retrieved and stored in oldHostname, this example restores it.

languageversion = "1.0.0";
apiversion = "kubernative/kubeops/sina/user/v4";
name = "kosi-example-packagev3";
description = "kosi-example-package";
version = "0.1.0";
docs = "docs.tgz";
logo = "logo.png";

files =
{
    input = "template.yaml";
}

install
{
    hostname
    (
        setVar = "oldHostname";
        sudo = "true";
        sudoPassword = "YourSecurePassword";
    );
}

This is particularly useful in rollback scenarios where a hostname needs to be reverted to its original value after temporary changes.

Example 4 - Using hostname Plugin with other plugins

Using hostname with the if and print plugins

This example shows the interaction of several plugins. In this example, we use the hostname plugin together with the if and print plugins to check whether it is the correct hostname and to output a specific message and perform a corresponding action:

languageversion = "1.0.0";
apiversion = "kubernative/kubeops/sina/user/v4";
name = "kosi-example-hostname-if-print";
description = "Example using hostname with if and print";
version = "0.1.0";
docs = "docs.tgz";
logo = "logo.png";

files =
{
    input = "template.yaml";
}

install
{
    hostname(get = "oldHostname");

    if (condition = "$oldHostname$ = 'myHostname'") then
    {
        print(message = "The hostname myHostname should not be taken, so the hostname is changed to myVeryVeryNewHostname..");

        hostname
        (
            set = "myVeryVeryNewHostname";
            sudo = "true";
            sudoPassword = "SecurePassword";
        );
    }
    else
    {
        print(message = "The hostname is OK, do not make any changes.");
    }
}

Expected Behavior

If hostname is myHostname:

2025-02-26 15:18:28 Info:      The hostname myHostname should not be taken, so the hostname is changed to myVeryVeryNewHostname..
  • The hostname is changed to myVeryVeryNewHostname.

Otherwise:

2025-02-26 15:18:28 Info:      The hostname is OK, do not make any changes.
  • As we have not specified anything else in the else part of the if plugin, nothing else happens in the else part.