You try to run a playbook and get an inventory parse error. The error message points to a line number, but inventory files can be tricky—the syntax for INI and YAML differs, and mixing them causes problems. A malformed inventory stops everything.

Introduction

This article covers troubleshooting steps and solutions for Fix Ansible Inventory Parse Error. The error typically occurs in production environments and can cause service disruptions if not addressed promptly.

Symptoms

``` ERROR! We were unable to read either as JSON nor YAML, these are offending files: /home/user/inventory/hosts

The error appears to have been in '/home/user/inventory/hosts': line 5, column 1 ```

Or INI-specific errors:

bash
ERROR! Expected host definition, got: webservers

Or YAML-specific errors:

bash
ERROR! We were unable to read the inventory file as YAML:
  mapping values are not allowed here

Common Causes

  • Configuration misconfiguration
  • Missing or incorrect credentials
  • Network connectivity issues
  • Version compatibility problems
  • Resource exhaustion or limits
  • Permission or access denied

Step-by-Step Fix

Validate your inventory:

```bash # Parse and show inventory ansible-inventory --list

# Show in YAML format ansible-inventory --list -y

# Show specific host ansible-inventory --host webserver

# Graph view ansible-inventory --graph ```

Check which inventory file Ansible is using:

```bash # Show inventory path ansible-config dump | grep inventory

# Or ansible --version | grep inventory ```

Common INI Format Errors

Missing Group Headers

Problem: ``ini webserver ansible_host=192.168.1.10 dbserver ansible_host=192.168.1.11

Items without a group header are ignored or cause errors.

Fix: ```ini [webservers] webserver ansible_host=192.168.1.10

[dbservers] dbserver ansible_host=192.168.1.11 ```

Wrong Group Syntax

Problem: ```ini [webservers] web1 web2 web3

[webservers:vars] # Wrong placement ansible_user=deploy ```

Fix: ```ini [webservers] web1 web2 web3

[webservers:vars] ansible_user=deploy ```

The :vars section must come after the host list.

Invalid Host Variable Syntax

Problem: ``ini [webservers] webserver ansible_host=192.168.1.10 ansible_port=2222

Works, but if you have spaces in values:

ini
[webservers]
webserver ansible_host=192.168.1.10 description=Web Server 1
# Error: "Server" is parsed as a new host

Fix - quote values: ``ini [webservers] webserver ansible_host=192.168.1.10 description="Web Server 1"

Nested Group Syntax

Problem: ```ini [production] web1 web2

[production:children] webservers # Error if webservers group doesn't exist ```

Fix - define child groups first: ```ini [webservers] web1 web2

[production:children] webservers ```

Default Group Reserved

Problem: ``ini [all] web1 web2

all is a reserved group containing all hosts.

Fix - use a different name: ``ini [myhosts] web1 web2

Common YAML Format Errors

Wrong Indentation

Problem: ``yaml webservers: hosts: web1: ansible_host: 192.168.1.10

Fix - correct YAML indentation: ``yaml webservers: hosts: web1: ansible_host: 192.168.1.10 web2: ansible_host: 192.168.1.11

Missing `hosts` Key

Problem: ``yaml webservers: web1: ansible_host: 192.168.1.10

Fix: ``yaml webservers: hosts: web1: ansible_host: 192.168.1.10

Wrong Variable Section

Problem: ```yaml webservers: hosts: web1: ansible_host: 192.168.1.10 vars: # This applies to webservers group ansible_user: deploy

all: vars: # This applies to all hosts ansible_user: deploy ```

Correct structure: ``yaml all: children: webservers: hosts: web1: ansible_host: 192.168.1.10 vars: ansible_user: deploy vars: ansible_user: default_user

Children Syntax

Problem: ``yaml production: children: webservers: hosts: web1:

Fix - proper children structure: ``yaml all: children: production: children: webservers: hosts: web1: ansible_host: 192.168.1.10

Mixed Format Issues

Mixing INI and YAML in Same File

