Module Helper Functions

Komandan provides several helper functions that can be used to send commands or other actions to the target server from a custom module. (see Create a Custom Module)

self.ssh:cmd

This function sends a command to the target server using SSH. The function takes one arguments:

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

The function returns a table with the following fields:

  • exit_code: an integer that contains the exit code of the command.
  • stdout: a string that contains the standard output of the command.
  • stderr: a string that contains the standard error of the command.

self.ssh:cmdq

This function sends a command to the target server using SSH with quiet mode, so the output will not be recorded in the session results.

The function takes one arguments:

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

The function returns a table with the following fields:

  • exit_code: an integer that contains the exit code of the command.
  • stdout: a string that contains the standard output of the command.
  • stderr: a string that contains the standard error of the command.

self.ssh:requires

This function checks if commands are available on the target server. The function takes one argument:

  • cmds: a string or table that contains the commands to be checked.

The function does not return any value and will throw an error if any of the commands are not available.

self.ssh:write_remote_file

This function writes a file to the target server using SSH. The function takes two arguments:

  • remote path: a string that contains the path to the destination file on the target server.
  • content: a string that contains the content to be written to the file.

The function does not return any value.

self.ssh:upload

This function uploads a file to the target server using SSH. The function takes two arguments:

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

The function does not return any value.

self.ssh:download

This function downloads a file from the target server using SSH. The function takes two arguments:

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

The function does not return any value.

self.ssh:get_remote_env

This function gets the environment variables from the target server using SSH. The function takes one argument:

  • var: a string that contains the name of the environment variable to be retrieved.

The function returns a string that contains the value of the environment variable.

self.ssh:get_tmpdir

This function returns the path to the temporary directory for Komandan on the target server. The function does not take any arguments. The function returns a string that contains the path to the temporary directory. The default tmpdir is $HOME/.komandan/tmp, otherwise it will be /tmp/komandan.

self.ssh:chmod

This function changes the permissions of a file on the target server using SSH. The function takes two arguments:

  • remote_path: a string that contains the path to the file to be changed.
  • mode: a string that contains the permissions to be set.

self.ssh:set_changed

This function sets the changed status of the module result. The function takes one argument:

  • changed: a boolean that indicates whether the system on the target server is changed or not.

The function does not return any value.