Sign inSign up free

871. Turn tofu output Into an Inventory

medium 20 XP ~11 minHands-on lab

This machine has OpenTofu, the open-source tool that reads the same configuration language as Terraform. Run it as tofu (terraform runs the same program). There is no network: the local, random and null providers are already installed.

After applying /home/student/infra/courier, the pipeline configures the servers with Ansible, which needs an inventory. The applied configuration has an output servers: a map from host name to { ip, role }.

Task

Create the executable script /home/student/infra/courier/scripts/inventory.sh. Run from the configuration directory, it reads the servers output with tofu output -json and prints, for the current applied servers:

[cache]
cache-1 ansible_host=10.0.2.10

[db]
db-1 ansible_host=10.0.3.10

[web]
web-1 ansible_host=10.0.1.10
web-2 ansible_host=10.0.1.11
  • One group per role, groups sorted by role, hosts sorted by name, exactly one empty line between groups, nothing else.
  • If the output can't be read (for example, nothing applied yet), it fails with a non-zero exit code.

The grader also applies other servers and runs your script again.

About this machine

Ubuntu 24.04 with no internet access. You are student with passwordless sudo. The grader checks the final state of the machine, not the commands you typed. Values like names and ports are generated for you.

Part of Prepare for the Terraform Associate. Next step: Sensitive Values Done Right

Teaching or writing about this? Embed this lab on your page

TerminalNot started

Your machine is ready to start

A fresh, isolated Ubuntu machine boots in a few seconds with the task already set up.

No account needed: 3 of 3 free tries left. Guest machines last 30 minutes.