Is it possible to use variable in inventory

25 views
Skip to first unread message

Chenxiong Qi

unread,
Aug 30, 2015, 1:12:53 PM8/30/15
to Ansible Project
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

Serge van Ginderachter

unread,
Aug 30, 2015, 1:16:47 PM8/30/15
to ansible...@googlegroups.com

On 30 August 2015 at 12:23, Chenxiong Qi <qcx...@gmail.com> wrote:
a file inventory/myhost, it contains following line

myhost ansible_ssh_host={{ myhost_ip_address }}

​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']

Brian Coca

unread,
Aug 30, 2015, 1:45:28 PM8/30/15
to Ansible Project
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.


--
Brian Coca
Reply all
Reply to author
Forward
0 new messages