TF-004 Use Terraform outside the core workflow Practice Question
An organization uses Terraform to provision infrastructure and then Ansible to configure it. They want to pass dynamic IP addresses from Terraform to Ansible. What is a recommended approach?
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Use terraform output -json and parse it in Ansible as an inventory.
`terraform output -json` produces JSON output that can be parsed by Ansible to dynamically build an inventory. This approach is simple, directly uses Terraform's built-in output, and avoids additional infrastructure. Option A is incorrect: while environment variables can pass simple strings, they are less suitable for structured data like multiple IP addresses and are not the recommended pattern. Option B is incorrect: `terraform_remote_state` is designed for fetching outputs from other Terraform configurations, not for exposing data to external tools like Ansible. Option C is incorrect: using Consul KV store introduces unnecessary complexity and external dependencies; the direct `terraform output` approach is simpler and more maintainable.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Use environment variables in the pipeline to pass IP addresses.
Why it's wrong here
While environment variables can pass simple, static values, they become unwieldy and error-prone for dynamically generated lists of IP addresses or complex data structures. Each IP would require a distinct variable, making the pipeline configuration brittle and difficult to maintain as the infrastructure scales or changes. This approach lacks the structured output capabilities needed for robust integration with configuration management tools.
- ✗
Use the terraform_remote_state data source in a dummy Terraform configuration.
Why it's wrong here
The terraform_remote_state data source is specifically designed to allow one Terraform configuration to read outputs from another Terraform state file. Its primary use case is for orchestrating dependencies between different Terraform modules or projects. It cannot directly expose state data in a format consumable by Ansible without an additional Terraform apply step, which would be an unnecessary intermediary layer.
- ✗
Store outputs in Consul KV store and have Ansible read from there.
Why it's wrong here
Storing Terraform outputs in Consul's Key-Value store is technically feasible and provides a centralized data source. However, this method introduces additional operational overhead by requiring a running Consul cluster, configuring Terraform to write to it, and then configuring Ansible to read from it. For simply passing Terraform outputs to Ansible, it adds an unnecessary dependency and complexity compared to direct integration.
- ✓
Use terraform output -json and parse it in Ansible as an inventory.
Why this is correct
The terraform output -json command provides a structured, machine-readable JSON representation of all defined Terraform outputs. Ansible can natively consume dynamic inventories in JSON format, making this a highly efficient and idiomatic integration method. This approach directly leverages Terraform's output capabilities and Ansible's inventory system without requiring intermediate tools or complex parsing logic.
Go deeper
Related to this question
About these practice questions
One of 428 original TF-004 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This TF-004 practice question is part of Courseiva's free HashiCorp certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the TF-004 exam.