# Is it possible to use variable in inventory

**URL:** <https://forum.ansible.com/t/is-it-possible-to-use-variable-in-inventory/19146>\
**Category:** Ansible Project\
**Created:** [August 30, 2015, 10:23am UTC](https://forum.ansible.com/t/is-it-possible-to-use-variable-in-inventory/19146 "2015-08-30T10:23:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chenxiong\_Qi](https://avatars.discourse-cdn.com/v4/letter/c/0ea827/32.png) [@Chenxiong\_Qi](https://forum.ansible.com/u/Chenxiong_Qi)\
**Post date:** [August 30, 2015, 10:23am UTC](https://forum.ansible.com/t/is-it-possible-to-use-variable-in-inventory/19146/1 "2015-08-30T10:23:34Z")

</div>

I would like to reuse some variables defined under a role in an inventory file. For example,

a file inventory/myhost, it contains following line

myhost ansible\_ssh\_host={{ myhost\_ip\_address }}

By reading Ansible documentation and searching possible solutions, I did not get a solution so far. I’m wondering if it’s possible to use variable in inventory. Your help will be much appreciated. Thanks!

Regards,  
Chenxiong Qi

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [August 30, 2015, 5:16pm UTC](https://forum.ansible.com/t/is-it-possible-to-use-variable-in-inventory/19146/2 "2015-08-30T17:16:22Z")

</div>

​the variable ​  
ansible\_ssh\_hos  
​t​ will just be available for that host

you can also access it from another host as  
hostvars.myhost.ansible\_ssh\_host or hostvars['myhost']['ansible\_ssh\_host']

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [August 30, 2015, 5:45pm UTC](https://forum.ansible.com/t/is-it-possible-to-use-variable-in-inventory/19146/3 "2015-08-30T17:45:23Z")

</div>

In this case, you should be able to use it.

In general, you just have to be mindful of where a variable gets used  
and templated and where it gets defined.

Since connection variables get used after role imports, this should  
not be a problem.
