How do you output variables to Ansible? [duplicate]

ぃ、小莉子 提交于 2020-05-17 06:45:12

问题


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}"

and a terra_render.tf like below

# 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"
}

I already have a variables.tf file in which i have declared that variable

variable "share_location" {
    type = string

}

and in the terraform.tfvars gave the value as null

share_location = null

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.

How do you output variables to Ansible?


回答1:


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

Old code :

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
  }
}

New Code:

# 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
  }
}



回答2:


If you are generating a supported format like YAML, you do not need a template file.

You can generate YAML directly from Terraform data structures as follows:

resource "local_file" "tf_ansible_vars_file" {
  content  = yamlencode(
    {
      tf_share_location = var.share_location
      # gitlab_backup_bucket_name = aws_s3_bucket.gitlab_backup.
    }
  )
  filename = var.ansible_vars_filename
}

variable "ansible_vars_filename" {
    type = string
    default = "/home/deployment_root/app4/tf_ansible_vars_file.yml"
}

Applying that with -var="share_location=example_location" will yield a file like this:

"tf_share_location": "example_location"

Whether or not the quoting the configuration variable names matters depends on Ansible. It shouldn't as it's still valid YAML. Terraform's yamlencode quotes those regardless of whether they need to be, which is regrettable.

I have extracted ansible_vars_filename to a variable as you may want to make that configurable.

I have also left in the commented out gitlab_backup_bucket_name as adding it to the YAML file is as simple as uncommenting it.

You can learn more about yamlencode here:

https://www.terraform.io/docs/configuration/functions/yamlencode.html



来源:https://stackoverflow.com/questions/61664836/how-do-you-output-variables-to-ansible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!