Skip to main content

Ansible Support

Last updated on

Harness Infrastructure as Code Management (IaCM) integrates with Ansible so you can define your target machines (inventories) and apply automation tasks (playbooks) as part of your CI/CD pipelines.

  • Inventories define where your automation runs.
  • Playbooks define what automation is applied.

Inventories

An inventory defines the machines and groups that your playbooks will target.

Create a new inventory

  1. In the IaCM module, navigate to Inventories and click New Inventory.
  2. Name your inventory and select either Static or Dynamic.
    • Static inventories let you provide a list of host addresses directly in the UI.
    • Dynamic inventories automatically populate hosts from your Terraform/OpenTofu configuration in the selected workspace.

Configure inventory

Add groups and hosts

From the new inventory page, you can create inventory groups, add hosts, and variables and review activity history such as executions. There is no specific order to add groups and hosts, as you can add hosts to groups and groups to hosts at any time.

In the Hosts tab:

  • Add Group → Name the group and assign hosts.
  • Add Host → Enter host address, assign to one or more groups.

In the Variables tab:

  • Add Variable → Enter variable type (string/secret), key, and value.

In the Activity History tab:

  • View activity history, such as executions.

Example inventories

Static inventory example

When you create a static inventory in the UI, you manually enter host addresses.

Example hosts list:

  • web1.example.com
  • db1.example.com

The resulting inventory groups and hosts are visible in the Hosts tab.


Playbooks

A playbook describes the automation tasks to apply to your inventory.

Example playbook

- name: Configure web server infrastructure
hosts: webservers
become: yes
tasks:
- name: Ensure firewall is installed
apt:
name: ufw
state: present

- name: Allow HTTP traffic
ufw:
rule: allow
port: '80'
proto: tcp

- name: Ensure firewall is enabled
ufw:
state: enabled

Pipeline integration

Harness pipelines connect inventories and playbooks so you can run automation as part of your CI/CD workflows.

Example pipeline:

- stage:
name: Ansible
identifier: Ansible
description: ""
type: IACM
spec:
infrastructure:
type: KubernetesDirect
spec:
connectorRef: account.k8connector
namespace: harness-delegate
volumes: []
annotations: {}
labels: {}
automountServiceAccountToken: true
nodeSelector: {}
containerSecurityContext:
capabilities:
drop: []
add: []
os: Linux
hostNames: []
execution:
steps:
- step:
type: IACMAnsiblePlugin
name: Run
identifier: Run
spec:
command: run
imagePullPolicy: Always
image: <account-id>/ansible:ansibleSSHTest26
timeout: 1h
timeout: 1h
playbooks:
- test-playbook
inventories:
- test-inventory
tags: {}

How it works:

  • The pipeline uses the iacm-ansible-inventory inventory.
  • It runs the infrastructure.yml playbook from your Git repo.
  • Output is captured in the pipeline logs for traceability.

Troubleshooting

If you don’t see the expected results, try these quick checks:

  • Hosts missing from dynamic inventory: Verify your workspace has been refreshed and that filter criteria match the attributes in your Terraform/OpenTofu configuration.
  • Playbook variables not resolving: Check the host variables in the Variables tab and confirm the names you reference in your playbook match exactly.
  • Playbook not found in pipeline: Ensure the repository, branch, and path are correct and that your connector has valid access.
  • Connection issues with hosts: Confirm that credentials and network access (for example, SSH keys and security groups) are properly configured.

Summary

Next steps