The error was: 'inventory_dir' is undefined

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

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

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