概要
Terraformのバージョンを切り替えて便利に使えるようになるtfenv
HOMEディレクトリにインストールして自分用に使うのが楽だと思うけど、複数ユーザーで使う方法を調べてみた。
用意したもの
※多分、AlmaLinux 8.6でもCentOS 7.9でも同じはず
依存パッケージ追加
sudo yum install unzip
tfenvインストール
tfenv実行用のユーザーを作って /opt/tfenv/
にインストールする。
sudo useradd tfenv
sudo usermod -s /sbin/nologin -L tfenv
sudo mkdir /opt/tfenv
sudo chown tfenv: /opt/tfenv
sudo -u tfenv git clone https://github.com/tfutils/tfenv.git /opt/tfenv
sudo ln -s /opt/tfenv/bin/* /usr/local/bin/
インストール確認、こんな感じでUsageが表示されるはず
[user1@test-al9 ~]$ tfenv
tfenv 3.0.0-18-g1ccfddb
Usage: tfenv <command> [<options>]
Commands:
install Install a specific version of Terraform
use Switch a version to use
uninstall Uninstall a specific version of Terraform
list List all installed versions
list-remote List all installable versions
version-name Print current version
init Update environment to use tfenv correctly.
pin Write the current active version to ./.terraform-version
terraformインストールとバージョン切り替え
※tfenv installは問題ないけど、tfenv listやuseは内部でfindを呼ぶので、カレントディレクトリにtfenv実行ユーザーの実行権がないとPermission deniedが出る。
[user1@test-al9 ~]$ cd
[user1@test-al9 ~]$ sudo -u tfenv /opt/tfenv/bin/tfenv install latest
Installing Terraform v1.3.3
Downloading release tarball from https://releases.hashicorp.com/terraform/1.3.3/terraform_1.3.3_linux_amd64.zip
################################################################################################################# 100.0%
Downloading SHA hash file from https://releases.hashicorp.com/terraform/1.3.3/terraform_1.3.3_SHA256SUMS
Not instructed to use Local PGP (/opt/tfenv/use-{gpgv,gnupg}) & No keybase install found, skipping OpenPGP signature verification
terraform_1.3.3_linux_amd64.zip: 完了
Archive: /tmp/tfenv_download.C7be9f/terraform_1.3.3_linux_amd64.zip
inflating: /opt/tfenv/versions/1.3.3/terraform
Installation of terraform v1.3.3 successful. To make this your default version, run 'tfenv use 1.3.3'
[user1@test-al9 ~]$ sudo -u tfenv /opt/tfenv/bin/tfenv use latest
find: Failed to change directory: /home/user1: 許可がありません
find: Failed to change directory: /home/user1: 許可がありません
find: Failed to restore initial working directory: /home/user1: 許可がありません
No installed versions of terraform matched 'latest:^[0-9]\+\.[0-9]\+\.[0-9]\+$'. Trying to install a matching version since TFENV_AUTO_INSTALL=true
Terraform v1.3.3 is already installed
find: Failed to change directory: /home/user1: 許可がありません
find: Failed to change directory: /home/user1: 許可がありません
find: Failed to restore initial working directory: /home/user1: 許可がありません
Despite successfully installing a version matching 'latest:^[0-9]\+\.[0-9]\+\.[0-9]\+$', a matching version could not be found in '/opt/tfenv/versions/' - This should be pretty much impossible
cd /tmp
すれば問題ない
[user1@test-al9 ~]$ sudo -u tfenv find -maxdepth 0 > /dev/null
find: ‘.’: 許可がありません
find: Failed to restore initial working directory: /home/user1: 許可がありません
[user1@test-al9 ~]$ cd /tmp
[user1@test-al9 tmp]$ sudo -u tfenv find -maxdepth 0 > /dev/null
[user1@test-al9 tmp]$
[user1@test-al9 ~]$ cd /tmp
[user1@test-al9 tmp]$ sudo -u tfenv /opt/tfenv/bin/tfenv use latest
Switching default version to v1.3.3
Default version (when not overridden by .terraform-version or TFENV_TERRAFORM_VERSION) is now: 1.3.3
tfenvの使い方メモ
自分のユーザーにだけ影響する操作
インストール済みTerraformのバージョン確認
tfenv list
実行例
[user1@test-al9 ~]$ tfenv list
* 1.3.3 (set by /opt/tfenv/version)
1.0.0
一時的にインストール済みの別バージョンのterraformを使う
TFENV_TERRAFORM_VERSION=バージョン terraform
実行例
[user1@test-al9 ~]$ tfenv list
* 1.3.3 (set by /opt/tfenv/version)
1.0.0
[user1@test-al9 ~]$ terraform version
Terraform v1.3.3
on linux_amd64
[user1@test-al9 ~]$ TFENV_TERRAFORM_VERSION=1.0.0 terraform version
Terraform v1.0.0
on linux_amd64
Your version of Terraform is out of date! The latest version
is 1.3.3. You can update by downloading from https://www.terraform.io/downloads.html
一時的に自分用に別バージョンのterraformをインストールして使う
TFENV_CONFIG_DIR=~/tfenv/ TFENV_TERRAFORM_VERSION=バージョン terraform
実行例
[user1@test-al9 ~]$ TFENV_CONFIG_DIR=~/tfenv/ TFENV_TERRAFORM_VERSION=latest:^1.2. terraform version
version '1.2.9' is not installed (set by TFENV_TERRAFORM_VERSION). Installing now as TFENV_AUTO_INSTALL==true
Installing Terraform v1.2.9
Downloading release tarball from https://releases.hashicorp.com/terraform/1.2.9/terraform_1.2.9_linux_amd64.zip
################################################################################################################# 100.0%
Downloading SHA hash file from https://releases.hashicorp.com/terraform/1.2.9/terraform_1.2.9_SHA256SUMS
Not instructed to use Local PGP (/home/user1/tfenv/use-{gpgv,gnupg}) & No keybase install found, skipping OpenPGP signature verification
terraform_1.2.9_linux_amd64.zip: 完了
Archive: /tmp/tfenv_download.qnXlGI/terraform_1.2.9_linux_amd64.zip
inflating: /home/user1/tfenv/versions/1.2.9/terraform
Installation of terraform v1.2.9 successful. To make this your default version, run 'tfenv use 1.2.9'
Terraform v1.2.9
on linux_amd64
Your version of Terraform is out of date! The latest version
is 1.3.3. You can update by downloading from https://www.terraform.io/downloads.html
[user1@test-al9 ~]$ TFENV_CONFIG_DIR=~/tfenv tfenv list
1.2.9
No default set. Set with 'tfenv use <version>'
[user1@test-al9 ~]$ TFENV_CONFIG_DIR=~/tfenv tfenv use latest:^1.2
Switching default version to v1.2.9
Default version (when not overridden by .terraform-version or TFENV_TERRAFORM_VERSION) is now: 1.2.9
[user1@test-al9 ~]$ TFENV_CONFIG_DIR=~/tfenv tfenv list
* 1.2.9 (set by /home/user1/tfenv/version)
全ユーザーに影響するので専用ユーザーで実行する操作
terraformの最新バージョンをインストール
sudo -u tfenv /opt/tfenv/bin/tfenv install latest
terraformの特定バージョンをインストール
sudo -u tfenv /opt/tfenv/bin/tfenv install バージョン
実行例
[user1@test-al9 ~]$ sudo -u tfenv /opt/tfenv/bin/tfenv install 1.0.0
Installing Terraform v1.0.0
Downloading release tarball from https://releases.hashicorp.com/terraform/1.0.0/terraform_1.0.0_linux_amd64.zip
################################################################################################################# 100.0%
Downloading SHA hash file from https://releases.hashicorp.com/terraform/1.0.0/terraform_1.0.0_SHA256SUMS
Not instructed to use Local PGP (/opt/tfenv/use-{gpgv,gnupg}) & No keybase install found, skipping OpenPGP signature verification
terraform_1.0.0_linux_amd64.zip: 完了
Archive: /tmp/tfenv_download.ktVuVp/terraform_1.0.0_linux_amd64.zip
inflating: /opt/tfenv/versions/1.0.0/terraform
Installation of terraform v1.0.0 successful. To make this your default version, run 'tfenv use 1.0.0'
terraformのバージョン切り替え
cd /tmp
sudo -u tfenv /opt/tfenv/bin/tfenv use バージョン
実行例
[user1@test-al9 ~]$ tfenv list
* 1.3.3 (set by /opt/tfenv/version)
1.0.0
[user1@test-al9 ~]$ cd /tmp
[user1@test-al9 tmp]$ sudo -u tfenv /opt/tfenv/bin/tfenv use 1.0.0
Switching default version to v1.0.0
Default version (when not overridden by .terraform-version or TFENV_TERRAFORM_VERSION) is now: 1.0.0
[user1@test-al9 tmp]$ tfenv list
1.3.3
* 1.0.0 (set by /opt/tfenv/version)
terraformの不要なバージョンを削除
cd /tmp
sudo -u tfenv /opt/tfenv/bin/tfenv uninstall バージョン
実行例
[user1@test-al9 ~]$ tfenv list
1.3.3
* 1.0.0 (set by /opt/tfenv/version)
[user1@test-al9 ~]$ cd /tmp
[user1@test-al9 tmp]$ sudo -u tfenv /opt/tfenv/bin/tfenv uninstall 1.0.0
Uninstall Terraform v1.0.0
Terraform v1.0.0 is successfully uninstalled
[user1@test-al9 tmp]$ tfenv list
1.3.3
No default set. Set with 'tfenv use <version>'