inventory_dir is still present as a global variable in Ansible 2.4 ?

990 views
Skip to first unread message

ishan jain

unread,
Mar 7, 2018, 6:24:36 AM3/7/18
to Ansible Project
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:

[locals]
local1 localhost ansible_connection=local
local2 localhost ansible_connection=local



varsFile.yaml
#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"



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

who: MORE_VARS



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

  tasks:


     - debug: var=who

     - name: Accessing the inventory_dir as a host var
       debug: msg="Accessing inventory_dir as hostvar -- {{hostvars[groups.all.0].inventory_dir}}"

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




output
$ ansible --version
ansible 2.4.3.0
$ ansible-playbook playbook.yaml -i hosts.info

PLAY [locals] ***************************************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************************************
ok: [local1]
ok: [local2]

TASK [debug] ****************************************************************************************************************************************************************************************
ok: [local1] => {
    "who": "MORE_VARS"
}
ok: [local2] => {
    "who": "MORE_VARS"
}

TASK [Accessing the inventory_dir as a host var] ****************************************************************************************************************************************************
ok: [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] *********************************************************************************************************************************************************
ok: [local1] => {
    "msg": "Accessing inventory_dir directly -- /home/ij/temp"
}
ok: [local2] => {
    "msg": "Accessing inventory_dir directly -- /home/ij/temp"
}

PLAY RECAP ******************************************************************************************************************************************************************************************
local1                     : ok=4    changed=0    unreachable=0    failed=0
local2                     : 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 ?

Matt Martz

unread,
Mar 7, 2018, 12:58:36 PM3/7/18
to ansible...@googlegroups.com
Please see http://docs.ansible.com/ansible/latest/porting_guide_2.4.html#inventory for information about the change to 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+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/96846820-85b0-4aee-ac09-fb4062d9412f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net
Reply all
Reply to author
Forward
0 new messages