This is the multi-page printable view of this section. Click here to print.

Return to the regular view of this page.

Modules

Komandan has several built-in modules that can be used to perform various tasks on the target server.

1 - cmd

The cmd module allows you to execute a shell command on the target server.

Arguments

  • cmd: a string that contains the shell command to be executed.

Usage Example

local task = {
    name = "Execute a command",
    komandan.modules.cmd({
        cmd = "ls -l"
    })
}

komandan.komando(host, task)

2 - script

The script module allows you to execute a script on the target server.

Arguments

  • script: a string that contains the script to be executed.
  • from_file: a string that contains the local path to the script file to be executed on the target server. (script and from_file parameters are mutually exclusive)
  • interpreter: a string that specifies the interpreter to use for the script. If not specified, the script will be executed using the default shell.

Usage Example

local task = {
    name = "Execute a script",
    komandan.modules.script({
        script = "print('Hello from script')",
        interpreter = "python"
    })
}

komandan.komando(host, task)

3 - upload

The upload module allows you to upload a file to the target server.

Arguments

  • src: a string that contains the path to the file to be uploaded.
  • dst: a string that contains the path to the destination file on the target server.

Usage Example

local task = {
    name = "Upload a file",
    komandan.modules.upload({
        src = "/path/to/local/file",
        dst = "/path/to/remote/file"
    })
}

komandan.komando(host, task)

4 - download

The download module allows you to download a file from the target server.

Arguments

  • src: a string that contains the path to the file to be downloaded.
  • dst: a string that contains the path to the destination file on the local machine.

Usage Example

local task = {
    name = "Download a file",
    komandan.modules.download({
        src = "/path/to/remote/file",
        dst = "/path/to/local/file"
    })
}

komandan.komando(host, task)

5 - apt

The apt module allows you to install an APT package on the target server.

Arguments

  • package: a string that contains the name of the package to be installed.
  • action: a string that specifies the action to be taken on the package. (default is install. Supported actions: install, remove, purge, upgrade, autoremove)
  • update_cache: a boolean that indicates whether to update the package cache before installing the package. (default is false)
  • install_recommends: a boolean that indicates whether to install recommended packages. (default is true)

Usage Example

local task = {
    name = "Install apache2",
    komandan.modules.apt({
        package = "apache2",
        update_cache = true
    })
}

komandan.komando(host, task)

6 - lineinfile

The lineinfile module allows you to modify a file by adding or removing lines.

Arguments

  • path: a string that contains the path to the file to be modified.
  • line: a string that contains the line to be added or removed.
  • state: a string that contains the state of the line. Can be “present” or “absent”. (default is “present”)
  • pattern: a string that contains the pattern (regular expression) to search for in the file. (optional)
  • insert_after: a string that contains the line to insert the new line after. (optional)
  • insert_before: a string that contains the line to insert the new line before. (optional)
  • create: a boolean that indicates whether to create the file if it does not exist. (default is false)
  • backup: a boolean that indicates whether to create a backup of the file before modifying it. (default is false)

Usage Example

local task = {
    name = "Add a line in a file",
    komandan.modules.lineinfile({
        path = "/tmp/target_file.txt",
        line = "This is a new line",
        state = "present",
        create = true,
        backup = true,
    }),
}

7 - template

The template module allows you to create a file using a jinja template.

Arguments

  • src: a string that contains the path to the template file on the local machine.
  • dst: a string that contains the path to the destination file on the target server.
  • vars: a table that contains the variables to be used in the template.

Usage Example

local task = {
    name = "Create a file using a template",
    komandan.modules.template({
        src = "/path/to/template.jinja",
        dst = "/tmp/target_file.txt",
        vars = {
            name = "John Doe",
            age = 30,
        },
    }),
}