你如何将变量输出到 Ansible?
我在此问题中遵循了此处的答案我创建了一个 template
文件 tf_ansible_vars_file.yml.tpl
如下
I followed the answer here in this question and
I have created a template
file tf_ansible_vars_file.yml.tpl
like below
tf_share_location: "${share_location}"
和一个 terra_render.tf
如下所示
# Define an Ansible var_file containing Terraform variable values
data "template_file" "tf_ansible_vars_file" {
template = "${file("/home/deployment_root/app4/tf_ansible_vars_file.yml.tpl")}"
vars = {
share_location = var.share_location
# gitlab_backup_bucket_name = aws_s3_bucket.gitlab_backup.bucket
}
}
# Render the Ansible var_file containing Terrarorm variable values
resource "local_file" "tf_ansible_vars_file" {
content = data.template_file.tf_ansible_vars_file.rendered
filename = "/home/deployment_root/app4/tf_ansible_vars_file.yml"
}
我已经有一个 variables.tf
文件,我在其中声明了该变量
I already have a variables.tf
file in which i have declared that variable
variable "share_location" {
type = string
}
并且在 terraform.tfvars
中给出的值为 null
and in the terraform.tfvars
gave the value as null
share_location = null
当我运行 terraform apply
时,出现以下错误
when i run terraform apply
i get the below error
Error: failed to render : <template_file>:1,23-37: Unknown variable; There is no variable named "share_location".
on terra_render.tf line 2, in data "template_file" "tf_ansible_vars_file":
2: data "template_file" "tf_ansible_vars_file" {
我的理解是它会创建该答案中提到的文件,但它不起作用.
My understanding is it will create a file as mentioned in that answer, but it is not working.
如何将变量输出到 Ansible?
How do you output variables to Ansible?
在下面的代码中,我需要给出我在 terraformvar.share_location
/code> 就我而言${data.azurerm_storage_account.new.name}/sharename
之后我可以从 variables.tf
以及 terraform.tfvars
中删除它因为我得到了产生的价值.谢谢
In the below code, instead of var.share_location
, i need to give the variable that i used in terraform
in my case
${data.azurerm_storage_account.new.name}/sharename
and after that i can remove that from variables.tf
as well as terraform.tfvars
as i am getting the value generated. Thanks
旧代码:
data "template_file" "tf_ansible_vars_file" {
template = "${file("/home/deployment_root/app4/tf_ansible_vars_file.yml.tpl")}"
vars = {
share_location = var.share_location
# gitlab_backup_bucket_name = aws_s3_bucket.gitlab_backup.bucket
}
}
新代码:
# Define an Ansible var_file containing Terraform variable values
data "template_file" "tf_ansible_vars_file" {
template = "${file("/home/deployment_root/app4/tf_ansible_vars_file.yml.tpl")}"
vars = {
share_location = "${data.azurerm_storage_account.new.name}/sharename"
# gitlab_backup_bucket_name = aws_s3_bucket.gitlab_backup.bucket
}
}