From 28e2eae902d3cd623e5739a4edd979de3d6e0c2b Mon Sep 17 00:00:00 2001
From: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Fri, 17 Jul 2015 12:56:27 +0530
Subject: [PATCH] Make gathering=explicit work again

There was a confusion between the valid values for defaults.gathering
(explicit/implicit/smart) and a play's gather_facts setting (boolean),
which resulted in gathering=explicit being ignored.
---
 lib/ansible/executor/play_iterator.py | 14 +++++++++++++-
 lib/ansible/playbook/play.py          |  2 +-
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/lib/ansible/executor/play_iterator.py b/lib/ansible/executor/play_iterator.py
index 2ca3815e419..8deeac8b4dd 100644
--- a/lib/ansible/executor/play_iterator.py
+++ b/lib/ansible/executor/play_iterator.py
@@ -19,6 +19,8 @@
 from __future__ import (absolute_import, division, print_function)
 __metaclass__ = type
 
+from ansible import constants as C
+
 from ansible.errors import *
 from ansible.playbook.block import Block
 from ansible.playbook.task import Task
@@ -130,7 +132,17 @@ class PlayIterator:
         elif s.run_state == self.ITERATING_SETUP:
             s.run_state = self.ITERATING_TASKS
             s.pending_setup = True
-            if self._play.gather_facts == 'smart' and not host._gathered_facts or boolean(self._play.gather_facts):
+
+            # Gather facts if the default is 'smart' and we have not yet
+            # done it for this host; or if 'explicit' and the play sets
+            # gather_facts to True; or if 'implicit' and the play does
+            # NOT explicitly set gather_facts to False.
+
+            gathering = C.DEFAULT_GATHERING
+            if ((gathering == 'smart' and not host._gathered_facts) or
+                (gathering == 'explicit' and boolean(self._play.gather_facts)) or
+                (gathering == 'implicit' and
+                    (self._play.gather_facts is None or boolean(self._play.gather_facts)))):
                 if not peek:
                     # mark the host as having gathered facts
                     host.set_gathered_facts(True)
diff --git a/lib/ansible/playbook/play.py b/lib/ansible/playbook/play.py
index 2d31adec64c..ecaeac23622 100644
--- a/lib/ansible/playbook/play.py
+++ b/lib/ansible/playbook/play.py
@@ -58,7 +58,7 @@ class Play(Base, Taggable, Become):
     _accelerate_port     = FieldAttribute(isa='int', default=5099) # should be alias of port
 
     # Connection
-    _gather_facts        = FieldAttribute(isa='string', default='smart')
+    _gather_facts        = FieldAttribute(isa='bool', default=None)
     _hosts               = FieldAttribute(isa='list', default=[], required=True, listof=string_types)
     _name                = FieldAttribute(isa='string', default='')