Group Hostvars with --limit

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

There is not a way to do this currently.

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

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!

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

Are there plans to allow it Michael?

I’ve just been bitten by the same behaviour…

https://gist.github.com/phips/2a51a5d32fdff4dcb719

Cheers,

–Mark

This is the ‘fact caching’ discussion.

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