From cb6fa4b6bc77f8694bc0cddbc907ded9bc12fb84 Mon Sep 17 00:00:00 2001 From: Alejandro Roman Date: Sun, 22 Nov 2026 15:44:09 +0400 Subject: [PATCH] fix(applications_carrier): overwrite a carrier left in the fact cache on clear The fact cache merges the cacheable fact layer, so the empty mapping that clear registered there left an older fact-parked carrier in ansible_facts, where get_vars kept deep-copying it before every task. clear now writes None for every carrier key it finds in ansible_facts, which is the recovery the reader's CarrierInvisibleError message points to. The integration test parks the carrier as a fact, clears it and parks it again, with fact injection on and off. Co-Authored-By: Claude Opus 5.5 --- plugins/action/applications_carrier.py | 28 +++++++++------ .../test_applications_carrier.py | 34 ++++++++++++++++++- .../action/test_applications_carrier.py | 23 +++++++++++++ 3 files changed, 74 insertions(+), 11 deletions(-) diff --git a/plugins/action/applications_carrier.py b/plugins/action/applications_carrier.py index 5c95648413..6fd4e888f9 100644 --- a/plugins/action/applications_carrier.py +++ b/plugins/action/applications_carrier.py @@ -99,13 +99,13 @@ class ActionModule(ActionBase): ) if boolean(args.get("clear", False)): - self._park( - { - APPLICATIONS_RENDERED_FACT: None, - USERS_RENDERED_FACT: None, - PROVIDER_VIEW_RENDERED_FACT: None, - } - ) + cleared = { + APPLICATIONS_RENDERED_FACT: None, + USERS_RENDERED_FACT: None, + PROVIDER_VIEW_RENDERED_FACT: None, + } + facts = task_vars.get("ansible_facts") or {} + self._park(cleared, {fact: None for fact in cleared if fact in facts}) result.update(changed=False) return result @@ -136,10 +136,18 @@ class ActionModule(ActionBase): if view_carrier is not None: carriers[PROVIDER_VIEW_RENDERED_FACT] = view_carrier - self._park(carriers) + self._park(carriers, {}) result.update(changed=False, _ansible_no_log=True) return result - def _park(self, carriers: dict[str, Any]) -> None: - self.register_host_variables({}, VariableLayer.CACHEABLE_FACT) + def _park(self, carriers: dict[str, Any], cacheable: dict[str, Any]) -> None: + """Register the carriers outside the fact cache. + + Args: + carriers: carrier variables for the ephemeral fact layer. + cacheable: entries for the cacheable fact layer. The fact cache + merges this layer, so an empty mapping leaves a stale carrier + there; only ``None`` overwrites its payload. + """ + self.register_host_variables(cacheable, VariableLayer.CACHEABLE_FACT) self.register_host_variables(carriers, VariableLayer.EPHEMERAL_FACT) diff --git a/tests/integration/ansible/action_plugins/test_applications_carrier.py b/tests/integration/ansible/action_plugins/test_applications_carrier.py index 8cca57fc74..f1c1aa2abf 100644 --- a/tests/integration/ansible/action_plugins/test_applications_carrier.py +++ b/tests/integration/ansible/action_plugins/test_applications_carrier.py @@ -70,7 +70,7 @@ class LookupModule(LookupBase): ) is not None, "fact_cache": sorted( name for name in ({APPLICATIONS_RENDERED_FACT!r}, {USERS_RENDERED_FACT!r}) - if name in facts + if facts.get(name) is not None ), }}] """ @@ -137,6 +137,29 @@ HAZARD_PLAY = """--- """ +RECOVERY_PLAY = """--- +- name: Clear a fact-parked carrier and park it again + hosts: localhost + gather_facts: false + tasks: + - name: Park the carrier in the fact cache + fact_carrier: {} + + - name: Clear it, as the reader's recovery hint says + applications_carrier: + clear: true + + - name: Park the carrier again + applications_carrier: {} + + - name: Read the parked carrier + ansible.builtin.copy: + dest: "{{ probe_out }}" + content: "{{ lookup('carrier_probe') | to_json }}" + mode: "0644" +""" + + CROSS_HOST_PLAY = f"""--- - name: Park the carrier on two hosts and read the other host's through hostvars hosts: all @@ -265,6 +288,15 @@ class TestApplicationsCarrierReachesLaterWorkers(unittest.TestCase): self.assertIsNone(report) self.assertIn("not visible as a variable", output) + def test_clear_removes_a_fact_parked_carrier_from_the_fact_cache(self): + for inject in (True, False): + with self.subTest(inject=inject): + code, output, report = self._play(RECOVERY_PLAY, inject, "localhost,") + self.assertEqual(code, 0, output) + self.assertEqual( + report, {"applications": True, "users": True, "fact_cache": []} + ) + if __name__ == "__main__": unittest.main() diff --git a/tests/unit/python/plugins/action/test_applications_carrier.py b/tests/unit/python/plugins/action/test_applications_carrier.py index cfd2560f3f..690d9cb678 100644 --- a/tests/unit/python/plugins/action/test_applications_carrier.py +++ b/tests/unit/python/plugins/action/test_applications_carrier.py @@ -277,6 +277,29 @@ class TestApplicationsCarrierActionPlugin(unittest.TestCase): self.assertEqual(applications.calls, []) self.assertEqual(users.calls, []) + def test_clear_overwrites_a_carrier_left_in_the_fact_cache(self): + action = _make_action(_FakeTask(args={"clear": True})) + stale = {"key": ["roles", ["a"]], "applications": {"x": {}}} + with patch( + "plugins.action.applications_carrier.ActionBase.run", + autospec=True, + return_value={}, + ): + action.run( + task_vars={ + "ansible_facts": { + APPLICATIONS_RENDERED_FACT: stale, + "os_family": "Debian", + } + } + ) + + self.assertEqual( + action.registered[VariableLayer.CACHEABLE_FACT], + {APPLICATIONS_RENDERED_FACT: None}, + ) + self.assertIsNone(_parked(action)[APPLICATIONS_RENDERED_FACT]) + def test_clear_false_still_renders(self): applications, users, loader = _lookups() action = _make_action(_FakeTask(args={"clear": "no"}))