Introduction
Ansible uses inventory files to define target hosts. When the inventory file is missing, at an incorrect path, or has invalid syntax, Ansible cannot find any hosts to manage.
Symptoms
Inventory not found:
```bash $ ansible all -m ping
[WARNING]: Unable to parse /etc/ansible/hosts as an inventory source [WARNING]: No inventory was parsed, only implicit localhost is available [WARNING]: provided hosts list is empty, only localhost is available ```
No hosts matched:
```bash $ ansible webservers -m ping
[WARNING]: Could not match supplied host pattern, ignoring: webservers [WARNING]: No hosts matched ```
File not found:
```bash $ ansible-playbook site.yml -i inventory/hosts
ERROR! Unable to read the inventory file: inventory/hosts File not found: inventory/hosts ```
Common Causes
- 1.Wrong file path - Inventory file at different location
- 2.File doesn't exist - Inventory file not created
- 3.Invalid format - YAML or INI syntax errors
- 4.Not in ansible.cfg - Default inventory path not set
- 5.Permission denied - Can't read inventory file
- 6.Wrong -i option - Incorrect path in command line
Step-by-Step Fix
- 1.Identify the error in logs
- 2.Verify configuration settings
- 3.Test connectivity
- 4.Apply corrective action
- 5.Verify the fix
Step 1: Check Default Inventory Location
```bash # Default inventory locations Ansible checks: # 1. -i option on command line # 2. ANSIBLE_INVENTORY environment variable # 3. inventory in ansible.cfg # 4. /etc/ansible/hosts
# Check if default inventory exists ls -la /etc/ansible/hosts
# Check ansible.cfg for inventory setting cat /etc/ansible/ansible.cfg | grep inventory cat ansible.cfg | grep inventory 2>/dev/null
# Check environment variable echo $ANSIBLE_INVENTORY
# List all ansible configuration ansible-config dump | grep INVENTORY ```
Step 2: Create Inventory File
```bash # Create default inventory file sudo mkdir -p /etc/ansible sudo touch /etc/ansible/hosts
# Add hosts to inventory # INI format: cat << 'EOF' | sudo tee /etc/ansible/hosts [webservers] web1.example.com web2.example.com
[dbservers] db1.example.com
[all:children] webservers dbservers EOF
# Or create project-specific inventory mkdir -p inventory cat << 'EOF' > inventory/hosts [webservers] web1.example.com web2.example.com EOF
# Test inventory ansible-inventory -i inventory/hosts --list ```
Step 3: Specify Inventory Path
```bash # Using -i option ansible all -i inventory/hosts -m ping
# For directory (all files in directory used) ansible all -i inventory/ -m ping
# For multiple inventory files ansible all -i inventory/hosts -i inventory/dynamic.py -m ping
# In ansible-playbook ansible-playbook site.yml -i inventory/hosts
# Using environment variable export ANSIBLE_INVENTORY=inventory/hosts ansible all -m ping ```
Step 4: Configure ansible.cfg
```bash # Create ansible.cfg in project directory cat << 'EOF' > ansible.cfg [defaults] inventory = ./inventory/hosts host_key_checking = False roles_path = ./roles retry_files_enabled = False EOF
# Verify configuration ansible-config view | grep inventory
# Ansible will now use ./inventory/hosts by default ansible all -m ping
# Check which config file is being used ansible-config view | head -1 ```
Step 5: Validate Inventory Format
```bash # Validate INI format inventory ansible-inventory -i inventory/hosts --list
# Validate YAML format inventory cat << 'EOF' > inventory/hosts.yml all: children: webservers: hosts: web1.example.com: web2.example.com: dbservers: hosts: db1.example.com: EOF
# Validate YAML syntax ansible-inventory -i inventory/hosts.yml --list
# Common YAML errors: # - Wrong indentation # - Missing colon # - Invalid characters
# Validate inventory and show graph ansible-inventory -i inventory/hosts --graph ```
Step 6: Check File Permissions
```bash # Check inventory file permissions ls -la inventory/hosts
# Should be readable # Fix permissions if needed: chmod 644 inventory/hosts
# Check directory permissions ls -la inventory/
# If using executable inventory script: chmod +x inventory/dynamic.py
# Check ownership chown $USER:$USER inventory/hosts
# Test read access cat inventory/hosts ```
Step 7: Test Inventory Parsing
```bash # List all inventory hosts ansible all --list-hosts
# List specific group ansible webservers --list-hosts
# Show inventory details ansible-inventory -i inventory/hosts --list
# Show inventory as graph ansible-inventory -i inventory/hosts --graph
# Debug inventory parsing ansible all -m debug -a "var=inventory_hostname" -v
# Check host variables ansible-inventory -i inventory/hosts --host web1.example.com ```
Step 8: Use Dynamic Inventory
```bash # Dynamic inventory scripts must be executable chmod +x inventory/aws_ec2.py
# Test dynamic inventory ./inventory/aws_ec2.py --list
# Use with ansible ansible all -i inventory/aws_ec2.py -m ping
# AWS dynamic inventory with plugin cat << 'EOF' > inventory/aws_ec2.yml plugin: aws_ec2 regions: - us-east-1 - us-west-2 keyed_groups: - key: tags.Environment EOF
# Use plugin ansible-inventory -i inventory/aws_ec2.yml --list ```
Step 9: Debug Inventory Issues
```bash # Run with verbose output ansible all -m ping -vvv
# Check what Ansible sees ansible localhost -m debug -a "var=hostvars"
# Check inventory path resolution ansible-config dump | grep -i inventory
# Test minimal inventory echo "localhost" > /tmp/test_inventory ansible all -i /tmp/test_inventory -m ping
# Check for hidden characters cat -A inventory/hosts
# Verify no Windows line endings file inventory/hosts # Should show: ASCII text # If shows: ASCII text, with CRLF line terminators dos2unix inventory/hosts ```
Step 10: Common Inventory Patterns
```bash # INI format with groups [webservers] web[01:10].example.com # Range: web01-web10
[dbservers] db1.example.com ansible_host=192.168.1.10
[all:vars] ansible_user=admin ansible_ssh_private_key_file=~/.ssh/id_rsa
# YAML format cat << 'EOF' > inventory/hosts.yml all: vars: ansible_user: admin children: webservers: hosts: web1.example.com: ansible_host: 192.168.1.1 web2.example.com: ansible_host: 192.168.1.2 dbservers: hosts: db1.example.com: ansible_port: 2222 EOF
# Multiple inventory files in directory mkdir -p inventory/group_vars inventory/host_vars # inventory/hosts # inventory/group_vars/webservers.yml # inventory/host_vars/web1.example.com.yml ```
Inventory File Locations
| Priority | Location | Use Case |
|---|---|---|
| 1 | -i option | Command line override |
| 2 | ANSIBLE_INVENTORY env | Environment config |
| 3 | ansible.cfg | Project default |
| 4 | /etc/ansible/hosts | System default |
Verification
```bash # After creating/configuring inventory
# 1. Verify file exists ls -la inventory/hosts
# 2. Validate syntax ansible-inventory -i inventory/hosts --list
# 3. List hosts ansible all --list-hosts
# 4. Test connectivity ansible all -m ping
# 5. Run playbook ansible-playbook site.yml
# 6. Verify configuration ansible-config view | grep inventory
# Should show hosts, not warning about empty inventory ```
Prevention
To prevent Ansible inventory not found issues from recurring, implement these proactive measures:
1. Validate Inventory in CI/CD
```bash # Add to CI pipeline ansible-inventory -i inventory/ --list --check
# Pre-commit hook for inventory validation cat << 'EOF' > .git/hooks/pre-commit #!/bin/bash if git diff --cached --name-only | grep -q "inventory/"; then ansible-inventory -i inventory/hosts --list > /dev/null 2>&1 if [ $? -ne 0 ]; then echo "ERROR: Invalid inventory file" exit 1 fi fi EOF chmod +x .git/hooks/pre-commit ```
2. Use ansible.cfg for Consistent Paths
```ini # ansible.cfg in project root [defaults] inventory = ./inventory/hosts host_key_checking = False retry_files_enabled = False gathering = smart
# Ensures consistent inventory location ```
3. Document Inventory Structure
```bash # Create inventory README cat << 'EOF' > inventory/README.md # Inventory Structure
- hosts - Main inventory file (INI format)
- group_vars/ - Group-specific variables
- host_vars/ - Host-specific variables
Usage ansible all -i inventory/ -m ping EOF ```
Best Practices Checklist
- [ ] Validate inventory in CI/CD pipeline
- [ ] Use ansible.cfg for consistent paths
- [ ] Document inventory structure
- [ ] Use version control for inventory
- [ ] Test inventory with ansible-inventory
- [ ] Keep inventory paths relative
Related Issues
- [Fix Ansible Playbook Failed](/articles/fix-ansible-playbook-failed)
- [Fix Ansible Host Unreachable](/articles/fix-ansible-host-unreachable)
- [Fix Ansible Module Import Failed](/articles/fix-ansible-module-import-failed)
Related Articles
- [Technical troubleshooting: Fix Cicd Artifact Upload Failed Storage Issue in C](cicd-artifact-upload-failed-storage)
- [Technical troubleshooting: Fix Cicd Code Quality Gate Failed Sonarqube Issue ](cicd-code-quality-gate-failed-sonarqube)
- [Technical troubleshooting: Fix Cicd Deployment Failed Health Check Issue in C](cicd-deployment-failed-health-check)
- [Technical troubleshooting: Fix Cicd Github Actions Workflow Queue Timeout in ](cicd-github-actions-workflow-queue-timeout)
- [Technical troubleshooting: Fix Cicd Gitlab Runner Stuck Pending Issue in CI/C](cicd-gitlab-runner-stuck-pending)
<script type="application/ld+json"> { "@context": "https://schema.org", "@type": "TechArticle", "headline": "Fix Ansible Inventory Not Found", "description": "Troubleshoot Ansible inventory not found errors. Check path, format, and ansible.cfg configuration.", "url": "https://www.fixwikihub.com/fix-ansible-inventory-not-found", "publisher": { "@type": "Organization", "name": "FixWikiHub", "url": "https://www.fixwikihub.com" }, "author": { "@type": "Person", "name": "FixWikiHub Editorial Team" }, "datePublished": "2026-04-04T09:01:26.036Z", "dateModified": "2026-04-04T09:01:26.036Z" } </script>