# Group Hostvars with --limit

**URL:** <https://forum.ansible.com/t/group-hostvars-with-limit/14548>\
**Category:** Ansible Project\
**Created:** [February 12, 2014, 2:36am UTC](https://forum.ansible.com/t/group-hostvars-with-limit/14548 "2014-02-12T02:36:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_O\_Connel](https://avatars.discourse-cdn.com/v4/letter/c/6bbea6/32.png) [@Christopher\_O\_Connel](https://forum.ansible.com/u/Christopher_O_Connel)\
**Post date:** [February 12, 2014, 2:36am UTC](https://forum.ansible.com/t/group-hostvars-with-limit/14548/1 "2014-02-12T02:36:50Z")

</div>

Hello all,

I’m currently wrestling with creating DNS zonefiles when running a playbook which uses tempates to set NS authority records based on the members of the ‘dns’ group.

Consider the template snippet:

{% for host in groups[‘dns-edge’] %}  
{% if hostvars[host][‘ansible\_default\_ipv4’] is defined %}  
{{ domain }} IN NS {{ hostvars[host][‘ansible\_default\_ipv4’][‘address’] }}  
{% endif %}  
{% endfor %}

You’ll notice the extra if statement in there. The problem is that if --limit is applied when the playbook is run, it only will render out a single NS record instead of the whole list of machines in the dns group, since gather\_facts hasn’t been run on those machines.

Question: Is there anyway to force ansible to run gather facts on the whole group even if limit is applied on the command line.  
Follow up: If not, can anyone think of a way of doing this without defining some global like “dns\_authoritative\_server” containing a static list?

All the best,

~ Christopher

---

<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:** [February 12, 2014, 1:21pm UTC](https://forum.ansible.com/t/group-hostvars-with-limit/14548/2 "2014-02-12T13:21:26Z")

</div>

There is not a way to do this currently.

---

<div class="post-metadata">

**Author:** ![Walid](https://avatars.discourse-cdn.com/v4/letter/w/ea5d25/32.png) [@Walid](https://forum.ansible.com/u/Walid)\
**Post date:** [February 12, 2014, 4:35pm UTC](https://forum.ansible.com/t/group-hostvars-with-limit/14548/3 "2014-02-12T16:35:30Z")

</div>

What if the task for zone file templates is a in a later play where you might limit the targeted hosts by an external variable you pass from the ansible-playbook command, and run another prior play for all hosts that you need in another play above it. not a clean solution, but will possibly work

---

<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:** [February 12, 2014, 4:37pm UTC](https://forum.ansible.com/t/group-hostvars-with-limit/14548/4 "2014-02-12T16:37:04Z")

</div>

Yep, I was just thinking about that.

Here’s a sketch!

- hosts: all  
tasks: # gather facts only

- hosts: “all:{{subset}}”  
gather\_facts: False  
roles:

- do\_stuff

ansible-playbook site.yml -e “subset=region3”

That will do it!

---

<div class="post-metadata">

**Author:** ![Christopher\_O\_Connel](https://avatars.discourse-cdn.com/v4/letter/c/6bbea6/32.png) [@Christopher\_O\_Connel](https://forum.ansible.com/u/Christopher_O_Connel)\
**Post date:** [February 17, 2014, 2:40am UTC](https://forum.ansible.com/t/group-hostvars-with-limit/14548/5 "2014-02-17T02:40:36Z")

</div>

Thanks!

I found another solution (wrote a custom module to load the values I need), but this looks like it should work.

In the longer run, it might make sense to have a variable on a play called something like “additional\_gather\_facts” to allow a broader set of facts to be gathered than the hosts the play will run on.

All the best,

~ Christopher

---

<div class="post-metadata">

**Author:** ![Mark\_Phillips](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@Mark\_Phillips](https://forum.ansible.com/u/Mark_Phillips)\
**Post date:** [June 16, 2014, 6:18pm UTC](https://forum.ansible.com/t/group-hostvars-with-limit/14548/6 "2014-06-16T18:18:42Z")

</div>

Are there plans to allow it Michael?

I’ve just been bitten by the same behaviour…

[https://gist.github.com/phips/2a51a5d32fdff4dcb719](https://gist.github.com/phips/2a51a5d32fdff4dcb719)

Cheers,

–Mark

---

<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 17, 2014, 12:49pm UTC](https://forum.ansible.com/t/group-hostvars-with-limit/14548/7 "2014-06-17T12:49:35Z")

</div>

This is the ‘fact caching’ discussion.

I want to have something here but we need to smite various P2 and P3 backlog items first.
