# Using "short lived facts" and caching, Eg. ansible\_uptime\_seconds

**URL:** <https://forum.ansible.com/t/using-short-lived-facts-and-caching-eg-ansible-uptime-seconds/44324>\
**Category:** Get Help\
**Tags:** facts, ansible-core\
**Created:** [August 19, 2025, 5:26am UTC](https://forum.ansible.com/t/using-short-lived-facts-and-caching-eg-ansible-uptime-seconds/44324 "2025-08-19T05:26:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Antill](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/james_antill/32/9295_2.png) [@James\_Antill](https://forum.ansible.com/u/James_Antill)\
**Post date:** [August 19, 2025, 5:26am UTC](https://forum.ansible.com/t/using-short-lived-facts-and-caching-eg-ansible-uptime-seconds/44324/1 "2025-08-19T05:26:04Z")

</div>

I’m currently using `ansible_uptime_seconds` however that gives an old cached value a _lot_ more than I’d expect it to (Eg. reboot the server, and then run some task on the server to register a variable but `ansible_uptime_seconds` is still from before the reboot).

I assume this is a known feature of facts, due to caching. And that’s probably fine for most facts, as most of my facts don’t change OS/ip/etc

But there are a few times where I’d really like these “can change more often” facts to be updated, and AFAICS the only workaround is to run the playbooks with `--flush-cache`?

Is there a better way to say “make sure this fact is accurate”?

Do people solve the problem by not using facts for these kinds of problems (Eg. parse /proc/uptime or `uptime` cmd output directly, and store that in a variable)?

Version: ansible core 2.14.18

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [August 19, 2025, 5:31am UTC](https://forum.ansible.com/t/using-short-lived-facts-and-caching-eg-ansible-uptime-seconds/44324/2 "2025-08-19T05:31:37Z")

</div>

Note that you can explicitly run the [ansible.bulitin.setup module](https://docs.ansible.com/ansible/devel/collections/ansible/builtin/setup_module.html) restricted to certain facts to update these. `ansible_uptime_seconds` seems to be part of `hardware`, so specifying `gather_subset=[!all, !min, hardware]` and `filter=[uptime_seconds]` updates only that value.

You can see this with: `ansible localhost -m setup -a 'gather_subset=!all,!min,hardware filter=uptime_seconds'` - it will only give you the uptime.

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [August 19, 2025, 1:57pm UTC](https://forum.ansible.com/t/using-short-lived-facts-and-caching-eg-ansible-uptime-seconds/44324/3 "2025-08-19T13:57:58Z")

</div>

You can also ‘clear facts’, but this is not fine grained.

> **[ansible.builtin.meta module – Execute Ansible ‘actions’ — Ansible Community...](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/meta_module.html#parameter-free_form)**

---

<div class="post-metadata">

**Author:** ![James\_Antill](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/james_antill/32/9295_2.png) [@James\_Antill](https://forum.ansible.com/u/James_Antill)\
**Post date:** [August 19, 2025, 10:43pm UTC](https://forum.ansible.com/t/using-short-lived-facts-and-caching-eg-ansible-uptime-seconds/44324/4 "2025-08-19T22:43:59Z")

</div>

> [@felixfontein](#):
>
> Note that you can explicitly run the [ansible.bulitin.setup module](https://docs.ansible.com/ansible/devel/collections/ansible/builtin/setup_module.html) restricted to certain facts to update these.

As far as I can see this still uses the cache, like having `gather_facts: true` which I was using (but not for everything). Also not sure of the exact syntax, but the obvious one blows up when I run ansible-playbook on it ;).

---

<div class="post-metadata">

**Author:** ![flowerysong](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/flowerysong/32/2305_2.png) [@flowerysong](https://forum.ansible.com/u/flowerysong)\
**Post date:** [August 19, 2025, 11:33pm UTC](https://forum.ansible.com/t/using-short-lived-facts-and-caching-eg-ansible-uptime-seconds/44324/5 "2025-08-19T23:33:14Z")

</div>

> [@James\_Antill](#):
>
> As far as I can see this still uses the cache, like having `gather_facts: true`

It does not. Cached facts only prevent automatic running of `gather_facts` when `DEFAULT_GATHERING=smart`, they have no effect on explicit fact gathering in a task. See [I don't understand gather\_subset - #12 by flowerysong](https://forum.ansible.com/t/i-dont-understand-gather-subset/6660/12)

---

<div class="post-metadata">

**Author:** ![James\_Antill](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/james_antill/32/9295_2.png) [@James\_Antill](https://forum.ansible.com/u/James_Antill)\
**Post date:** [August 20, 2025, 2:39am UTC](https://forum.ansible.com/t/using-short-lived-facts-and-caching-eg-ansible-uptime-seconds/44324/6 "2025-08-20T02:39:17Z")

</div>

Thanks, that post you linked to should also solve the syntax problems I was having.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [September 19, 2025, 2:39am UTC](https://forum.ansible.com/t/using-short-lived-facts-and-caching-eg-ansible-uptime-seconds/44324/7 "2025-09-19T02:39:30Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
