We first create a template-inventory.tpl file, which uses Terraform's template format. In this file, we use a for loop with the syntax %{ for host, ip in vm_dnshost ~}, which allows us to loop the elements of the vm_dnshost variable. For each VM in this loop, we use the following syntax:
${host} ansible_host=${ip}
We end the loop with the %{ endfor ~} syntax.
For more details on this templating format, read the documentation at https://www.terraform.io/docs/configuration/expressions.html#string-templates.
Then in step 2, to the Terraform configuration we add a local_file resource (which we have already studied in the Manipulating local files with Terraform recipe of Chapter 2, Writing Terraform Configuration) in which we fill in the following properties:
- filename: This contains inventory as its value, which is the name of the file that will be generated.
In this recipe, the file will be generated inside the directory...