Hi All,

I am trying to understand the changes with Ansible 2.4 that affects the 
usage of inventory_dir variable.

Earlier i was using a static host file and in my playbooks, i was heavily 
relying on inventory_dir to load variable files which in turn was loading 
the target hosts. I understand the purpose of making the inventory_dir as a 
hostvar, what i did not understand is *how can i still access inventory_dir 
directly as a global variable in my playbooks* *?*

To show what i mean, i created a little demo:

local1 localhost ansible_connection=local
local2 localhost ansible_connection=local

#This doesn't work anymore
#moreVars: "{{ inventory_dir ~ 'moreVars.yaml' }}"

#This also works
#moreVars: "{{hostvars[groups.all.0].inventory_dir}}/moreVars.yaml"

#This works and this is what i was using before 2.4
moreVars: "{{inventory_dir}}/moreVars.yaml"

#Prior to 2.4, this is how i defined target hosts. I understand this will 
not work anymore
targetHosts: locals


- hosts: locals
        - varsFile.yaml
        - "{{moreVars}}"


     - debug: var=who

     - name: Accessing the inventory_dir as a host var
       debug: msg="Accessing inventory_dir as hostvar -- 

     - name: Accessing the inventory DIR directly
       debug: msg="Accessing inventory_dir directly -- {{inventory_dir}}"


*$ ansible --versionansible$ ansible-playbook playbook.yaml -i 
hosts.infoPLAY [locals] 
[Gathering Facts] 
[local1]ok: [local2]TASK [debug] 
[local1] => {    "who": "MORE_VARS"}ok: [local2] => {    "who": 
"MORE_VARS"}TASK [Accessing the inventory_dir as a host var] 
[local1] => {    "msg": "Accessing inventory_dir as hostvar -- 
/home/ij/temp"}ok: [local2] => {    "msg": "Accessing inventory_dir as 
hostvar -- /home/ij/temp"}TASK [Accessing the inventory DIR directly] 
[local1] => {    "msg": "Accessing inventory_dir directly -- 
/home/ij/temp"}ok: [local2] => {    "msg": "Accessing inventory_dir 
directly -- /home/ij/temp"}PLAY RECAP 
                   : ok=4    changed=0    unreachable=0    failed=0local2  
                   : ok=4    changed=0    unreachable=0    failed=0*

*So my questions:*
*1) How is inventory_dir still available as global var ?*
*2) Can i continue using it directly or i should 
use hostvars[groups.all.0].inventory_dir ?*

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-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
For more options, visit https://groups.google.com/d/optout.

Reply via email to