Sample Python script to dynamically parse terraform.tfstate file into Ansible inventory
Copyright (c) 2018, IBM UK [email protected] ti_version = '0.6'
This dynamic inventory script is written for use with Ansible and Terraform on IBM Cloud. Details of how to setup the script can be found in the IBM Cloud documentation. http://................................
This script can be used alongside static inventory files in the same directory
This inventory script expects to find Terraform tags of the form group: host_group associated with each tf instance to define the host group membership for Ansible. Multiple group tags are allowed per host
To avoid co-mingling the Ansible and Terraform definitions in the same directory,
the terraform.tfstate defining the current state of the deployed infrastructure,
is read from the Terraform direcfory. This is specified using the
terraform_inv.ini file in the same directory as this script, pointing to the
location of the terraform.tfstate file to be inventoried. The tfstate file should be referenced by is full path and file name.
[TFSTATE]
#TFSTATE_FILE = /nnn/nnn/nnn/tr_test_files/terraform.tfstate
#TFSTATE_FILE = /Users/JohnDoe/terraform/ibm/app2x/terraform.tfstate
#TFSTATE_FILE = /usr/share/terraform/ibm/app2x/terraform.tfstate
Validate correct execution:
- With supplied test files -
./terraform_inv.py -t ../tr_test_files/terraform.tfstate
- With ini file
./terraform.py --list
Successful execution returns groups with lists of hosts and _meta/hostvars with a detailed host listing. Validate successful operation with ansible from the playbook directory: ansible-inventory -i inventory --list