# Ansible read variable defined in inventory from playbook yaml file.

**URL:** <https://forum.ansible.com/t/ansible-read-variable-defined-in-inventory-from-playbook-yaml-file/27994>\
**Category:** Ansible Project\
**Created:** [July 9, 2018, 1:09pm UTC](https://forum.ansible.com/t/ansible-read-variable-defined-in-inventory-from-playbook-yaml-file/27994 "2018-07-09T13:09:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Madushan\_Chathuranga](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@Madushan\_Chathuranga](https://forum.ansible.com/u/Madushan_Chathuranga)\
**Post date:** [July 9, 2018, 1:09pm UTC](https://forum.ansible.com/t/ansible-read-variable-defined-in-inventory-from-playbook-yaml-file/27994/1 "2018-07-09T13:09:26Z")

</div>

Need some help regarding reading on vars from inventory…

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [July 9, 2018, 5:18pm UTC](https://forum.ansible.com/t/ansible-read-variable-defined-in-inventory-from-playbook-yaml-file/27994/2 "2018-07-09T17:18:11Z")

</div>

Your inventory looks wrong, I'm not sure if you are going for a 'yaml'  
format one?

In any case, accessing variables would just be using the variable  
name, for example:

- debug: msg='{{username}}'

but for that to work the file would need to look like this (assuming  
you want the vars fro every host in the group):

groupname:  
&nbsp;&nbsp;&nbsp;hosts:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;localhost:  
&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;username: myusername  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;password: mypassword

---

<div class="post-metadata">

**Author:** ![Madushan\_Chathuranga](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@Madushan\_Chathuranga](https://forum.ansible.com/u/Madushan_Chathuranga)\
**Post date:** [July 9, 2018, 5:38pm UTC](https://forum.ansible.com/t/ansible-read-variable-defined-in-inventory-from-playbook-yaml-file/27994/3 "2018-07-09T17:38:46Z")

</div>

Hi Brain,

Thank you. yes I was going for a yaml formatted inventory for this example but I wouldn’t mind even if it is a ini file. I tried as you have mentioned. It gives an an error as failed to connect to host via ssh : permission denied. I actually don’t want to connect to any hosts. what I want is access the username, password variables from inventory rather than passing them as --extra-vars.

is accesing using hostvars[‘hosatname’].username different to this.

Is it possible to access any variable in inventory just only using the variable name ?

---

<div class="post-metadata">

**Author:** ![Madushan\_Chathuranga](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@Madushan\_Chathuranga](https://forum.ansible.com/u/Madushan_Chathuranga)\
**Post date:** [July 9, 2018, 5:57pm UTC](https://forum.ansible.com/t/ansible-read-variable-defined-in-inventory-from-playbook-yaml-file/27994/4 "2018-07-09T17:57:15Z")

</div>

II’m trying to create a inventory dynamically base on this.

“group”: {  
“hosts”: [  
“localhost”  
],  
“vars”:  
{  
“username” : “myusername”,  
“password” : “mypassword”  
}  
},  
“\_meta”: {  
“hostvars”: {  
“localhost”: {  
“ansible\_connection”: “ssh”,

}  
}  
}

What I’m trying to then is access username ans password variables from playbook yaml file

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [July 9, 2018, 9:29pm UTC](https://forum.ansible.com/t/ansible-read-variable-defined-in-inventory-from-playbook-yaml-file/27994/5 "2018-07-09T21:29:35Z")

</div>

As i said, accessing the variables is just using them by name.

The problem on 'connection' is that you defined localhost and must  
have not set gather\_facts: no.

The 'form' of the inventory does not matter as long as it is a valid inventory.
