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?
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
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.