variables in hostname doesn't work well with jinja2 templates

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 :slight_smile:

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

test.yml

`

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

raised a GitHub issue for the same :slight_smile: - https://github.com/ansible/ansible/issues/16565