Repository: ansible/ansible

Issue:
Module system/user.py doesn't change file permission of directories 
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report

##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
ansible/lib/ansible/modules/system/user.py

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
ansible 2.0.0.2
ansible 2.4.0
```

##### CONFIGURATION
<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->

##### OS / ENVIRONMENT
<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say “N/A” for anything that is not platform-specific.
-->
Ansible runs on SLES 11.3 and shall configure a SLES 12.2 system

##### SUMMARY
<!--- Explain the problem briefly -->
The user module does copy the content of the /etc/skel directory but does not pass the ownership of the contained directories to the user.

##### STEPS TO REPRODUCE
<!---
For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used.
-->
First create a directory as root in /etc/skel
`
mkdir -p /etc/skel/notmine
`
Create a play
<!--- Paste example playbooks or commands between quotes below -->
```yaml
---
- name: Create home dir for user
  user:
    name: "itsme"
    group: "users"
    home: "/home/itsme"
    shell: "/bin/bash"
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
```
ls -ld /home/itsme
drwxr-xr-x 3 itsme users 4096 May 23 15:39 .
drwxr-xr-x 4 root  root  4096 May 23 15:39 ..
-rw-r--r-- 1 itsme users  220 Sep  1  2015 .bash_logout
-rw-r--r-- 1 itsme users 3771 Sep  1  2015 .bashrc
drwxr-xr-x 2 itsme users  4096 May 23 15:39 notmine <<<<<<<<<
-rw-r--r-- 1 itsme users  655 Jun 24  2016 .profile
```

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
```
drwxr-xr-x 3 itsme users 4096 May 23 15:39 .
drwxr-xr-x 4 root  root  4096 May 23 15:39 ..
-rw-r--r-- 1 itsme users  220 Sep  1  2015 .bash_logout
-rw-r--r-- 1 itsme users 3771 Sep  1  2015 .bashrc
drwxr-xr-x 2 root  root  4096 May 23 15:39 notmine  <<<<<<<<<
-rw-r--r-- 1 itsme users  655 Jun 24  2016 .profile
```

<!--- Paste verbatim command output between quotes below -->
```

```



Discussion:

Resolving patch:
diff --git a/lib/ansible/modules/system/user.py b/lib/ansible/modules/system/user.py
--- a/lib/ansible/modules/system/user.py
+++ b/lib/ansible/modules/system/user.py
@@ -719,7 +719,7 @@ def chown_homedir(self, uid, gid, path):
             os.chown(path, uid, gid)
             for root, dirs, files in os.walk(path):
                 for d in dirs:
-                    os.chown(path, uid, gid)
+                    os.chown(os.path.join(root, d), uid, gid)
                 for f in files:
                     os.chown(os.path.join(root, f), uid, gid)
         except OSError:


Test patch: