From 6237dab4cf3e2442403646825b8f6f9e72e40b7b Mon Sep 17 00:00:00 2001
From: Brian Coca <brian.coca+git@gmail.com>
Date: Tue, 6 Jan 2015 10:06:50 -0500
Subject: [PATCH] fix for when state=directory, follow=yes and target is
 symlink to directory

---
 files/file.py | 35 +++++++++++++++++++++++------------
 1 file changed, 23 insertions(+), 12 deletions(-)

diff --git a/files/file.py b/files/file.py
index e154d6ad07f..46185f29215 100644
--- a/files/file.py
+++ b/files/file.py
@@ -103,6 +103,23 @@ EXAMPLES = '''
 
 '''
 
+
+def get_state(path):
+    ''' Find out current state '''
+
+    if os.path.lexists(path):
+        if os.path.islink(path):
+            return 'link'
+        elif os.path.isdir(path):
+            return 'directory'
+        elif os.stat(path).st_nlink > 1:
+            return 'hard'
+        else:
+            # could be many other things, but defaulting to file
+            return 'file'
+
+    return 'absent'
+
 def main():
 
     module = AnsibleModule(
@@ -143,18 +160,7 @@ def main():
             pass
         module.exit_json(path=path, changed=False, appears_binary=appears_binary)
 
-    # Find out current state
-    prev_state = 'absent'
-    if os.path.lexists(path):
-        if os.path.islink(path):
-            prev_state = 'link'
-        elif os.path.isdir(path):
-            prev_state = 'directory'
-        elif os.stat(path).st_nlink > 1:
-            prev_state = 'hard'
-        else:
-            # could be many other things, but defaulting to file
-            prev_state = 'file'
+    prev_state = get_state(path)
 
     # state should default to file, but since that creates many conflicts,
     # default to 'current' when it exists.
@@ -220,6 +226,11 @@ def main():
         module.exit_json(path=path, changed=changed)
 
     elif state == 'directory':
+
+        if follow and prev_state == 'link':
+            path = os.readlink(path)
+            prev_state = get_state(path)
+
         if prev_state == 'absent':
             if module.check_mode:
                 module.exit_json(changed=True)