Problem: ```yaml [webservers] web1 ansible_host=192.168.1.10

dbservers: hosts: db1: ```

Don't mix formats in a single file.

Fix - use one format consistently:

INI: ```ini [webservers] web1 ansible_host=192.168.1.10

[dbservers] db1 ansible_host=192.168.1.20 ```

YAML: ``yaml webservers: hosts: web1: ansible_host: 192.168.1.10 dbservers: hosts: db1: ansible_host: 192.168.1.20

Directory-Based Inventory

For complex inventories, use a directory structure:

bash
inventory/
├── hosts.yml
├── group_vars/
│   ├── all.yml
│   ├── webservers.yml
│   └── dbservers.yml
└── host_vars/
    ├── web1.yml
    └── web2.yml

inventory/hosts.yml: ``yaml all: children: webservers: hosts: web1: web2: dbservers: hosts: db1:

inventory/group_vars/webservers.yml: ``yaml ansible_user: deploy nginx_port: 80

Dynamic Inventory Issues

If using dynamic inventory scripts:

Check script is executable:

bash
chmod +x inventory/dynamic_script.py

Test script output:

bash
./inventory/dynamic_script.py --list

Verify JSON/YAML output:

bash
./inventory/dynamic_script.py --list | python -m json.tool

Use inventory plugin instead of script:

yaml
# inventory/aws_ec2.yml
plugin: aws_ec2
regions:
  - us-east-1
filters:
  tag:Environment: production

Special Characters in Hostnames

Problem: ``ini [webservers] web-server-01 ansible_host=192.168.1.10

Hyphens in hostnames are fine in INI, but might cause issues in YAML.

YAML fix: ``yaml webservers: hosts: web-server-01: ansible_host: 192.168.1.10

Or quote: ``yaml webservers: hosts: "web-server-01": ansible_host: 192.168.1.10

Verification

After fixing, verify:

```bash # List all inventory ansible-inventory --list

# Show as graph ansible-inventory --graph

# Test host access ansible all --list-hosts

# Ping all hosts ansible all -m ping

# Verify a specific host ansible-inventory --host web1 ```

Quick Checklist

  1. 1.Correct format? Use INI or YAML consistently
  2. 2.Valid syntax? Run ansible-inventory --list
  3. 3.Groups defined? Hosts must be in groups
  4. 4.Indentation correct? YAML needs 2-space indentation
  5. 5.Variables quoted? Values with spaces need quotes
  6. 6.File extension? Use .yml for YAML, no extension or .ini for INI
  • [WordPress troubleshooting: Ansible Artifact Download Uses an Old Mi](ansible-artifact-download-uses-an-old-mirror-after-proxy-change)
  • [WordPress troubleshooting: Ansible Audit Trail Misses Events Under ](ansible-audit-trail-misses-events-under-burst-load)
  • [WordPress troubleshooting: Ansible Background Worker Gets Stuck in ](ansible-background-worker-stuck-in-a-retry-loop)
  • [WordPress troubleshooting: Ansible Backup Completes but Restore Fai](ansible-backup-completes-but-restore-fails-checksum-validation)
  • [WordPress troubleshooting: Ansible Batch Importer Duplicates Rows A](ansible-batch-importer-duplicates-rows-after-a-retry)

<script type="application/ld+json"> { "@context": "https://schema.org", "@type": "TechArticle", "headline": "Fix Ansible Inventory Parse Error", "description": "Learn how to fix Ansible inventory parsing errors with correct INI and YAML syntax, host patterns, and group configurations.", "url": "https://www.fixwikihub.com/fix-ansible-inventory-parse-error", "publisher": { "@type": "Organization", "name": "FixWikiHub", "url": "https://www.fixwikihub.com" }, "author": { "@type": "Person", "name": "FixWikiHub Editorial Team" }, "datePublished": "2025-11-11T16:30:23.114Z", "dateModified": "2025-11-11T16:30:23.114Z" } </script>