diff --git a/lib/ansible/modules/packaging/language/pip.py b/lib/ansible/modules/packaging/language/pip.py index 55f2e6e1e5..fc0dab4088 100644 --- a/lib/ansible/modules/packaging/language/pip.py +++ b/lib/ansible/modules/packaging/language/pip.py @@ -84,7 +84,9 @@ options: description: - The Python executable used for creating the virtual environment. For example C(python3.5), C(python2.7). When not specified, the - Python version used to run the ansible module is used. + Python version used to run the ansible module is used. This parameter + should not be used when C(virtualenv_command) is using C(pyvenv) or + the C(-m venv) module. required: false default: null state: @@ -457,16 +459,27 @@ def main(): if '--no-site-packages' in cmd_opts: cmd += ' --no-site-packages' - if virtualenv_python: - cmd += ' -p%s' % virtualenv_python - elif PY3: - # Ubuntu currently has a patch making virtualenv always - # try to use python2. Since Ubuntu16 works without - # python2 installed, this is a problem. This code mimics - # the upstream behaviour of using the python which invoked - # virtualenv to determine which python is used inside of - # the virtualenv (when none are specified). - cmd += ' -p%s' % sys.executable + # -p is a virtualenv option, not compatible with pyenv or venv + # this if validates if the command being used is not any of them + if not any(ex in module.params['virtualenv_command'] for ex in ('pyvenv', '-m venv')): + if virtualenv_python: + cmd += ' -p%s' % virtualenv_python + elif PY3: + # Ubuntu currently has a patch making virtualenv always + # try to use python2. Since Ubuntu16 works without + # python2 installed, this is a problem. This code mimics + # the upstream behaviour of using the python which invoked + # virtualenv to determine which python is used inside of + # the virtualenv (when none are specified). + cmd += ' -p%s' % sys.executable + + # if venv or pyvenv are used and virtualenv_python is defined, then + # virtualenv_python is ignored, this has to be acknowledged + elif module.params['virtualenv_python']: + module.fail_json( + msg='virtualenv_python should not be used when' + ' using the venv module or pyvenv as virtualenv_command' + ) cmd = "%s %s" % (cmd, env) rc, out_venv, err_venv = module.run_command(cmd, cwd=chdir)