0Pricing
Ansible Academy · Lesson

constructed: Group Hosts by Tags

Build groups from cloud metadata.

constructed: Group Hosts by Tags is a free Ansible Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ansible Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Raw Hosts Are Not Enough

A flat list of EC2 instances is hard to target. You want groups like webservers or production built from each host metadata.

Two Ways to Group

You can group inside the aws_ec2 plugin with keyed_groups, or layer the constructed plugin on top for richer logic.

keyed_groups from a Tag

The keyed_groups option turns a tag value into a group name automatically, one group per distinct value.

keyed_groups:
  - key: tags.Environment
    prefix: env

Prefixes Keep Names Clean

A prefix like env turns the value prod into the group env_prod, so cloud tags never collide with your own group names.

The constructed Plugin

The constructed plugin adds groups and variables to existing hosts using Jinja2, after another source has loaded them.

plugin: constructed

groups: Build by Expression

Under groups, a Jinja2 expression decides membership. Hosts where it is true join that group.

groups:
  is_prod: "tags.Environment == 'prod'"

compose: Set Host Variables

The compose key sets new host variables from expressions, like deriving an app name from a tag for later use.

compose:
  app: tags.Service | default('unknown')

keyed_groups Here Too

constructed also supports keyed_groups, letting you generate dynamic group names from any fact or variable already present.

strict Catches Typos

Set strict: true so a bad expression fails loudly instead of silently skipping a host you meant to group.

strict: true

Now Target Groups Cleanly

Once groups exist you write plays against them naturally, like hosts: is_prod, with no static host list anywhere.

- hosts: is_prod
  roles:
    - webserver

Chain Sources in Order

Point Ansible at a directory of inventory files. It loads aws_ec2 first, then constructed enriches those hosts with your groups.

Quick Check

Recall which option auto-creates groups from a tag.

Recap

Use keyed_groups with the constructed plugin groups and compose to turn cloud tags into clean, targetable Ansible groups. 🏷️

Frequently asked questions

Is the “constructed: Group Hosts by Tags” lesson free?

Yes — the full text of “constructed: Group Hosts by Tags” is free to read here on the web, and the Ansible Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ansible Academy course, upgrade to CoddyKit PRO.

What will I learn in “constructed: Group Hosts by Tags”?

Build groups from cloud metadata. You practise Ansible Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ansible Academy?

No prior experience is required. Ansible Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “constructed: Group Hosts by Tags” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ansible Academy lesson?

Yes. Every Ansible Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Static Limits & the Inventory Plugin Model
  2. The aws_ec2 Inventory Plugin
  3. constructed: Group Hosts by Tags
  4. Caching & Multiple Inventory Sources
← Back to Ansible Academy