# variables in hostname doesn't work well with jinja2 templates

**URL:** <https://forum.ansible.com/t/variables-in-hostname-doesnt-work-well-with-jinja2-templates/21910>\
**Category:** Ansible Project\
**Created:** [July 2, 2016, 4:17pm UTC](https://forum.ansible.com/t/variables-in-hostname-doesnt-work-well-with-jinja2-templates/21910 "2016-07-02T16:17:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aneesh\_Joseph](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Aneesh\_Joseph](https://forum.ansible.com/u/Aneesh_Joseph)\
**Post date:** [July 2, 2016, 4:17pm UTC](https://forum.ansible.com/t/variables-in-hostname-doesnt-work-well-with-jinja2-templates/21910/1 "2016-07-02T16:17:23Z")

</div>

I’m using variables within the inventory host name to provision environments dynamically using the ansible openstack modules and this is working well

`[openstack] myserver-{{env}}.subdomain.mycompany.com`

but, when I try to use these hostnames in a jinja2 template snippet, it’s giving me errors

`

- name: find list of openstack servers which  
set\_fact:  
openstack\_servers: “{% for host in groups[‘openstack’] %}{{ hostvars[host][‘inventory\_hostname’]{% if not loop.last %},{% endif %}{% endfor %}”  
`

Is there a way to make it working? Any help or references in this regard will be appreciated 🙂

To replicate this kind of error, run the below play (test.yml) with the below inventory file(inventory.txt)

test.yml

`

---

<div class="post-metadata">

**Author:** ![Aneesh\_Joseph](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Aneesh\_Joseph](https://forum.ansible.com/u/Aneesh_Joseph)\
**Post date:** [July 2, 2016, 7:51pm UTC](https://forum.ansible.com/t/variables-in-hostname-doesnt-work-well-with-jinja2-templates/21910/2 "2016-07-02T19:51:58Z")

</div>

looks like ansible lazily loads the **hostvars** dict with the raw hostname and doesn’t apply variable substitution, but the for other dicts like **groups** variable substitutions are applied. Is this a bug?

The hostvars dict has _ **myserver-{{env}}.subdomain.mycompany.** \_\_ **com** _ as the key instead of myserver-dev1.subdomain.mycompany. **com** and ansible fails with the error The error was: ‘ansible.vars.hostvars.HostVars object’ has no attribute u’[myserver-dev1.subdomain.mycompany.com](http://myserver-dev1.subdomain.mycompany.com)

---

<div class="post-metadata">

**Author:** ![Aneesh\_Joseph](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Aneesh\_Joseph](https://forum.ansible.com/u/Aneesh_Joseph)\
**Post date:** [July 4, 2016, 12:07pm UTC](https://forum.ansible.com/t/variables-in-hostname-doesnt-work-well-with-jinja2-templates/21910/3 "2016-07-04T12:07:30Z")

</div>

raised a GitHub issue for the same 🙂 - [https://github.com/ansible/ansible/issues/16565](https://github.com/ansible/ansible/issues/16565)
