Terraform-inventory Page

terraform-inventory


* Definition: terraform-inventory is a tool that dynamically generates an inventory file for Ansible from Terraform state files. It bridges the gap between Terraform, used for infrastructure provisioning, and Ansible, used for configuration management.
* Function: Parses Terraform state files to produce an inventory file in a format that Ansible can use, enabling seamless integration between Terraform and Ansible.
* Components:
* Terraform State File: The JSON file generated by Terraform that keeps track of the infrastructure resources it manages.
* terraform-inventory Binary: The executable tool that reads the Terraform state file and outputs an Ansible inventory.
* Features:
* Dynamic Inventory Generation: Automatically creates an up-to-date inventory file from the Terraform state.
* Group and Host Variables: Supports grouping hosts and setting variables based on Terraform attributes.
* Easy Integration: Works with existing Terraform and Ansible setups without significant changes.
* Customizable Output: Can be configured to match specific inventory formats or requirements.
* Usage: Useful for DevOps teams who use Terraform for provisioning infrastructure and Ansible for configuration management, ensuring that the inventory reflects the current state of the infrastructure.

Examples


* Installing terraform-inventory:
```bash
wget https://github.com/adammck/terraform-inventory/releases/download/v0.9/terraform-inventory_v0.9_linux_amd64.zip
unzip terraform-inventory_v0.9_linux_amd64.zip
sudo mv terraform-inventory /usr/local/bin/
```

* Using terraform-inventory to generate an Ansible inventory:
```bash
terraform-inventory -list /path/to/terraform.tfstate
```

* Example of running an Ansible playbook with a dynamic inventory:
```bash
ansible-playbook -i $(terraform-inventory -list /path/to/terraform.tfstate) my-playbook.yml
```

* Sample inventory output:
```json
{
"_meta": {
"hostvars": {
"ec2-34-201-75-64.compute-1.amazonaws.com": {
"ansible_ssh_host": "34.201.75.64",
"instance_type": "t2.micro"
}
}
},
"all": {
"hosts": [
"ec2-34-201-75-64.compute-1.amazonaws.com"
]
},
"aws_ec2": {
"hosts": [
"ec2-34-201-75-64.compute-1.amazonaws.com"
],
"vars": {
"ansible_ssh_user": "ubuntu"
}
}
}
```

Summary


* terraform-inventory: A tool that generates dynamic Ansible inventory files from Terraform state files, enabling integration between infrastructure provisioning with Terraform and configuration management with Ansible. It supports dynamic inventory generation, group and host variables, and easy integration with existing setups.