# Force gathering facts on all hosts when using --tags or --limit

**URL:** <https://forum.ansible.com/t/force-gathering-facts-on-all-hosts-when-using-tags-or-limit/13446>\
**Category:** Ansible Project\
**Created:** [September 10, 2013, 1:22pm UTC](https://forum.ansible.com/t/force-gathering-facts-on-all-hosts-when-using-tags-or-limit/13446 "2013-09-10T13:22:34Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nick\_Groenen](https://avatars.discourse-cdn.com/v4/letter/n/c77e96/32.png) [@Nick\_Groenen](https://forum.ansible.com/u/Nick_Groenen)\
**Post date:** [September 10, 2013, 1:22pm UTC](https://forum.ansible.com/t/force-gathering-facts-on-all-hosts-when-using-tags-or-limit/13446/1 "2013-09-10T13:22:34Z")

</div>

I have a very large playbook which configures our entire  
infrastructure. Because of this, various steps are tagged so that only  
specific parts of the playbook can be run, cutting down on runtime  
when required.

Parts of this setup use facts/hostvars to automatically create correct  
configuration files. For example, nginx config adding all the  
application servers that are defined in the inventory to the correct  
upstream definitions, and iptables on the appservers automatically  
opening up the correct ports to the loadbalancers.

However, when running the playbook with --limit, or --tags, not all  
hosts are contacted, and as a result, facts aren't available on every  
system in the infrastructure. This causes all kinds of problems for my  
setup, obviously.

Is there any way to force gathering of facts on all hosts, even when  
specifying one of these options? Or another way to deal with this  
situation that I haven't thought of?

Right now, I'm solving it for the --tags case by having one task at  
the start of the playbook, which simply calls the ping module and has  
every tag that's used listed. This way, this task is kicked off no  
matter which tag is specified, causing facts to be gathered on every  
system in our inventory.

Obviously, this isn't a practical solution however, nor does it solve  
the case where limit it used.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 10, 2013, 2:00pm UTC](https://forum.ansible.com/t/force-gathering-facts-on-all-hosts-when-using-tags-or-limit/13446/2 "2013-09-10T14:00:33Z")

</div>

Fact caching is something we want to look into for the 1.4 release.

---

<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:** [September 20, 2013, 9:05pm UTC](https://forum.ansible.com/t/force-gathering-facts-on-all-hosts-when-using-tags-or-limit/13446/3 "2013-09-20T21:05:18Z")

</div>

Hi,

How about being able to specify which facts you need back ( some sort of white/black listing) so that in a large scale infrastructure that does not cause any extra overhead on the network nor on the Ansible managment/console where these data gets reported back.

kind regards

Walid

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [September 20, 2013, 9:17pm UTC](https://forum.ansible.com/t/force-gathering-facts-on-all-hosts-when-using-tags-or-limit/13446/4 "2013-09-20T21:17:44Z")

</div>

@walid, setup module has this option, use the filters, from the examples:

ansible all -m setup -a ‘filter=ansible\_eth[0-2]’

---

<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:** [September 21, 2013, 6:48am UTC](https://forum.ansible.com/t/force-gathering-facts-on-all-hosts-when-using-tags-or-limit/13446/5 "2013-09-21T06:48:37Z")

</div>

Thank you Brian, my mistake i should have read the documentation first. this is perfect, thanks

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 21, 2013, 1:23pm UTC](https://forum.ansible.com/t/force-gathering-facts-on-all-hosts-when-using-tags-or-limit/13446/6 "2013-09-21T13:23:52Z")

</div>

Hi Walid,

It’s also true that there really isn’t a ginormous amount of data coming back from the facts modules, so it shouldn’t be a problem.

If you are writing some modules of your own, you’ll likely have a few facts modules that you might choose to not call on a case by case basis if they are intensive (walid\_facts, etc)

–Michael

---

<div class="post-metadata">

**Author:** ![Kerry\_Kurian](https://avatars.discourse-cdn.com/v4/letter/k/7993a0/32.png) [@Kerry\_Kurian](https://forum.ansible.com/u/Kerry_Kurian)\
**Post date:** [January 10, 2014, 7:20pm UTC](https://forum.ansible.com/t/force-gathering-facts-on-all-hosts-when-using-tags-or-limit/13446/7 "2014-01-10T19:20:01Z")

</div>

Have you found a way to solve this for the --limit case?

---

<div class="post-metadata">

**Author:** ![Nick\_Groenen](https://avatars.discourse-cdn.com/v4/letter/n/c77e96/32.png) [@Nick\_Groenen](https://forum.ansible.com/u/Nick_Groenen)\
**Post date:** [January 13, 2014, 4:52pm UTC](https://forum.ansible.com/t/force-gathering-facts-on-all-hosts-when-using-tags-or-limit/13446/8 "2014-01-13T16:52:35Z")

</div>

No I haven't. In our case, we're simply not using --limit for the time  
being, and have our tags set up to still allow pretty granular  
targeting. Fact caching is going to help somewhat with this problem,  
once it's implemented.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [January 13, 2014, 4:59pm UTC](https://forum.ansible.com/t/force-gathering-facts-on-all-hosts-when-using-tags-or-limit/13446/9 "2014-01-13T16:59:22Z")

</div>

implementing the offline caches might solve this as most host data should be available from previous runs.

---

<div class="post-metadata">

**Author:** ![Strahinja\_Kustudic2](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@Strahinja\_Kustudic2](https://forum.ansible.com/u/Strahinja_Kustudic2)\
**Post date:** [March 14, 2014, 4:28pm UTC](https://forum.ansible.com/t/force-gathering-facts-on-all-hosts-when-using-tags-or-limit/13446/10 "2014-03-14T16:28:46Z")

</div>

I know this is now and old topic, but it didn’t make sense to open a new one.

Having offline cache would be nice, but wouldn’t be easier to make something like:

**gather\_facts: force** or **gather\_facts\_**** force: True**

or something similar, so that this overrides --limit / --tags and always gathers facts.

---

<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:** [March 14, 2014, 4:42pm UTC](https://forum.ansible.com/t/force-gathering-facts-on-all-hosts-when-using-tags-or-limit/13446/11 "2014-03-14T16:42:26Z")

</div>

We’ve discussed this and what we want to do with gather\_facts is make a config setting

gather\_facts\_tendancy: always or lazy, default always

and then if you want to force when it’s lazy, you could just call the ‘- setup’ module in the tasks section.

---

<div class="post-metadata">

**Author:** ![Strahinja\_Kustudic2](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@Strahinja\_Kustudic2](https://forum.ansible.com/u/Strahinja_Kustudic2)\
**Post date:** [March 14, 2014, 5:01pm UTC](https://forum.ansible.com/t/force-gathering-facts-on-all-hosts-when-using-tags-or-limit/13446/12 "2014-03-14T17:01:00Z")

</div>

That is a cool solution. Do you have an estimate when will this feature be added?

---

<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:** [March 14, 2014, 5:11pm UTC](https://forum.ansible.com/t/force-gathering-facts-on-all-hosts-when-using-tags-or-limit/13446/13 "2014-03-14T17:11:02Z")

</div>

I’m not actively working on it – I believe Brian Coca had expressed interest in getting this in, also open to someone going ahead and doing it.

–Michael

---

<div class="post-metadata">

**Author:** ![Grzegorz\_Nosek](https://avatars.discourse-cdn.com/v4/letter/g/5daacb/32.png) [@Grzegorz\_Nosek](https://forum.ansible.com/u/Grzegorz_Nosek)\
**Post date:** [March 24, 2014, 5:43pm UTC](https://forum.ansible.com/t/force-gathering-facts-on-all-hosts-when-using-tags-or-limit/13446/14 "2014-03-24T17:43:34Z")

</div>

Hi,

Sorry to dig up this thread again but it’s also an issue for me.

AIUI, Strahinja Kustudić needs (just as I do) a way to always gather facts on all hosts, regardless of tags/limits. What your proposal does is (again, AIUI) improve performance without the need of gather\_facts: false in every play. It’s cool but different. We need different functionality, not better performance.

Consider:

---

<div class="post-metadata">

**Author:** ![Kerry\_Kurian](https://avatars.discourse-cdn.com/v4/letter/k/7993a0/32.png) [@Kerry\_Kurian](https://forum.ansible.com/u/Kerry_Kurian)\
**Post date:** [March 24, 2014, 6:31pm UTC](https://forum.ansible.com/t/force-gathering-facts-on-all-hosts-when-using-tags-or-limit/13446/15 "2014-03-24T18:31:14Z")

</div>

re: always gathering facts on all hosts, regardless of tags/limits.

What I’ve been doing is creating a facts.yml file that looks like this:

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [March 24, 2014, 6:54pm UTC](https://forum.ansible.com/t/force-gathering-facts-on-all-hosts-when-using-tags-or-limit/13446/16 "2014-03-24T18:54:31Z")

</div>

I was thinking something like:

```auto
setup: target={{item}}
with_items: groups['webservers']

```

this gives people more fine grained control, target will populate hostvars[target], vs current host.

or would it be better to use delegate\_to?

---

<div class="post-metadata">

**Author:** ![Grzegorz\_Nosek](https://avatars.discourse-cdn.com/v4/letter/g/5daacb/32.png) [@Grzegorz\_Nosek](https://forum.ansible.com/u/Grzegorz_Nosek)\
**Post date:** [March 24, 2014, 7:00pm UTC](https://forum.ansible.com/t/force-gathering-facts-on-all-hosts-when-using-tags-or-limit/13446/17 "2014-03-24T19:00:34Z")

</div>

W dniu 24.03.2014 19:54, Brian Coca pisze:

> I was thinking something like:
> 
> ```auto
> setup: target={{item}}
> with_items: groups['webservers']
> 
> ```
> 
> this gives people more fine grained control, target will populate  
> hostvars[target], vs current host.
> 
> or would it be better to use delegate\_to?

I'd say delegate\_to (with a group, which isn't allowed ATM, I think?) would be cleaner as it would transparently support other modules (like custom facts) without modifying them. Also, to support this AFAIK you need local action plugins, not just a module to run on the remote host.

So either this:

- setup:  
&nbsp;&nbsp;&nbsp;delegate\_to: webservers

Or this, which probably isn't valid Ansible either:

- setup:  
&nbsp;&nbsp;&nbsp;delegate\_to: "{{ item }}"  
&nbsp;&nbsp;&nbsp;with\_inventory\_hostnames: webservers

Any of these would be fine with me.

Best regards,  
&nbsp;&nbsp;Grzegorz Nosek

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [March 24, 2014, 7:02pm UTC](https://forum.ansible.com/t/force-gathering-facts-on-all-hosts-when-using-tags-or-limit/13446/18 "2014-03-24T19:02:56Z")

</div>

it would require changing current behavior, now if you do setup + delegate\_to, you populate ‘current’ host with delegated host facts.

---

<div class="post-metadata">

**Author:** ![Grzegorz\_Nosek](https://avatars.discourse-cdn.com/v4/letter/g/5daacb/32.png) [@Grzegorz\_Nosek](https://forum.ansible.com/u/Grzegorz_Nosek)\
**Post date:** [March 24, 2014, 7:20pm UTC](https://forum.ansible.com/t/force-gathering-facts-on-all-hosts-when-using-tags-or-limit/13446/19 "2014-03-24T19:20:37Z")

</div>

W dniu 24.03.2014 20:02, Brian Coca pisze:

> it would require changing current behavior, now if you do setup +  
> delegate\_to, you populate 'current' host with delegated host facts.

Right, my bad.

Still, I don't really like the target= as a parameter of the setup module. It complicates the implementation too much IMHO. It effectively duplicates the whole support of delegate\_to with a minor change in functionality (which I forgot and you pointed out).

Another play-level option (delegate\_facts: true?) might be more universal and would keep writing custom fact modules trivial.

Best regards,  
&nbsp;&nbsp;Grzegorz Nosek

---

<div class="post-metadata">

**Author:** ![Strahinja\_Kustudic2](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@Strahinja\_Kustudic2](https://forum.ansible.com/u/Strahinja_Kustudic2)\
**Post date:** [March 24, 2014, 11:26pm UTC](https://forum.ansible.com/t/force-gathering-facts-on-all-hosts-when-using-tags-or-limit/13446/20 "2014-03-24T23:26:28Z")

</div>

I know that I suggested this, but I really don’t see a downside in using **gather\_facts: force**. It is simple, seems the easiest to implement, it is easy to read and feels natural. Only downside I see is that gather\_facts is a Boolean, and if we allowed “force”, it will end being that and might feel a little strange.

On the other hand adding additional options to **setup** module like **ignore\_tags** and **ignore\_limit** is cool, but what I don’t like about it is that you would need to do **gather\_facts: False** before that. The same goes for using **setup** with **delegate\_to**.

Don’t get me wrong those additional parameters for the setup module are great ideas and I think Ansible should have them as well, so that you can gather custom facts, but Ansible is always being promoted as being extremely simple and **gather\_facts: force/always** is as simple as it can be.

[Next page](https://forum.ansible.com/t/force-gathering-facts-on-all-hosts-when-using-tags-or-limit/13446.md?page=2)
