Repository: ansible/ansible

Issue:
docker_container homedir ('/~') issue running localhost
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
docker_container

##### ANSIBLE VERSION
```
ansible --version
ansible 2.2.0.0
```

##### CONFIGURATION
Default

##### OS / ENVIRONMENT
OS X El Capitan Version 10.11.6

##### SUMMARY

Created a local playbook for building docker hosts for testing.

Moved config files for container into $USER '~/' dir

docker_container uses an incorrect path for '~/' when mounting volumes.

##### STEPS TO REPRODUCE

These are all done with 

```
- hosts: all
  connection: local
```

Prometheus Container

```
    - name: prometheus container
      docker_container:
        name: prometheus
        image: prom/prometheus
        command: -config.file=/etc/prometheus/prometheus.yml -alertmanager.url=http://127.0.0.1:9093/ -storage.local.path=/var/lib/prometheus/data -log.level=debug
        published_ports:
          - 9090:9090
        volumes:
        - "~/prometheus-dev/lib/prometheus/:/var/lib/prometheus/"
        - "~/prometheus-dev/etc/prometheus/:/etc/prometheus/"
```
From Debug

```
            "volumes": [
                "~/prometheus-dev/lib/prometheus/:/var/lib/prometheus/",
                "~/prometheus-dev/etc/prometheus/:/etc/prometheus/"
            ],
```

Prometheus cannot find any of the files in ~/



To get around this ran pwd command in ~/ and used output

```
    - name: check 
      shell: pwd
      args:
        chdir: ~/
      register: homedir

    - name: prometheus container
      docker_container:
        name: prometheus
        image: prom/prometheus
        command: -config.file=/etc/prometheus/prometheus.yml -alertmanager.url=http://127.0.0.1:9093/ -storage.local.path=/var/lib/prometheus/data -log.level=debug
        published_ports:
          - 9090:9090
        volumes:
        - "{{ homedir.stdout }}/prometheus-dev/lib/prometheus/:/var/lib/prometheus/"
        - "{{ homedir.stdout }}/prometheus-dev/etc/prometheus/:/etc/prometheus/"
      tags: docker, prometheus

```
From debug:
```
            "volumes": [
                "/Users/smuggla/prometheus-dev/lib/prometheus/:/var/lib/prometheus/",
                "/Users/smuggla/prometheus-dev/etc/prometheus/:/etc/prometheus/"
            ],
```

##### EXPECTED RESULTS
Access to ~/ of non-sudo user

##### ACTUAL RESULTS
~/ used is not that of user running playbook



Discussion:
Update: was looking at commit history on this and found that it placed it in the directory that the playbook was located.

so if I ran `~/taco/update.yml` it created a dir in `~/taco/~/`
cc @ThomasSteinbach @cove @dusdanig @jctanner @joshuaconner @kassiansun @softzilla @zfil
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
@Smuggla ansible on target machines runs under `$HOME` dir, so `./` is good enough. 
cc @chouseknecht
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/lib/ansible/modules/cloud/docker/docker_container.py b/lib/ansible/modules/cloud/docker/docker_container.py
--- a/lib/ansible/modules/cloud/docker/docker_container.py
+++ b/lib/ansible/modules/cloud/docker/docker_container.py
@@ -829,14 +829,14 @@ def _expand_host_paths(self):
             if ':' in vol:
                 if len(vol.split(':')) == 3:
                     host, container, mode = vol.split(':')
-                    if re.match(r'[\.~]', host):
-                        host = os.path.abspath(host)
+                    if re.match(r'[.~]', host):
+                        host = os.path.abspath(os.path.expanduser(host))
                     new_vols.append("%s:%s:%s" % (host, container, mode))
                     continue
                 elif len(vol.split(':')) == 2:
                     parts = vol.split(':')
-                    if parts[1] not in VOLUME_PERMISSIONS and re.match(r'[\.~]', parts[0]):
-                        host = os.path.abspath(parts[0])
+                    if parts[1] not in VOLUME_PERMISSIONS and re.match(r'[.~]', parts[0]):
+                        host = os.path.abspath(os.path.expanduser(parts[0]))
                         new_vols.append("%s:%s:rw" % (host, parts[1]))
                         continue
             new_vols.append(vol)


Test patch:
diff --git a/test/sanity/code-smell/use-argspec-type-path.py b/test/sanity/code-smell/use-argspec-type-path.py
--- a/test/sanity/code-smell/use-argspec-type-path.py
+++ b/test/sanity/code-smell/use-argspec-type-path.py
@@ -7,6 +7,7 @@
 def main():
     skip = set([
         # add legitimate uses of expanduser to the following list
+        'lib/ansible/modules/cloud/docker/docker_container.py',  # uses colon-separated paths, can't use type=path
         'lib/ansible/modules/cloud/lxc/lxc_container.py',
         'lib/ansible/modules/cloud/rackspace/rax_files_objects.py',
         'lib/ansible/modules/database/mongodb/mongodb_parameter.py',