Learn how to streamline your infrastructure automation by seamlessly passing Terraform output variables into Ansible playbooks as vars_files for efficient and dynamic configuration management.
This document outlines the process of passing variables from Terraform to Ansible, enabling seamless integration between infrastructure provisioning and configuration management. The process involves defining outputs in Terraform, capturing these outputs, and then referencing them as variables within your Ansible playbooks.
Output Terraform Variables: In your Terraform code, define outputs for the variables you want to pass to Ansible.
output "subnet_id" {
value = aws_subnet.example.id
}
Capture Terraform Outputs:
Use a tool like terraform output
or jq
to capture the output values and store them in a JSON or YAML file.
terraform output -json > terraform.json
Create Ansible Vars File:
Create a YAML file (e.g., vars.yml
) to store the Terraform outputs as Ansible variables.
subnet_id: "{{ lookup('file', 'terraform.json') | from_json | json_query('subnet_id') }}"
Use Variables in Ansible Playbook:
In your Ansible playbook, reference the variables defined in the vars.yml
file.
- name: Create EC2 instance
hosts: all
tasks:
- name: Launch instance
amazon.aws.ec2_instance:
subnet_id: "{{ subnet_id }}"
Run Ansible Playbook:
Execute your Ansible playbook, passing the vars.yml
file using the -e
or --extra-vars
flag.
ansible-playbook -i inventory.ini playbook.yml -e @vars.yml
Explanation:
lookup
and json_query
to extract specific values from the JSON file.vars.yml
.-e
flag passes the vars.yml
file to the playbook, making the variables available.This setup uses Terraform to create an AWS VPC and subnet, saving the subnet ID to a JSON file. An Ansible playbook then references this file to dynamically retrieve the subnet ID and launch an EC2 instance within the provisioned subnet, demonstrating infrastructure-as-code collaboration.
# Configure the AWS Provider
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}
provider "aws" {
region = "us-west-2" # Replace with your desired region
}
# Create a VPC
resource "aws_vpc" "example" {
cidr_block = "10.0.0.0/16"
}
# Create a Subnet
resource "aws_subnet" "example" {
vpc_id = aws_vpc.example.id
cidr_block = "10.0.1.0/24"
}
# Output the Subnet ID
output "subnet_id" {
value = aws_subnet.example.id
}
---
- name: Create EC2 instance
hosts: all
tasks:
- name: Launch instance
amazon.aws.ec2_instance:
key_name: your_key_pair_name # Replace with your key pair name
instance_type: t2.micro
image_id: ami-0c55b159c2d5wi538e # Replace with your desired AMI ID
subnet_id: "{{ subnet_id }}"
assign_public_ip: true
security_groups:
- default
---
subnet_id: "{{ lookup('file', 'terraform.json') | from_json | json_query('subnet_id') }}"
Deploy Terraform Infrastructure:
terraform init
terraform apply -auto-approve
Capture Terraform Outputs:
terraform output -json > terraform.json
Run Ansible Playbook:
ansible-playbook -i inventory.ini playbook.yml -e @vars.yml
Explanation:
subnet_id
variable.subnet_id
value from the terraform.json
file using lookup
, from_json
, and json_query
.vars.yml
file to access the Terraform output.Note:
inventory.ini
) configured for your target environment.Flexibility and Reusability:
Alternative Approaches:
Security Considerations:
Best Practices:
Additional Tips:
terraform output
Filtering: You can use the -raw
flag with terraform output
to get a specific output value without any formatting, making it easier to parse in Ansible.json_query
for extracting specific values and to_json
for converting data structures to JSON format.By understanding these concepts and best practices, you can effectively bridge the gap between Terraform and Ansible, enabling a robust and automated workflow for infrastructure provisioning and configuration management.
This document outlines the process of passing variables from Terraform to Ansible, enabling seamless infrastructure provisioning and configuration.
Steps:
Define Terraform Outputs:
output
directive to specify the variables you want to pass to Ansible. For example:
output "subnet_id" {
value = aws_subnet.example.id
}
Capture Terraform Outputs:
terraform apply
, capture the output values using tools like terraform output
or jq
. Store these values in a structured format like JSON or YAML:
terraform output -json > terraform.json
Create Ansible Variables File:
vars.yml
) to store the captured Terraform outputs as Ansible variables. Utilize lookup
and json_query
to extract specific values from the JSON file:
subnet_id: "{{ lookup('file', 'terraform.json') | from_json | json_query('subnet_id') }}"
Utilize Variables in Ansible Playbook:
vars.yml
within your tasks:
- name: Create EC2 instance
hosts: all
tasks:
- name: Launch instance
amazon.aws.ec2_instance:
subnet_id: "{{ subnet_id }}"
Execute Ansible Playbook:
vars.yml
file using the -e
or --extra-vars
flag to make the variables available during execution:
ansible-playbook -i inventory.ini playbook.yml -e @vars.yml
Benefits:
This approach effectively bridges the gap between Terraform and Ansible, enabling a robust and automated workflow for infrastructure provisioning and configuration management. By defining outputs in Terraform and referencing them as variables within Ansible playbooks, you can create a seamless process for deploying and configuring your infrastructure. This method ensures that Ansible has access to the latest infrastructure details, promoting consistency and reducing manual errors. Remember to handle sensitive data with care and follow security best practices when passing variables between these tools.
execute
Interpolation in Terraform ยท 3.