How to pass terraform outputs variables into ansible as vars_files?

那年仲夏 提交于 2020-06-08 04:16:46

问题


I am provisioning AWS infrastructure using terraform and want to pass variables such as aws_subnet_id and aws_security_id into ansible playbook using vars_file (don't know if there is any other way though). How can I do that?


回答1:


terraform outputs are an option, or you can just use something like:

provisioner "local-exec" {
  command = "ANSIBLE_HOST_KEY_CHECKING=\"False\" ansible-playbook -u ${var.ssh_user} --private-key=\"~/.ssh/id_rsa\" --extra-vars='{"aws_subnet_id": ${aws_terraform_variable_here}, "aws_security_id": ${aws_terraform_variable_here} }' -i '${azurerm_public_ip.pnic.ip_address},' ansible/deploy-with-ansible.yml"
}

or you can do a sed thing ... as a local provisioner to update the var file..

or you can use terraform outputs.... your preference....




回答2:


Use terraform outputs - https://www.terraform.io/intro/getting-started/outputs.html (it is not clear if you are using it already)

Then using command like terraform output ip, you can then use those values in your scripts to generate or populate other files like inventory files or vars_file.

Another option is to use terraform templates and render your files like inventory files from terraform itself and then use it from Ansible.




回答3:


I use a Terraform template to create an Ansible vars_file.

Create Terraform template file tf_ansible_vars_file.yml.tpl. Prefix the Ansible variable names with tf_ to remind you that their values come from Terraform:

# Ansible vars_file containing variable values from Terraform.
# Generated by Terraform mgmt configuration.

tf_environment: "${environment}"
tf_gitlab_backup_bucket_name: "${gitlab_backup_bucket_name}"

In your Terraform configuration file, render the template to a file:

# Define an Ansible var_file containing Terraform variable values
data "template_file" "tf_ansible_vars_file" {
  template = "${file("./tf_ansible_vars_file.yml.tpl")}"
  vars = {
    environment = var.environment
    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 = "./tf_ansible_vars_file.yml"
}

Run terraform apply to create Ansible var_file tf_ansible_vars_file.yml containing Terraform variable values:

# Ansible vars_file containing variable values from Terraform.
# Generated by Terraform mgmt configuration.

tf_environment: "mgmt"
tf_gitlab_backup_bucket_name: "project-mgmt-gitlab-backup"

Add tf_ansible_vars_file.yml to your Ansible playbook:

  vars_files:
    - ../terraform/mgmt/tf_ansible_vars_file.yml

Now, in Ansible the variables defined in this file will contain values from Terraform.

Obviously, this means that you must run Terraform before Ansible. But it won't be so obvious to all your Ansible users. Add assertions to your Ansible playbook to help the user figure out what to do if a tf_ variable is missing:

- name: Check mandatory variables imported from Terraform
  assert:
    that:
      - tf_environment is defined
      - tf_gitlab_backup_bucket_name is defined
    fail_msg: "tf_* variable usually defined in '../terraform/mgmt/tf_ansible_vars_file.yml' is missing"



回答4:


I highly recommend this script. It works well and is maintained by Cisco and will give you more flexibility.

https://github.com/CiscoCloud/terraform.py



来源:https://stackoverflow.com/questions/40353666/how-to-pass-terraform-outputs-variables-into-ansible-as-vars-files

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