# inventory not being able to read when using ansible 2 api

**URL:** <https://forum.ansible.com/t/inventory-not-being-able-to-read-when-using-ansible-2-api/22866>\
**Category:** Ansible Project\
**Created:** [October 24, 2016, 1:31pm UTC](https://forum.ansible.com/t/inventory-not-being-able-to-read-when-using-ansible-2-api/22866 "2016-10-24T13:31:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oguz\_Yarimtepe](https://avatars.discourse-cdn.com/v4/letter/o/d07c76/32.png) [@Oguz\_Yarimtepe](https://forum.ansible.com/u/Oguz_Yarimtepe)\
**Post date:** [October 24, 2016, 1:31pm UTC](https://forum.ansible.com/t/inventory-not-being-able-to-read-when-using-ansible-2-api/22866/1 "2016-10-24T13:31:56Z")

</div>

Here is my structure.

✗ tree -L 2  
.  
├── ansible.cfg  
├── ansible.cfg.bak  
├── group\_vars  
│ ├── default  
│ └── default.bak  
├── hosts  
├── playbook.yml  
├── playbook\_hostname.yml  
├── playbook\_ip.yml  
├── roles  
│ ├── hostname  
│ ├── ip  
│ └── satellite

When i run the below code, i am not able to connect to the remote machine. When i check with tcpdump, no connection request is sent, it seems somehow no connection details are read from inventory files.

This is the error i saw when i install the ansible 2.0.0.1

raise AnsibleError(“no host vars as host is not in inventory: %s” % hostname)  
ansible.errors.AnsibleError: ERROR! no host vars as host is not in inventory: 10.35.75.247

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [October 24, 2016, 1:37pm UTC](https://forum.ansible.com/t/inventory-not-being-able-to-read-when-using-ansible-2-api/22866/2 "2016-10-24T13:37:30Z")

</div>

You don’t show how/where you define `self.hostname_hosts`.

---

<div class="post-metadata">

**Author:** ![Oguz\_Yarimtepe](https://avatars.discourse-cdn.com/v4/letter/o/d07c76/32.png) [@Oguz\_Yarimtepe](https://forum.ansible.com/u/Oguz_Yarimtepe)\
**Post date:** [October 24, 2016, 1:42pm UTC](https://forum.ansible.com/t/inventory-not-being-able-to-read-when-using-ansible-2-api/22866/3 "2016-10-24T13:42:09Z")

</div>

Here is the self variables. They are read from a settings.yml and are set during execution

```auto
self.data = data

self.host = data['host']
self.hostname = data['hostname']
self.conn_name = data['conn_name']
if ipam.ip:
    self.guest_ip4_address = ipam.restart_ip
else:
    self.guest_ip4_address = data['guest_ip4_address']

if ipam.ip:
    self.guest_ip4_address_for_restart = ipam.ip
else:
    self.guest_ip4_address_for_restart = data['guest_ip4_address_for_restart']

if [ipam.gw](http://ipam.gw):
    self.guest_gw4_address = [ipam.gw](http://ipam.gw)
else:
    self.guest_gw4_address = data['guest_gw4_address']

self.template_ip = data['template_ip']

self.host = data['host']
self.hostname_hosts = data['hostname_hosts']
self.hostname_playbook_path = data['hostname_playbook']
self.ip_playbook_path = data['ip_playbook']
self.logger = logger

```

```auto

```

```auto
and at the yml file here is the line:

```

```auto

```

---

<div class="post-metadata">

**Author:** ![Oguz\_Yarimtepe](https://avatars.discourse-cdn.com/v4/letter/o/d07c76/32.png) [@Oguz\_Yarimtepe](https://forum.ansible.com/u/Oguz_Yarimtepe)\
**Post date:** [October 24, 2016, 4:22pm UTC](https://forum.ansible.com/t/inventory-not-being-able-to-read-when-using-ansible-2-api/22866/4 "2016-10-24T16:22:28Z")

</div>

I realized that i haven’t set the ansible.cfg file path. My cli app is at another path and although i set the inventory variable there, nothing changed. Still not able to connect to the remote host. It is somehow not able to connect, not able to understand the user and password vars defined at the inventory.

---

<div class="post-metadata">

**Author:** ![Oguz\_Yarimtepe](https://avatars.discourse-cdn.com/v4/letter/o/d07c76/32.png) [@Oguz\_Yarimtepe](https://forum.ansible.com/u/Oguz_Yarimtepe)\
**Post date:** [October 24, 2016, 6:14pm UTC](https://forum.ansible.com/t/inventory-not-being-able-to-read-when-using-ansible-2-api/22866/5 "2016-10-24T18:14:00Z")

</div>

It seems that the problem is definetly about reading the inventory. at the /Users/oyarimtepe/Dev/vmware/command-center/cc-cli/venv/lib/python2.7/site-packages/ansible/inventory/ **init**.py

```auto
def get_host(self, hostname):
    if hostname not in self._hosts_cache:
        self._hosts_cache[hostname] = self._get_host(hostname)
**..**

```

```auto

```

```auto
**hosts_cache is empty.** 

```

```auto
Thats why it is throwing exception. Seems i am doing something wrong about defining an Inventory as host.

```

```auto

```

```auto
Here is my hosts file /Users/oyarimtepe/Dev/vmware/command-center/register/hosts

```

```auto
[local]
127.0.0.1 ansible_python_interpreter=python

[default]
10.35.75.247 ansible_python_interpreter=python ansible_user=root ansible_ssh_pass=xxxx

```

```auto

```

```auto
I set it at 

```

```auto

```
