# How to access an host variable defined in inventory

**URL:** <https://forum.ansible.com/t/how-to-access-an-host-variable-defined-in-inventory/15383>\
**Category:** Ansible Project\
**Created:** [June 1, 2014, 2:28pm UTC](https://forum.ansible.com/t/how-to-access-an-host-variable-defined-in-inventory/15383 "2014-06-01T14:28:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Servermanaged](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@Servermanaged](https://forum.ansible.com/u/Servermanaged)\
**Post date:** [June 1, 2014, 2:28pm UTC](https://forum.ansible.com/t/how-to-access-an-host-variable-defined-in-inventory/15383/1 "2014-06-01T14:28:29Z")

</div>

Hello friends  
maybe a noob question but I’m unable to get host variable work as expected. This is my question.

Given this template:

object Host “{{ hostvars[item][‘ansible\_fqdn’] }}” {  
import “generic-host”  
address = “{{ hostvars[item][‘ansible\_eth0’][‘ipv4’][‘address’] }}”  
vars.os = “Linux”  
vars.sla = “24x7”  
vars.http = “yes”  
vars.smtp = “yes”  
}

generated by this task:

- name: Copy Host Definitions  
template: src=host\_def.j2  
dest={{ icinga2\_hosts\_dir }}{{ hostvars[item][‘ansible\_fqdn’] }}.conf  
owner=root  
group=root  
mode=0644  
with\_items: groups[‘monitored’]  
notify:
- restart icinga2

How I can access an host variable eventually defined in inventory?

[monitored:children]  
myservers

[myserver]  
host1 vars\_http=no

What I want to do is something like this:

object Host “{{ hostvars[item][‘ansible\_fqdn’] }}” {  
import “generic-host”  
address = “{{ hostvars[item][‘ansible\_eth0’][‘ipv4’][‘address’] }}”  
vars.os = “Linux”  
vars.sla = “24x7”  
{% if vars\_http == “no” %}  
vars.http = “no”  
{% else %}  
vars.http = “yes”  
{% endif %}  
vars.smtp = “yes”  
}

Thanks for support.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [June 1, 2014, 7:09pm UTC](https://forum.ansible.com/t/how-to-access-an-host-variable-defined-in-inventory/15383/2 "2014-06-01T19:09:19Z")

</div>

Since you appear to be walking over “hostvars” are you trying to have a template that includes the variables for every host in a certain group?

If so, we know how to do that.

Let us know.
