【问题标题】:When running import "tfconfig" with sentinel I get import tfconfig is not available使用 sentinel 运行 import "tfconfig" 时,我得到 import tfconfig is not available
【发布时间】:2019-08-23 04:48:37
【问题描述】:

我正在学习 Terraform 和 sentinel。

我无法使某些基本功能正常工作。

我在这里有一个政策:

import "tfconfig"

default_foo = rule { tfconfig.variables.foo.default is "bar" }
default_number = rule { tfconfig.variables.number.default is 42 }

main = rule { default_foo and default_number }

这里还有一个变量文件:

variable "foo" {
  default = "bar"
}

variable "number" {
  default = 42
}

但是当我跑步时:

sentinel apply policy.sentinel 

我收到以下错误:

policy.sentinel:1:1: Import "tfconfig" is not available.

我一直在寻找解决方案几个小时的任何想法。

谢谢

【问题讨论】:

  • 我在想这可能只有在 Terraform Enterprise 上直接运行这些命令时才有效?

标签: terraform hashicorp-sentinel


【解决方案1】:

为了在 Sentinel SDK 中使用特定于 Terraform 的导入,您需要使用 mock data 生成数据结构以进行测试。

当您通过 Terraform Cloud 运行 Terraform 时,成功的计划将生成一个 Sentinel 模拟文件,其中包含 Terraform Cloud 在针对该计划评估策略时自己使用的相同数据,因此您可以将该模拟数据签入您的存储库策略测试套件的一部分。

您可以使用speculative plans(在启用了远程后端的命令行上运行terraform plan)为要测试策略的故意无效配置创建模拟数据,而无需将这些无效配置推送到你的版本控制系统。

您可以对JSON definitions 包含引用这些模拟文件的mock 对象的测试用例使用sentinel test,然后这些测试用例评估的策略将能够导入tfconfig、tfplan 和tfstate 并获得与策略是否在 Terraform Cloud 中针对原始计划运行的等效结果。

【讨论】:

  • 你可以通过运行terraform plan -out some.tfplan然后terraform show -json some.tfplan在本地生成计划JSON。
  • 您可以通过运行 terraform plan -out some.tfplan 然后 terraform show -json some.tfplan 在本地生成计划 JSON,尽管它似乎与 TF Cloud 模拟导出的格式不太一样。
【解决方案2】:

我有我的模拟数据和所有内容,但我仍然收到此错误。

运行策略时出现运行时错误: ensure-policy.sentinel:3:1:导入“tfplan”不可用

我意识到我忘记了我的政策目录根目录下的“sentinel.json”文件,它告诉哨兵在哪里寻找模拟数据

https://www.terraform.io/docs/cloud/sentinel/mock.html

这是他们推荐默认目录设置的方式,但您仍然需要该 .json 文件来告诉哨兵在哪里查看 这是 sentinel.json 文件的样子

【讨论】:

    猜你喜欢
    • 2020-08-25
    • 2022-12-28
    • 2022-08-13
    • 2019-12-17
    • 2021-01-29
    • 2019-10-13
    • 2020-11-30
    • 1970-01-01
    • 2021-03-24
    相关资源
    最近更新 更多