asdf管理多版本Terraform
asdf-vm 是一个管理多版本运行环境的 CLI 工具,有点类似多 nodejs
版本管理工具 n
,相比其他类型的工具,asdf-vm
以插件形式支持了众多的语言与开发工具,支持全局配置,同时也支持通过 .tool-versions
配置文件以项目维度来管理工具版本(类似前端项目下的 packages.json
)。我们在使用 Terraform 的过程中,也经常遇到要切换不同 terraform
版本的情况,asdf-vm
就能够满足我们的需求。
安装asdf
需要 curl
与 git
依赖:1
2
3
4
5
6
7
8
9
10redhat/centos
yum install curl git
debian/Ubuntu
apt install curl git
git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.10.0
echo '. $HOME/.asdf/asdf.sh' >> ~/.bashrc
echo '. $HOME/.asdf/completions/asdf.bash' >> ~/.bashrc # 自动补全
安装asdf plugin
Hashicorp 的 asdf
插件仓库地址: https://github.com/asdf-community/asdf-hashicorp
更多其他插件可以从官方仓库查找: https://github.com/asdf-vm/asdf-plugins1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16按需添加插件:
asdf plugin-add boundary https://github.com/asdf-community/asdf-hashicorp.git
asdf plugin-add consul https://github.com/asdf-community/asdf-hashicorp.git
asdf plugin-add levant https://github.com/asdf-community/asdf-hashicorp.git
asdf plugin-add nomad https://github.com/asdf-community/asdf-hashicorp.git
asdf plugin-add packer https://github.com/asdf-community/asdf-hashicorp.git
asdf plugin-add sentinel https://github.com/asdf-community/asdf-hashicorp.git
asdf plugin-add serf https://github.com/asdf-community/asdf-hashicorp.git
asdf plugin-add terraform https://github.com/asdf-community/asdf-hashicorp.git
asdf plugin-add terraform-ls https://github.com/asdf-community/asdf-hashicorp.git
asdf plugin-add tfc-agent https://github.com/asdf-community/asdf-hashicorp.git
asdf plugin-add vault https://github.com/asdf-community/asdf-hashicorp.git
asdf plugin-add waypoint https://github.com/asdf-community/asdf-hashicorp.git
我们本文只需要terraform:
asdf plugin-add terraform https://github.com/asdf-community/asdf-hashicorp.git
假设你是在一个已经存在 .tools-version
文件的仓库下,而你又没有安装过那些 asdf plugin
,那么你可以直接执行如下命令来安装 .tools-version
中依赖的插件:1
while read plugin version; do asdf plugin-add "${plugin}"; done < .tool-versions
之后执行以下命令安装 .tools-version
中的所有工具即可:1
asdf install
安装指定版本Terraform
1 | 列出可用terraform版本 |