# This isn't working for me  -hosts: {{ hostvars\['localhost'\]\['mylist'\] }}   :   hostvars is undefined

**URL:** <https://forum.ansible.com/t/this-isnt-working-for-me-hosts-hostvars-localhost-mylist-hostvars-is-undefined/21145>\
**Category:** Ansible Project\
**Created:** [April 12, 2016, 8:12pm UTC](https://forum.ansible.com/t/this-isnt-working-for-me-hosts-hostvars-localhost-mylist-hostvars-is-undefined/21145 "2016-04-12T20:12:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Larry\_Fast](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@Larry\_Fast](https://forum.ansible.com/u/Larry_Fast)\
**Post date:** [April 12, 2016, 8:12pm UTC](https://forum.ansible.com/t/this-isnt-working-for-me-hosts-hostvars-localhost-mylist-hostvars-is-undefined/21145/1 "2016-04-12T20:12:23Z")

</div>

I’m trying to dynamically build the group of hosts that will be used in a play. I’m doing this within a hosts: localhost play. However hostvars seems to be undefined when evaluating the - hosts: line. Suggestions?

---

<div class="post-metadata">

**Author:** ![viper233](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/viper233/32/2060_2.png) [@viper233](https://forum.ansible.com/u/viper233)\
**Post date:** [April 12, 2016, 8:51pm UTC](https://forum.ansible.com/t/this-isnt-working-for-me-hosts-hostvars-localhost-mylist-hostvars-is-undefined/21145/2 "2016-04-12T20:51:27Z")

</div>

Not being helpful, but re-enforcing the (mis)behaviour using Ansible 2.0.2

For my case I was calling sts\_assume\_role on localhost (hosts:localhost) for get a different aws id, key and token seeing it’s only an api call, then setting then using set\_facts to set variables. In another play in the same playbook I called it against another host (host:foobar) and tried to reference hostvars[‘localhost’][‘aws\_cred\_id’] .The variable is not defined was my result.

I managed to work around it just by calling the sts\_assume\_role task on the remote host.

Is this the expected behaviour? I thought magic variables could be used for this? is it only for retrieving group file variables?

[http://docs.ansible.com/ansible/playbooks\_variables.html#magic-variables-and-how-to-access-information-about-other-hosts](http://docs.ansible.com/ansible/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts)

---

<div class="post-metadata">

**Author:** ![Larry\_Fast](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@Larry\_Fast](https://forum.ansible.com/u/Larry_Fast)\
**Post date:** [April 12, 2016, 9:39pm UTC](https://forum.ansible.com/t/this-isnt-working-for-me-hosts-hostvars-localhost-mylist-hostvars-is-undefined/21145/3 "2016-04-12T21:39:42Z")

</div>

Thanks Steve. This sounds very much like what I’m trying to do. In my case I want to enforce some rules, defaults and aliases on a host group selected via --extra-vars. Thought I would do that with a localhost Play and set\_facts, as you described. If this is the wrong approach I’m happy to get steered in the right direction. I started looking at dynamic inventory but this seems to be overkill for my current need. It also feels like the wrong direction. Another option I’m considering is a jinja2 function. Eg. - hosts: "{{ myj2func() }}

---

<div class="post-metadata">

**Author:** ![viper233](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/viper233/32/2060_2.png) [@viper233](https://forum.ansible.com/u/viper233)\
**Post date:** [April 12, 2016, 9:47pm UTC](https://forum.ansible.com/t/this-isnt-working-for-me-hosts-hostvars-localhost-mylist-hostvars-is-undefined/21145/4 "2016-04-12T21:47:13Z")

</div>

Just want to make sure you’ve considered add\_host  
[http://docs.ansible.com/ansible/add\_host\_module.html](http://docs.ansible.com/ansible/add_host_module.html)

---

<div class="post-metadata">

**Author:** ![Larry\_Fast](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@Larry\_Fast](https://forum.ansible.com/u/Larry_Fast)\
**Post date:** [April 12, 2016, 10:08pm UTC](https://forum.ansible.com/t/this-isnt-working-for-me-hosts-hostvars-localhost-mylist-hostvars-is-undefined/21145/5 "2016-04-12T22:08:16Z")

</div>

Hmm, looks promising but seems to be the opposite of what I want. I would want to create a new group ID that references an existing group. Another solution would be to dynamically create an extra-var or similar root/global scoped variable. Is there anything like set\_extra\_var with behaviour similar to set\_fact? This is pushing back toward the functionality that works:

- hosts: “{{ extra\_var }}” I tested this and it works
- hosts: “{{ dynamically\_created\_extra\_var }}” Does this exist?
