Member-only story

Automating the creation of an Ansible hosts file from Terraform.

Jack Roper
2 min readMar 16, 2021

--

If you have VMs, you need to configure them. Ansible is a configuration management tool, enabling automation at scale through code.

Ansible requires a host file that lists machines to define what is under its control.

An example of a hosts file is shown below (screenshot is taken from https://www.azurecitadel.com/arc/servers-hack/scale/):

Ansible hosts file example

Maintaining this list quickly becomes challenging, so this is where automating the creation of it from the Terraform output comes in.

First, you’ll need to create a template file:

A variable in your variables.tf file (set it to true!):

In outputs.tf

The module.windows_vms are module.linux_vms modules are set to output the FQDNs:

This code is taken from https://github.com/azurecitadel/arc-onprem-servers if you want to grab it and unpick it further!

Want more Terraform content? Check out my other articles on Terraform here!

Cheers! 🍻

--

--

Jack Roper
Jack Roper

Written by Jack Roper

A blog about DevOps & Cloud Tech. Specializing in Terraform, Kubernetes, GitHub, Azure & Azure DevOps! ☁️

No responses yet