Repository: ansible/ansible

Issue:
pip: Does not use chdir argument when building path to pip
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
pip

##### ANSIBLE VERSION
```
ansible 2.3.0.0
  config file =
  configured module search path = Default w/o overrides
  python version = 2.7.13 (default, May 16 2017, 10:06:45) [GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)]
```

##### CONFIGURATION
None

##### OS / ENVIRONMENT
macOS targeting Ubuntu, but it's general issue.

##### SUMMARY

When I use the `chdir` argument to pip together with `virtualenv` I expect it to be able to install things since they together should make up a full path to the pip executable.

But when I run I get an error saying pip can't be found. I notice that if I change the path to the virtualenv to be absolute then I can install things. This seems incongruent with the `chdir` argument.

It looks like it is this line that would need to be modified to take `chdir` into account: https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/packaging/language/pip.py#L330

##### STEPS TO REPRODUCE

<!--- Paste example playbooks or commands between quotes below -->
```yaml
- name: Install Python dependencies
  pip:
    chdir: "/home/app/"
    requirements: requirements.txt
    virtualenv: "/venv"
    virtualenv_site_packages: yes
    state: present

```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
Installing my requirements
##### ACTUAL RESULTS
pip or pip2 executable cannot be found.

<!--- Paste verbatim command output between quotes below -->
```
fatal: [my-app]: FAILED! => {"changed": false, "failed": true, "msg": "Unable to find pip in the virtualenv, venv, under any of these names: pip2, pip. Make sure pip is present in the virtualenv."}
```



Discussion:
cc @mattupstate
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
@bjorn-spire I am unable to reproduce this using Ansible 2.3.

Resolving patch:
diff --git a/lib/ansible/modules/packaging/language/pip.py b/lib/ansible/modules/packaging/language/pip.py
--- a/lib/ansible/modules/packaging/language/pip.py
+++ b/lib/ansible/modules/packaging/language/pip.py
@@ -380,6 +380,11 @@ def main():
     virtualenv_python = module.params['virtualenv_python']
     chdir = module.params['chdir']
     umask = module.params['umask']
+    env = module.params['virtualenv']
+
+    venv_created = False
+    if chdir:
+        env = os.path.join(chdir, env)
 
     if umask and not isinstance(umask, int):
         try:
@@ -402,8 +407,6 @@ def main():
         err = ''
         out = ''
 
-        env = module.params['virtualenv']
-        venv_created = False
         if env:
             if not os.path.exists(os.path.join(env, 'bin', 'activate')):
                 venv_created = True


Test patch:
diff --git a/test/integration/targets/pip/tasks/pip.yml b/test/integration/targets/pip/tasks/pip.yml
--- a/test/integration/targets/pip/tasks/pip.yml
+++ b/test/integration/targets/pip/tasks/pip.yml
@@ -183,3 +183,22 @@
   assert:
     that:
       - "pip_install_venv.changed"
+
+# https://github.com/ansible/ansible/issues/25122
+- name: ensure is a fresh virtualenv
+  file:
+    state: absent
+    name: "{{ output_dir }}/pipenv"
+
+- name: install requirements file into virtual + chdir
+  pip:
+   name: q
+   chdir: "{{ output_dir }}/"
+   virtualenv: "pipenv"
+   state: present
+  register: venv_chdir
+
+- name: make sure fresh virtualenv + chdir report changed
+  assert:
+    that:
+      - "venv_chdir.changed"