Alliot's blog

asdf管理多版本Terraform

  asdf-vm 是一个管理多版本运行环境的 CLI 工具,有点类似多 nodejs 版本管理工具 n,相比其他类型的工具,asdf-vm 以插件形式支持了众多的语言与开发工具,支持全局配置,同时也支持通过 .tool-versions 配置文件以项目维度来管理工具版本(类似前端项目下的 packages.json)。我们在使用 Terraform 的过程中,也经常遇到要切换不同 terraform 版本的情况,asdf-vm 就能够满足我们的需求。

安装asdf

  需要 curlgit 依赖:

1
2
3
4
5
6
7
8
9
10
# redhat/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-plugins

1
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

安装指定版本Terraform

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 列出可用terraform版本  
asdf list-all terraform

# 安装指定版本
asdf install terraform 0.11.14

# 设置全局版本, 该参数会在当前用户HOME下生成 .tool-version 文件来标识版本
asdf global terraform 0.12.5

# 如果只是为了在某路径下使用特定版本,可以通过local参数来指定,该参数会在当前目录下生成 .tool-version
# asdf local terraform 0.12.5
# 如果项目下已有 .tool-version,可以直接执行 asdf install来自动安装指定版本的工具

# 如果只是为了当前shell下临时切换,可以通过shell参数来指定
# asdf shell terraform 0.12.5

# 查看当前版本(可能需要你重新登录终端生效)
asdf current

# 卸载asdf指定的terraform版本 (区别于remove,uninstall仅仅是去掉环境变量,让你可以用其他途径安装的terraform)
asdf uninstall terraform
------ 本文结束 ------

本文标题:asdf管理多版本Terraform

文章作者:Alliot

发布时间:2022年06月08日 - 18:06

最后更新:2023年05月21日 - 00:05

原始链接:https://www.iots.vip/post/asdf-multi-version-terraform.html

许可协议: 署名-非商业性使用-相同方式共享 4.0 国际 转载请保留原文链接及作者。

若文章为您解决燃眉之急或是带来些许明朗,不妨打赏 Alliot 一杯香茗或是一杯咖啡