您如何将变量输出到Ansible? [英] How do you output variables to 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
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?
推荐答案
在下面的代码中,我需要提供在terraform
中使用的变量,而不是var.share_location
${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
}
}
这篇关于您如何将变量输出到Ansible?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!