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

12

24

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.

Vetchling answered 8/11, 2018 at 10:19 Comment(5)
Hi Mani, welcome to SO! You didn't specify the file that contains your group name, nor the version of ansible you are using. Although if it is /etc/ansible/hosts and you have a modern version of ansible, you can check to see what ansible sees about that file using ansible-inventory -i /path/to/the/inventory/file --listDrat
Separately, ansible_ssh_private_key_file=/home/ansibleuser/.ssh is incorrect as that points to a directory, and not to a file -- unless you have for some terrible reason named your file as .ssh in ansibleuser's home directoryDrat
I changed the ssh_priavtekey_file to /home/ansibleuser/.ssh/id_rsa. and tried the the commond ansible-inventory -i /path/to/the/inventory/file --list , got below response ...Vetchling
{ "_meta": { "hostvars": { "172.31.26.93": { "ansible_ssh_host": "172.31.26.93", "ansible_ssh_pass": "vagrant", "ansible_ssh_port": 22, "ansible_ssh_private_key_file": "/home/ansibleuser/.ssh/id_rsa", "ansible_ssh_user": "ansibleuser", "ansible_user": "vagrant" } } }, "a": { "hosts": [ "172.31.26.93" ] }, "all": { "children": [ "a", "ungrouped" ] }, "ungrouped": {} }Vetchling
ansible version is " 2.4.6.0" and he file that contains your group name is "hosts"Vetchling
D
26

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

[inventory]
enable_plugins = yaml, ini
Disproportion answered 26/1, 2021 at 14:44 Comment(1)
You just saved me a solution for weeks of effort. I could add a medal to this one. If i had known what to search for i would have found it faster, lol. keyword = multiple hosts, combine dynamic and static hosts. e.g. enable_plugins = aws_ec2, yaml, ini, inventory=./inventory and all inventory files, including aws_ec2.yml inside inventory/Sixth
G
25

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.

Groundless answered 22/4, 2019 at 2:36 Comment(0)
D
4

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

Derr answered 23/10, 2019 at 7:41 Comment(0)
M
1

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.

Michikomichon answered 30/7, 2020 at 14:10 Comment(2)
Mine was extra "/" at start of path "roles/jenkins/inventory/hosts.ini" was enoughNato
@Nato yes sometimes it's not easy to figure out what's wrong, Ansible error messages can be quite crypticMichikomichon
G
1

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.

Glossal answered 12/6, 2022 at 13:34 Comment(0)
T
1

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

Thinkable answered 18/8, 2022 at 19:58 Comment(0)
C
0

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
Cutthroat answered 20/1, 2019 at 18:2 Comment(3)
He was asking about dynamic inventory with script. He was not asking about manual host updatePoppy
A text file should not be chmod 777. It does not need execute permissions.Limpid
Read permissions should suffice.Michikomichon
K
0

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

Krissy answered 20/10, 2019 at 17:22 Comment(0)
W
0

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
Wallacewallach answered 7/3, 2021 at 2:52 Comment(0)
J
0

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

Jair answered 11/12, 2021 at 11:52 Comment(0)
T
0

The problem with mine was that, I try to commen someof the configuration. Which creates the problem. Just remove it and works fine.

I know its not specific solution of problem here, but just write it down to point out possible cause for other. Thanks

Thaler answered 1/4, 2023 at 19:52 Comment(0)
R
0

if your command ansible all --list-hosts works as expected then you might need to specify your host file. for example:

ansible all -i /etc/ansible/hosts -m copy -a 'src=suhaily.txt dest=/home/devops/'

this occurs if you are trying to execute command from some other location than /etc/ansible/

Riba answered 1/5, 2024 at 21:40 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.