diff --git a/lib/ansible/modules/packaging/language/pip.py b/lib/ansible/modules/packaging/language/pip.py index ae66cb23f0..fbe26fb92d 100644 --- 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 diff --git a/test/integration/targets/pip/tasks/pip.yml b/test/integration/targets/pip/tasks/pip.yml index 44c00a786b..b758dbff17 100644 --- 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"