# Load facts just-in-time

**URL:** <https://forum.ansible.com/t/load-facts-just-in-time/4311>\
**Category:** Get Help\
**Tags:** playbook\
**Created:** [March 11, 2024, 10:33am UTC](https://forum.ansible.com/t/load-facts-just-in-time/4311 "2024-03-11T10:33:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wedwards](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/wedwards/32/2545_2.png) [@wedwards](https://forum.ansible.com/u/wedwards)\
**Post date:** [March 11, 2024, 10:33am UTC](https://forum.ansible.com/t/load-facts-just-in-time/4311/1 "2024-03-11T10:33:21Z")

</div>

Hi,

Is there a pattern to collect facts just-in-time (JIT)?

Reason:

I use dynamic facts using `facts.d` ([Discovering variables: facts and magic variables — Ansible Documentation](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_vars_facts.html#facts-d-or-local-facts)).

All facts are collected when any Ansible playbook runs on the controller.

Collecting these facts is slow (inherent to my systems, not able to speed that up more). While I only need them for _some_ playbooks.

Playbooks that do not use these facts would be sped up enormously, if Ansible loads the fact only when it’s accessed by its `ansible_local.$fact` notation.

Of course, this would require Ansible to know _which_ local facts exist, and which `facts.d` script it needs to run to collect a specific one. A pattern which I haven’t been able to find.

---

<div class="post-metadata">

**Author:** ![brainmue](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/brainmue/32/2403_2.png) [@brainmue](https://forum.ansible.com/u/brainmue)\
**Post date:** [March 13, 2024, 11:48am UTC](https://forum.ansible.com/t/load-facts-just-in-time/4311/2 "2024-03-13T11:48:35Z")

</div>

I’m not quite sure if I’ve understood your question correctly, but you don’t necessarily have to do “gathering facts” at the beginning of your playbook.

You can use `gather_facts: no` to not create any at first and work through something and if you get to a point where you need it, then you just do it later.

```auto
- name: Gather facts
  setup:

```

I hope this can help?

---

<div class="post-metadata">

**Author:** ![wedwards](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/wedwards/32/2545_2.png) [@wedwards](https://forum.ansible.com/u/wedwards)\
**Post date:** [March 13, 2024, 12:28pm UTC](https://forum.ansible.com/t/load-facts-just-in-time/4311/3 "2024-03-13T12:28:09Z")

</div>

I do want to gather facts, just not specific ones that are expensive to get and are not always needed.

`gather_facts` does not allow me to exclude facts / facts files selectively.

---

<div class="post-metadata">

**Author:** ![brainmue](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/brainmue/32/2403_2.png) [@brainmue](https://forum.ansible.com/u/brainmue)\
**Post date:** [March 13, 2024, 1:03pm UTC](https://forum.ansible.com/t/load-facts-just-in-time/4311/4 "2024-03-13T13:03:19Z")

</div>

You’re right, but I don’t think there’s a solution for that.  
With automation, the throughput is not so relevant for me.

But what I can think of for a basic acceleration would be that you build up your dynamic inventory cyclically with `ansible-inventory` and store it as a static json file.  
You can then use this in the playbook executions. This eliminates the longer waiting time at the start of execution. But the disadvantage is, of course, that the data is never 100% up-to-date. But it could perhaps be a possibility.

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [March 21, 2024, 12:10pm UTC](https://forum.ansible.com/t/load-facts-just-in-time/4311/5 "2024-03-21T12:10:01Z")

</div>

`setup` and `gather_facts` support filtering and gathering specific subsets of facts.

```yaml
    - name: Gather only local facts
      gather_facts:
        gather_subset: "!all,!min,local"
        filter: "ansible_local" # <- redundant since only local facts are gathered

```

Valid subsets are documented here [ansible.builtin.setup module – Gathers facts about remote hosts — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/setup_module.html#parameter-gather_subset).
