# The error was: 'inventory\_dir' is undefined

**URL:** <https://forum.ansible.com/t/the-error-was-inventory-dir-is-undefined/31748>\
**Category:** Ansible Project\
**Tags:** kubernetes\
**Created:** [November 29, 2019, 8:37am UTC](https://forum.ansible.com/t/the-error-was-inventory-dir-is-undefined/31748 "2019-11-29T08:37:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhengqisong](https://avatars.discourse-cdn.com/v4/letter/z/3bc359/32.png) [@zhengqisong](https://forum.ansible.com/u/zhengqisong)\
**Post date:** [November 29, 2019, 8:37am UTC](https://forum.ansible.com/t/the-error-was-inventory-dir-is-undefined/31748/1 "2019-11-29T08:37:48Z")

</div>

ansible version: 2.7.4systemos : centos7

playbook:

- name: local init  
hosts: localhost

tasks:

- name: test  
debug: msg=“{{ host\_yum\_repos\_file\_path }}”

inventory file:

[master]  
192.168.0.1

[all:vars]  
host\_yum\_repos\_file\_path="{{inventory\_dir}}/resources/yumrepos

ansible-playbook -i inventoryfile planybook.yml

PLAY [local init] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* TASK [Gathering Facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ok: [localhost] TASK [test] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* fatal: [localhost]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: ‘inventory\_dir’ is undefined\n\nThe error appears to have been in ‘/media/sf\_workspace/earth/ansible-k8s-tst/playbooks/testplay.yaml’: line 19, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: test\n ^ here\n”} PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* localhost : ok=1 changed=0 unreachable=0 failed=1

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [November 29, 2019, 9:13am UTC](https://forum.ansible.com/t/the-error-was-inventory-dir-is-undefined/31748/2 "2019-11-29T09:13:20Z")

</div>

> ansible version: 2.7.4  
> systemos : centos7
> 
> playbook:  
> - name: local init  
> hosts: localhost  
> tasks:  
> - name: test  
> debug: msg="{{ host\_yum\_repos\_file\_path }}"
> 
> inventory file:  
> [master]  
> 192.168.0.1
> 
> [all:vars]  
> host\_yum\_repos\_file\_path="{{inventory\_dir}}/resources/yumrepos

Do you really miss the closing " here?

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [November 29, 2019, 9:14am UTC](https://forum.ansible.com/t/the-error-was-inventory-dir-is-undefined/31748/3 "2019-11-29T09:14:36Z")

</div>

Quoting from \*Special variables\*:  
"inventory\_dir: The directory of the inventory source in which the  
inventory\_hostname was first defined"  
[https://docs.ansible.com/ansible/latest/reference\_appendices/special\_variables.html#special-variables](https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html#special-variables)

The inventory hostname \*localhost\* hasn't been defined in any inventory  
source hence \*inventory\_dir\* is undefined.

It can be fixed for example by putting \*localhost\* into the \*inventoryfile\*

&nbsp;&nbsp;inventory file:  
&nbsp;&nbsp;localhost

&nbsp;&nbsp;[master]  
&nbsp;&nbsp;192.168.0.1  
&nbsp;&nbsp;  
&nbsp;&nbsp;[all:vars]  
&nbsp;&nbsp;host\_yum\_repos\_file\_path="{{inventory\_dir}}/resources/yumrepos"

&nbsp;&nbsp;# (closing quotation is missing in the example)

Cheers,

&nbsp;&nbsp;-vlado
