From 8743e6ae2ee7aa3ae94038554110a9255f5d8fd8 Mon Sep 17 00:00:00 2001
From: Brian Coca <bcoca@users.noreply.github.com>
Date: Wed, 10 Oct 2018 15:10:23 -0400
Subject: [PATCH] remove default from delegate_facts to inherit (#45492)

* remove default from delegate_facts to inherit

fixes #45456

* test delegate_facts

* added note about inheritance and defaults

* yamllint
---
 lib/ansible/playbook/block.py                 |  2 +-
 lib/ansible/playbook/role/__init__.py         |  2 +-
 lib/ansible/playbook/task.py                  |  6 ++++-
 .../delegate_to/delegate_facts_block.yml      | 25 +++++++++++++++++++
 test/integration/targets/delegate_to/runme.sh |  2 ++
 5 files changed, 34 insertions(+), 3 deletions(-)
 create mode 100644 test/integration/targets/delegate_to/delegate_facts_block.yml

diff --git a/lib/ansible/playbook/block.py b/lib/ansible/playbook/block.py
index e51e2218792..72f091dfede 100644
--- a/lib/ansible/playbook/block.py
+++ b/lib/ansible/playbook/block.py
@@ -38,7 +38,7 @@ class Block(Base, Become, Conditional, Taggable):
 
     # other fields
     _delegate_to = FieldAttribute(isa='string')
-    _delegate_facts = FieldAttribute(isa='bool', default=False)
+    _delegate_facts = FieldAttribute(isa='bool')
 
     # for future consideration? this would be functionally
     # similar to the 'else' clause for exceptions
diff --git a/lib/ansible/playbook/role/__init__.py b/lib/ansible/playbook/role/__init__.py
index 0d0f1bdc864..8475fecbfcf 100644
--- a/lib/ansible/playbook/role/__init__.py
+++ b/lib/ansible/playbook/role/__init__.py
@@ -94,7 +94,7 @@ def hash_params(params):
 class Role(Base, Become, Conditional, Taggable):
 
     _delegate_to = FieldAttribute(isa='string')
-    _delegate_facts = FieldAttribute(isa='bool', default=False)
+    _delegate_facts = FieldAttribute(isa='bool')
 
     def __init__(self, play=None, from_files=None, from_include=False):
         self._role_name = None
diff --git a/lib/ansible/playbook/task.py b/lib/ansible/playbook/task.py
index e314375d189..9ae95f82b57 100644
--- a/lib/ansible/playbook/task.py
+++ b/lib/ansible/playbook/task.py
@@ -66,6 +66,10 @@ class Task(Base, Conditional, Taggable, Become):
     # will be used if defined
     # might be possible to define others
 
+    # NOTE: ONLY set defaults on task attributes that are not inheritable,
+    # inheritance is only triggered if the 'current value' is None,
+    # default can be set at play/top level object and inheritance will take it's course.
+
     _args = FieldAttribute(isa='dict', default=dict())
     _action = FieldAttribute(isa='string')
 
@@ -73,7 +77,7 @@ class Task(Base, Conditional, Taggable, Become):
     _changed_when = FieldAttribute(isa='list', default=[])
     _delay = FieldAttribute(isa='int', default=5)
     _delegate_to = FieldAttribute(isa='string')
-    _delegate_facts = FieldAttribute(isa='bool', default=False)
+    _delegate_facts = FieldAttribute(isa='bool')
     _failed_when = FieldAttribute(isa='list', default=[])
     _loop = FieldAttribute()
     _loop_control = FieldAttribute(isa='class', class_type=LoopControl, inherit=False)
diff --git a/test/integration/targets/delegate_to/delegate_facts_block.yml b/test/integration/targets/delegate_to/delegate_facts_block.yml
new file mode 100644
index 00000000000..2edfeb42578
--- /dev/null
+++ b/test/integration/targets/delegate_to/delegate_facts_block.yml
@@ -0,0 +1,25 @@
+- hosts: testhost
+  gather_facts: false
+  tasks:
+    - name: set var to delegated host directly
+      set_fact: qq1=333
+      delegate_facts: true
+      delegate_to: localhost
+
+    - name: ensure qq1 exists in localhost but not in testhost
+      assert:
+        that:
+            - qq1 is undefined
+            - "'qq1' in hostvars['localhost']"
+
+    - name: set var to delegated host via inheritance
+      block:
+        - set_fact: qq2=333
+      delegate_facts: true
+      delegate_to: localhost
+
+    - name: ensure qq2 exists in localhost but not in testhost
+      assert:
+        that:
+            - qq2 is undefined
+            - "'qq2' in hostvars['localhost']"
diff --git a/test/integration/targets/delegate_to/runme.sh b/test/integration/targets/delegate_to/runme.sh
index 8d669df1980..d9978837b20 100755
--- a/test/integration/targets/delegate_to/runme.sh
+++ b/test/integration/targets/delegate_to/runme.sh
@@ -10,3 +10,5 @@ ansible-playbook test_loop_control.yml -v "$@"
 ansible-playbook test_delegate_to_loop_randomness.yml -v "$@"
 
 ansible-playbook delegate_and_nolog.yml -v "$@"
+
+ansible-playbook delegate_facts_block.yml -v "$@"