你如何将变量输出到 Ansible? [英] How do you output variables to Ansible?

查看:32
本文介绍了你如何将变量输出到 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?

推荐答案

在下面的代码中,我需要给出我在 terraform<中使用的变量而不是 var.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
  }
}

这篇关于你如何将变量输出到 Ansible?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