我想自动创建 Artifactory 存储库。为此,我使用了 Atlasian artifcatory 插件,但我在为虚拟存储库声明本地存储库时遇到问题。
这是我的文件夹:
modules/
├── local-repository/
│ ├── main.tf
│ ├── variables.tf
├── virtual-repository/
│ ├── main.tf
│ └──variables.tf
│── local-repository-xxx.tf
│── virtual-repository-xxx.tf
│── variables.tf
│──provider.tf
│── version.tf
本地存储库.tf :
resource "artifactory_local_repository" "local-repository" {
key = var.key
package_type = var.type
}
虚拟仓库:
resource "artifactory_virtual_repository" "virtual-repository" {
key = var.key
package_type = var.type
repositories = [var.repositories]
}
我想先创建所有本地存储库,然后我们要创建虚拟存储库。
在这种情况下我如何使用 call depend_on ?
谢谢
最佳答案
当您通过输出值从模块返回数据或通过输入变量将数据传递给模块时,值本身及其依赖项都会通过。
如果您使用对要依赖的对象的引用来填充子模块的变量,则可以避免在这里完全使用 depends_on
。例如:
resource "artifactory_local_repository" "local-repository" {
key = var.key
package_type = var.type
}
module "virtual_repository" {
source = "./virtual-repository"
# ...
# If you create the repositories value by referring to
# artifactory_local_repository.local-repository then
# any resource in the module that refers to
# var.repositories will in turn depend on
# artifactory_local_repository.local-repository
# automatically.
repositories = artifactory_local_repository.local-repository[*].id
}
在引用未自动指定所有依赖项的异常情况下,您可以在 depends_on 中使用输入变量和输出值。例如:
# Using var.repositories in a depends_on
# means that the resource will depend on
# whatever var.repositories depends on, even
# if it doesn't actually use the value of
# the variable.
depends_on = [var.repositories]
# Using module.local_repository.id in
# a depends_on means that the resource will
# depend on whatever the output "id"
# declaration in the module depends on.
depends_on = [module.local_repository.id]
在更不寻常的情况下,输出本身需要携带通常不会有的依赖项,您可以在 output< 中使用
block :depends_on
/
output "id" {
value = "anything"
depends_on = [artifactory_local_repository.local-repository]
}
仅引用其他对象并让 Terraform 自动计算依赖关系几乎总是可能且更好,因为这样依赖关系将随着模块的发展自动保持正确,并且您不必编写大量额外的依赖关系注释。 depends_on
仅适用于“隐藏”到 Terraform 的异常情况,因为两个对象之间的关系未被这些对象的参数捕获。 depends_on
是最后的手段。
关于Terraform depend_on 另一个路径中的另一个模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62638949/