The error was: 'inventory_dir' is undefined

1,202 views
Skip to first unread message

zheng...@sohu.com

unread,
Nov 29, 2019, 3:37:48 AM11/29/19
to Ansible Project

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

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


Stefan Hornburg (Racke)

unread,
Nov 29, 2019, 4:13:51 AM11/29/19
to ansible...@googlegroups.com
On 11/29/19 9:37 AM, zheng...@sohu.com wrote:
>
> 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
Racke

>
> 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
>
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/8c51eab8-5ac0-4987-9f50-253ecdc412a3%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/8c51eab8-5ac0-4987-9f50-253ecdc412a3%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Vladimir Botka

unread,
Nov 29, 2019, 4:14:50 AM11/29/19
to zheng...@sohu.com, ansible...@googlegroups.com
On Fri, 29 Nov 2019 00:37:48 -0800 (PST)
"zheng...@sohu.com" <zheng...@sohu.com> wrote:

> - 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
> [...]
> [localhost]: FAILED! => {"msg": "The task includes an option with an
> undefined variable. The error was: 'inventory_dir' is undefined

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

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*

inventory file:
localhost

[master]
192.168.0.1

[all:vars]
host_yum_repos_file_path="{{inventory_dir}}/resources/yumrepos"

# (closing quotation is missing in the example)

Cheers,

-vlado
Reply all
Reply to author
Forward
0 new messages