メインコンテンツへスキップ

基本的なCLI機能

OpenTofuへのコマンドラインインターフェースは、tofuコマンドです。これは、tofu inittofu 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が元の作業ディレクトリを使用する例外が2つあります。

  • CLI構成の設定は、特定のサブコマンド用ではなく、OpenTofuは-chdirオプションを処理する前にこれらの設定を処理します。

  • 構成の一部として元の作業ディレクトリのファイルを使用する必要がある場合、構成内のpath.cwdへの参照は、オーバーライドされた作業ディレクトリではなく、元の作業ディレクトリを生成します。ルートモジュールディレクトリを取得するには、path.rootを使用します。

シェルタブ補完

コマンドシェルとしてbashまたはzshのいずれかを使用している場合、OpenTofuはすべてのコマンド名と一部のコマンド引数に対してタブ補完のサポートを提供できます。

必要なコマンドをシェルプロファイルに追加するには、次のコマンドを実行します。

コードブロック
tofu -install-autocomplete

インストール後、補完を有効にする前に、シェルを再起動するか、プロファイルスクリプトを再読み込みする必要があります。

補完フックをアンインストールするには、シェルプロファイルで手動で変更されていないことを前提として、次のコマンドを実行します。

コードブロック
tofu -uninstall-autocomplete