Ansible :Unable to parse /etc/ansible/hosts as an inventory source

Viewed 99133

I am new to ansible, got the below issue. I was able to ssh into my client machine .but unable to run playbook.

Getting the error below:

[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. Note that the implicit
localhost does not match 'all'

 [WARNING]: Could not match supplied host pattern, ignoring: a

here a is my group name. my hosts given below :

---------


[a]
172.31.26.93



[all:vars]

ansible_user=vagrant
ansible_ssh_pass=vagrant


 ansible_ssh_host=172.31.26.93
 ansible_ssh_port=22
 ansible_ssh_user='ansibleuser'
 ansible_ssh_private_key_file=/home/ansibleuser/.ssh

------- my playbook file given below ----

- hosts: a
  tasks:
  - name: create a directory
    file: path=/home/ansiblesuser/www state=directory

This is the first time I am getting this issue.

11 Answers

Before running the playbook just run the following command

ansible all --list-hosts

If the above error persists then go to /etc/ansible/ansible.cfg and edit the inventory path which points to your specific host file.

i had the same issue, the plugins ini and yaml were not enable in the ansible.cfg :

[inventory]
enable_plugins = yaml, ini

I suddenly experienced the same issue with an inventory that was many years in use and hadn't changed recently. It turned out I enabled a plugin which caused this issue.

I enabled the plugin vmware_vm_inventory which was the source of the message. This showed up by running ansible-playbook -vvvv <host>

I'd figured I should define the plugin in an ansible.cfg file that is present in the folder where I run playbooks that use this plugin and leave it out of the /etc/ansible/ansible.cfg

The following solved the problem for me:

  1. Go to the root directory /
  2. cd etc or mkdir etc and cd etc
  3. mkdir ansible then cd ansible
  4. vi hosts (then add the hosts)
  5. chmod 777 hosts.
  6. Check ansible all -m ping

Centos 7

this was causing above error: ansible-playbook -i host_test -v tasks.yml

this fixed it: ansible-playbook -i hosttest -v tasks.yml

Host file with no read permissions caused the following error message:

[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. Note that
the implicit localhost does not match 'all'

Solution: change permissions

sudo chmod 744 /etc/ansible/hosts 

In another occasion I got an error message due to an incorrect formatting of the hosts file:

[WARNING]:  * Failed to parse /etc/ansible/hosts with yaml plugin: YAML
inventory has invalid structure, it should be a dictionary, got: <class
'ansible.parsing.yaml.objects.AnsibleUnicode'>
[WARNING]:  * Failed to parse /etc/ansible/hosts with ini plugin:
/etc/ansible/hosts:3: Expected key=value host variable assignment, got: ;
[WARNING]: Unable to parse /etc/ansible/hosts as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available

and I fixed the issue by correcting the format error.

As suggested in another answer check if hosts are available with

ansible all --list-hosts

Also check the actual location of the inventory file in the Ansible configuration file ansible.cfg.

You'll see this error if the file doesn't exist on disk as well.

This will be in the -vvvv log:

Skipping due to inventory source not existing or not being readable by the current user

Removing the spaces around the = worked for me, Ansible managed to parse my host.ini file.

I have the same problem:

The solution was Space issues in Token Solution was Space issues in Token

`$ ansible-playbook -i inventory/forem/setup.yml playbooks/providers/aws.yml [WARNING]: * Failed to parse /var/home/core/selfhost/inventory/forem/setup.yml with ini plugin: Invalid host pattern '---' supplied, '---' is normally a sign this is a YAML file. [WARNING]: * Failed to parse /var/home/core/selfhost/inventory/forem/setup.yml with yaml plugin: We were unable to read either as JSON nor YAML, these are the errors we got from each: JSON: Expecting value: line 1 column 1 (char 0) Syntax Error while loading YAML. could not find expected ':' The error appears to be in '/var/home/core/selfhost/inventory/forem/setup.yml': line 85, column 11, but may be elsewhere in the file depending on the exact syntax problem. The offending line appears to be: $ANSIBLE_VAULT;1.1;AES256 62376137383864393461613561353234643230666431643935303533346631393537363564366334 ^ here [WARNING]: Unable to parse /var/home/core/selfhost/inventory/forem/setup.yml as an inventory source [WARNING]: No inventory was parsed, only implicit localhost is available [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [Deploy Forem to AWS]

If you are still having this issue when you run

ansible-playbook -i path/to/inventory/file playbook.yml

Simply create an empty ansible.cfg file in the directory where you have your playbook.

Using ansible version ansible 2.9.27 able to resolve by giving complete (absolute path) for me error is like below

[WARNING]: Unable to parse /root/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. Note that the implicit localhost does not match 'all' [WARNING]: Could not match supplied host pattern, ignoring: all

Use following command

ansible-playbook -i /etc/ansible/hosts showfiles.yml

Related