- 命令行
- 基本 CLI 功能
基本 CLI 功能
OpenTofu 的命令行界面是 tofu 命令,它接受各种子命令,例如 tofu init 或 tofu plan。
要查看当前 OpenTofu 版本中可用的命令列表,请运行 tofu 而不带任何其他参数
Usage: tofu [global options] <subcommand> [args]
The available commands for execution are listed below.
The primary workflow commands are given first, followed by
less common or more advanced commands.
Main commands:
  init          Prepare your working directory for other commands
  validate      Check whether the configuration is valid
  plan          Show changes required by the current configuration
  apply         Create or update infrastructure
  destroy       Destroy previously-created infrastructure
All other commands:
  console       Try OpenTofu expressions at an interactive command prompt
  fmt           Reformat your configuration in the standard style
  force-unlock  Release a stuck lock on the current workspace
  get           Install or upgrade remote OpenTofu modules
  graph         Generate a Graphviz graph of the steps in an operation
  import        Associate existing infrastructure with a OpenTofu resource
  login         Obtain and save credentials for a remote host
  logout        Remove locally-stored credentials for a remote host
  metadata      Metadata related commands
  output        Show output values from your root module
  providers     Show the providers required for this configuration
  refresh       Update the state to match remote systems
  show          Show the current state or a saved plan
  state         Advanced state management
  taint         Mark a resource instance as not fully functional
  untaint       Remove the 'tainted' state from a resource instance
  version       Show the current OpenTofu version
  workspace     Workspace management
Global options (use these before the subcommand, if any):
  -chdir=DIR    Switch to a different working directory before executing the
                given subcommand.
  -help         Show this help output, or the help for a specified subcommand.
  -version      An alias for the "version" subcommand.
(您当前 OpenTofu 版本的输出可能与上述示例不同。)
要获取任何特定命令的具体帮助,请将 -help 选项与相关子命令一起使用。例如,要查看有关“validate”子命令的帮助,您可以运行 tofu validate -help。
OpenTofu CLI 中内置的内联帮助描述了每个命令最重要的特性。有关更详细的信息,请参阅每个命令的页面以了解详细信息。
使用 -chdir 切换工作目录
运行 OpenTofu 的通常方法是首先切换到包含根模块的 .tf 或 .tofu 文件的目录(例如,使用 cd 命令),以便 OpenTofu 可以在没有任何额外参数的情况下自动找到这些文件。
但在某些情况下,尤其是在将 OpenTofu 包裹在自动化脚本中时,从与根模块目录不同的目录运行 OpenTofu 可能很方便。为了允许这样做,OpenTofu 支持一个全局选项 -chdir=...,您可以在要运行的子命令名称之前包含它
tofu -chdir=environments/production apply
chdir 选项指示 OpenTofu 在运行给定子命令之前将其工作目录更改为给定目录。这意味着 OpenTofu 通常在当前工作目录中读取或写入的任何文件都将在给定目录中读取或写入。
在您指定 -chdir=... 时,有两个例外情况 OpenTofu 将使用原始工作目录。
- 
中的设置不适用于特定子命令,并且 OpenTofu 在对 -chdir选项采取行动之前会处理它们。
- 
如果您需要将原始工作目录中的文件用作配置的一部分,则配置中的 path.cwd引用将生成原始工作目录而不是覆盖的工作目录。使用path.root获取根模块目录。
Shell Tab 自动补全
如果您使用 bash 或 zsh 作为您的命令 shell,OpenTofu 可以为所有命令名称和一些命令参数提供 Tab 自动补全支持。
要将必要的命令添加到您的 shell 配置文件中,请运行以下命令
tofu -install-autocomplete
安装后,需要重新启动 shell 或重新读取其配置文件,然后才会激活自动补全功能。
要卸载自动补全钩子,假设它在 shell 配置文件中未手动修改,请运行以下命令
tofu -uninstall-autocomplete