Repository: ansible/ansible-modules-core

Issue:
Ansible requires the virtualenv command even when it doesn't use it
##### Issue Type:

Bug Report
##### Ansible Version:

```
ansible 1.8.2
  configured module search path = None
```
##### Environment:

Running from OS X 10.10.1
Managing Ubuntu 14.04.1
##### Summary:

When installing a package to an existing virtualenv directory, Ansible requires the virtualenv command even though it doesn't use it. 

When virtualenv isn't available on the standard PATH you must include the `virtualenv_command` argument for no reason. As a workaround, one can use `virtualenv_command=/bin/true` to avoid adding additional host specific details (which may change). Instead, Ansible shouldn't look for a command it won't use (i.e., when the virtualenv already exists).
##### Steps To Reproduce:

```
$ ansible all -m pip -a "name=pep8 virtualenv=~/.virtualenvs/pizza"
```
##### Expected Results:

```
trusty.local | success >> {
    "changed": true,
    "cmd": "/home/brainsik/.virtualenvs/pizza/bin/pip install pep8",
    "name": "pep8",
    "requirements": null,
    "state": "present",
    "stderr": "",
    "stdout": "Downloading/unpacking pep8\n  Downloading pep8-1.5.7-py2.py3-none-any.whl\nInstalling collected packages: pep8\nSuccessfully installed pep8\nCleaning up...\n",
    "version": null,
    "virtualenv": "/home/brainsik/.virtualenvs/pizza"
}
```
##### Actual Results:

```
trusty.local | FAILED >> {
    "failed": true,
    "msg": "Failed to find required executable virtualenv"
}
```



Discussion:

Resolving patch:
diff --git a/packaging/language/pip.py b/packaging/language/pip.py
--- a/packaging/language/pip.py
+++ b/packaging/language/pip.py
@@ -98,7 +98,7 @@
     required: false
     default: null
 notes:
-   - Please note that virtualenv (U(http://www.virtualenv.org/)) must be installed on the remote host if the virtualenv parameter is specified.
+   - Please note that virtualenv (U(http://www.virtualenv.org/)) must be installed on the remote host if the virtualenv parameter is specified and the virtualenv needs to be initialized.
 requirements: [ "virtualenv", "pip" ]
 author: Matt Wright
 '''
@@ -252,12 +252,14 @@ def main():
 
     if env:
         env = os.path.expanduser(env)
-        virtualenv = os.path.expanduser(virtualenv_command)
-        if os.path.basename(virtualenv) == virtualenv:
-            virtualenv = module.get_bin_path(virtualenv_command, True)
         if not os.path.exists(os.path.join(env, 'bin', 'activate')):
             if module.check_mode:
                 module.exit_json(changed=True)
+
+            virtualenv = os.path.expanduser(virtualenv_command)
+            if os.path.basename(virtualenv) == virtualenv:
+                virtualenv = module.get_bin_path(virtualenv_command, True)
+
             if module.params['virtualenv_site_packages']:
                 cmd = '%s --system-site-packages %s' % (virtualenv, env)
             else:
@@ -278,7 +280,7 @@ def main():
     pip = _get_pip(module, env, module.params['executable'])
 
     cmd = '%s %s' % (pip, state_map[state])
-    
+
     # If there's a virtualenv we want things we install to be able to use other
     # installations that exist as binaries within this virtualenv. Example: we 
     # install cython and then gevent -- gevent needs to use the cython binary, 
@@ -308,7 +310,7 @@ def main():
         cmd += ' %s' % _get_full_name(name, version)
     elif requirements:
         cmd += ' -r %s' % requirements
-    
+
     this_dir = tempfile.gettempdir()
     if chdir:
         this_dir = os.path.join(this_dir, chdir)
@@ -319,7 +321,7 @@ def main():
         elif name.startswith('svn+') or name.startswith('git+') or \
                 name.startswith('hg+') or name.startswith('bzr+'):
             module.exit_json(changed=True)
-        
+
         freeze_cmd = '%s freeze' % pip
         rc, out_pip, err_pip = module.run_command(freeze_cmd, cwd=this_dir)
 


Test patch: