Blame SOURCES/0001-Fix-final-fact-delegation-77008-77017.patch

eefa2c
From ae35fc04c3a2068b1d37efe813d1c6938b4f2634 Mon Sep 17 00:00:00 2001
eefa2c
From: Brian Coca <bcoca@users.noreply.github.com>
eefa2c
Date: Wed, 16 Feb 2022 11:55:03 -0500
eefa2c
Subject: [PATCH] Fix final fact delegation (#77008) (#77017)
eefa2c
eefa2c
* fix facts delegation loop overwrite
eefa2c
eefa2c
 partial revert of change to allow facts to be present in each loop iteration
eefa2c
 was not needed in final results as result processing alreayd had the disctiontion
eefa2c
 and ended up breaking the assumptions in the calling code.
eefa2c
eefa2c
 fixes #76676
eefa2c
eefa2c
(cherry picked from commit c9d3518d2f3812787e1627806b5fa93f8fae48a6)
eefa2c
---
eefa2c
 .../fragments/fix_fax_delegation_loops.yml    |  2 ++
eefa2c
 lib/ansible/executor/task_executor.py         |  8 +------
eefa2c
 .../delegate_to/delegate_facts_loop.yml       | 21 ++++++++++++++++++-
eefa2c
 3 files changed, 23 insertions(+), 8 deletions(-)
eefa2c
 create mode 100644 changelogs/fragments/fix_fax_delegation_loops.yml
eefa2c
eefa2c
diff --git a/changelogs/fragments/fix_fax_delegation_loops.yml b/changelogs/fragments/fix_fax_delegation_loops.yml
eefa2c
new file mode 100644
eefa2c
index 0000000000..d9e07bf110
eefa2c
--- /dev/null
eefa2c
+++ b/changelogs/fragments/fix_fax_delegation_loops.yml
eefa2c
@@ -0,0 +1,2 @@
eefa2c
+bugfixes:
eefa2c
+  - task_executor reverts the change to push facts into delegated vars on loop finalization as result managing code already handles this and was duplicating effort to wrong result.
eefa2c
diff --git a/lib/ansible/executor/task_executor.py b/lib/ansible/executor/task_executor.py
eefa2c
index 1a7c666fea..05dcb96446 100644
eefa2c
--- a/lib/ansible/executor/task_executor.py
eefa2c
+++ b/lib/ansible/executor/task_executor.py
eefa2c
@@ -745,13 +745,7 @@ class TaskExecutor:
eefa2c
 
eefa2c
         if 'ansible_facts' in result and self._task.action not in C._ACTION_DEBUG:
eefa2c
             if self._task.action in C._ACTION_WITH_CLEAN_FACTS:
eefa2c
-                if self._task.delegate_to and self._task.delegate_facts:
eefa2c
-                    if '_ansible_delegated_vars' in variables:
eefa2c
-                        variables['_ansible_delegated_vars'].update(result['ansible_facts'])
eefa2c
-                    else:
eefa2c
-                        variables['_ansible_delegated_vars'] = result['ansible_facts']
eefa2c
-                else:
eefa2c
-                    variables.update(result['ansible_facts'])
eefa2c
+                variables.update(result['ansible_facts'])
eefa2c
             else:
eefa2c
                 # TODO: cleaning of facts should eventually become part of taskresults instead of vars
eefa2c
                 af = wrap_var(result['ansible_facts'])
eefa2c
diff --git a/test/integration/targets/delegate_to/delegate_facts_loop.yml b/test/integration/targets/delegate_to/delegate_facts_loop.yml
eefa2c
index 90a25676dd..28a1488de3 100644
eefa2c
--- a/test/integration/targets/delegate_to/delegate_facts_loop.yml
eefa2c
+++ b/test/integration/targets/delegate_to/delegate_facts_loop.yml
eefa2c
@@ -5,7 +5,6 @@
eefa2c
         test: 123
eefa2c
       delegate_to: "{{ item }}"
eefa2c
       delegate_facts: true
eefa2c
-      when: test is not defined
eefa2c
       loop: "{{ groups['all'] | difference(['localhost']) }}"
eefa2c
 
eefa2c
     - name: ensure we didnt create it on current host
eefa2c
@@ -19,3 +18,23 @@
eefa2c
           - "'test' in hostvars[item]"
eefa2c
           - hostvars[item]['test'] == 123
eefa2c
       loop: "{{ groups['all'] | difference(['localhost']) }}"
eefa2c
+
eefa2c
+
eefa2c
+- name: test that we don't polute whole group with one value
eefa2c
+  hosts: localhost
eefa2c
+  gather_facts: no
eefa2c
+  vars:
eefa2c
+    cluster_name: bleh
eefa2c
+  tasks:
eefa2c
+  - name: construct different fact per host in loop
eefa2c
+    set_fact:
eefa2c
+      vm_name: "{{ cluster_name }}-{{item}}"
eefa2c
+    delegate_to: "{{ item }}"
eefa2c
+    delegate_facts: True
eefa2c
+    with_items: "{{ groups['all'] }}"
eefa2c
+
eefa2c
+  - name: ensure the fact is personalized for each host
eefa2c
+    assert:
eefa2c
+      that:
eefa2c
+        - hostvars[item]['vm_name'].endswith(item)
eefa2c
+    loop: "{{ groups['all'] }}"
eefa2c
-- 
eefa2c
2.30.2
eefa2c