fact disappears after regather

i have a local fact defined. when i first run ansible and it runs
through the gather facts stage, the fact shows up just fine. i do
some tasks and then regather the facts with setup

- name: regather facts
  ansible.builtin.setup:

i can see the gather task run, but when i look for that same variable
after the regather of the facts it's missing. the fact is
ansible_local.fstab and there's a dictionary under it, but the entire
ansible_local tree disappears

i tried adding gather_subset: all but that didn't see to make a
difference. is there something else i need to do to rerun a fact and
refresh the data?

Fact gathering overwrites all facts for a host, it does not update.
ansible_local is actually a subset of the facts gathered, it is
updated to what ansible_facts['local'] returns.

ok, my terminology was a little off, i'm okay with it overwriting then
entire fact tree. but i'm still not sure why the entire branch of
ansible_local disappears after the second run of gathering. is there
something different happening when the play runs "gather_facts"
version ansible.builtin.setup? (i wouldn't have thought so)

No, but it overwrites it even if the results from the current run are
empty. Fact gathering is supposed to reflect 'current state' so if for
example you remove a network interface, but had gathered facts about
it before hand, when you refresh facts, it will disappear from those
also.

okay, that’s understood. but i’m not sure my behavior is reflecting what you’re saying. either because ansible is doing something unexpected, or more likely i’m doing something wrong. what i see is

using this playbook

  • name: dump vars
    hosts: all
    connection: local
    gather_facts: true
    tasks:
  • name: disp var
    debug:
    var: ansible_local.fstab
  • name: regather
    ansible.builtin.setup:
  • name: redisp var
    debug:
    var: ansible_local.fstab

ansible start
gather facts

the debug will output the variable (which is just a dict of /etc/fstab)
regather the facts through setup
the second debug then outputs
ansible_local.fstab: ‘VARIABLE IS NOT DEFINED’

I cannot reproduce, either both have it defined or undefined
(depending if i add /etc/ansible/facts.d/fstab.fact or not).

it looks like it’s related to my fact pathing in some manner. in ansible.cfg we defined fact_path=/ansible/facts and there are no facts in /etc/ansible/facts.d. if i copy the fstab.fact file from /ansible/facts to /etc/ansible/facts.d then i do in get the variable before and after the setup module runs in the play

so does ansible reset like literally everything when you run the setup module? does it not re pull in the ansible.cfg where i have paths defined?

No, the cfg setting affects 'fact gathering' but not specific module
executions, you executing the 'setup' module or any other that returns
facts is not exactly the same thing.

Use module_defaults or just set the local facts dir when you invoke setup.