Skip to main content

Create Ansible Inventories

Last updated on

An inventory defines the machines your playbooks target and the variables those machines carry. This guide shows you how to create a static inventory with manually defined hosts, a dynamic inventory that derives hosts from the resources in a Terraform or OpenTofu workspace, and a plugin inventory that fetches hosts from a cloud provider through an Ansible inventory plugin.


What will you learn?

  • Inventory types: When to choose a static, dynamic, or plugin inventory.
  • Static setup: Create an inventory and define hosts and groups manually.
  • Dynamic setup: Add a source that resolves hosts from workspace resources, select the host address attribute, and filter hosts with conditions.
  • Plugin setup: Point Harness at a Git-stored inventory plugin configuration and a cloud connector.
  • Variables: Attach inventory-level variables and environment variables, including secrets.

Before you begin

  • Harness account with IaCM enabled: You need Infrastructure as Code Management under Infrastructure in Harness. For how to access or create a Harness account, see Getting started with Harness Platform.

    Contact Harness support:

    If IaCM does not appear, see Get started with IaCM or contact your account administrator or Harness Support.

  • Ansible concepts: Familiarity with hosts, groups, and inventories. Go to the Ansible in IaCM overview to review the concepts.

  • A provisioned workspace (dynamic inventories only): Dynamic sources resolve hosts from the resources of an IaCM workspace, so the workspace must exist and hold state. Go to Get started with IaCM to create and provision a workspace.

  • Git and cloud connectors (plugin inventories only): The plugin configuration file lives in a Git repository, and provider credentials come from a cloud connector. Go to Connect to a code repository and Cloud connectors to set these up.


Choose an inventory type

Harness IaCM supports three inventory types. Choose based on how your hosts come into existence:

  • Static: You define a list of hosts and groups manually. Best for fixed fleets, bastion hosts, and small stateful clusters where the topology is known and stable.
  • Dynamic: Hosts are derived dynamically from different sources, such as the resources of a Terraform or OpenTofu workspace. Best when provisioning creates or replaces hosts, because the inventory tracks state without manual updates.
  • Plugin: Hosts are fetched from a cloud provider by an Ansible inventory plugin, with the plugin configuration file stored in your Git repository. Best when your team already standardizes on plugins such as aws_ec2 or azure_rm.

Create an inventory

Follow the interactive guide or the step-by-step instructions to create an inventory.

After creation, Harness opens the inventory detail page with four tabs: Hosts, Variables, Activity History, and Configuration. The remaining setup differs by type.

A static inventory starts empty. You add groups and hosts manually on the Hosts tab.

  1. Select + New Group to add a group, or + New Host to add a host directly.
  2. For a group, enter a Group name and optionally select existing hosts that belong to it, then select Apply Changes.
  3. For a host, enter the Host Address (for example, web1.example.com or 10.0.1.25), optionally assign it to groups, then select Apply Changes.

Go to Manage hosts and groups for the full host and group workflow, including host-level variables and connection settings.


Filter hosts with conditions

Filters control which workspace resources a dynamic source turns into hosts. Each filter is a condition made of an attribute, an operator, and a value. Multiple filters combine with AND logic, so a resource must match every condition to be included.

To add a filter in the Source Configuration panel, do the following:

  1. Select Add Filter.
  2. Select the resource attribute to evaluate (for example, tags.environment).
  3. Select an operator: equals, not equals, contains, does not contain, starts with, or ends with.
  4. Enter the value to compare against (for example, prod).

Some common filter patterns are:

  • Environment scoping: tags.environment equals prod so only production resources are configured.
  • Role scoping: tags.Role contains web so database instances never receive the web playbook.
  • Naming conventions: tags.Name starts with edge- to select a named subset of a larger fleet.

Use dynamic variables from resource attributes

Dynamic sources can attach variables to every host they resolve, with values drawn from each resource's attributes or the workspace outputs. Use Harness expressions in the value field:

  • <+attributes.ATTRIBUTE_NAME> reads an attribute from the matched resource, for example <+attributes.private_ip>.
  • <+outputs.OUTPUT_NAME> reads an output from the workspace, for example <+outputs.cluster_endpoint>.

For example, a dynamic variable with key region and value <+attributes.availability_zone> gives every resolved host a region host variable your playbook can reference as {{ region }}. Static values are also allowed and apply unchanged to every host in the source.


Add inventory variables

Inventory-level variables apply to every host in the inventory and are the right place for connection settings and shared configuration. In the inventory's Variables tab, do the following:

  1. Select + Variables. A new variable row appears.
  2. Select the variable type: string for plain-text values or secret for sensitive values.
  3. Enter the variable key and value.
  4. Select Save Changes to persist the rows.

A common baseline for SSH-based fleets stores the connection user as a string (for example, ansible_user: ubuntu) and the private key as a secret-type variable (ansible_ssh_private_key_file) referencing a Harness secret. Secret values are masked in pipeline logs.

Environment variables defined on the inventory are injected into the Ansible process environment at run time rather than becoming Ansible variables.


View activity history

The Activity History tab lists past executions that used this inventory, including the playbook, the pipeline execution, and the run status. Use it to correlate configuration runs with provisioning events and to audit who changed what, when.


Troubleshooting

Dynamic inventory source resolves no hosts from workspace resources in Harness IaCM Ansible

Confirm the workspace holds state with resources of the selected Resource Type, and that every filter condition matches. Filters combine with AND logic, so one non-matching condition excludes the resource.

Host address is empty or wrong for hosts resolved by a dynamic inventory source in Harness IaCM

Check the Host Address Attribute on the source. The selected attribute must exist on the matched resource type and contain a reachable address, for example public_dns or public_ip.

Cannot edit the inventory identifier after creating an inventory in Harness IaCM

Inventory identifiers are immutable after creation. Create a new inventory with the desired identifier and delete the old one.


Next steps

You created an inventory and connected it to real hosts, either manually or from workspace state.