Valeriy Solovyov
Valeriy Solovyov

Reputation: 5648

How to Install ansible in virtualenv?

I need create some virtual environment on one Ubuntu 12.04 and run ansible inside it on another (where I can't install anything (there is no Internet connection).

I tried to install Ansible inside virtual enviroment:

export venv_name="ansible_virt_env"
mkdir ~/venv && cd ~/venv
echo "Virtualenv ${venv_name} directory" > README
virtualenv --no-site-packages --prompt="ansible" ${venv_name:-venv}
. ~/venv/${venv_name:-venv}/bin/activate
~/venv/${venv_name:-venv}/bin/pip install  -U pip
~/venv/${venv_name:-venv}/bin/pip install  -U setuptools
~/venv/${venv_name:-venv}/bin/pip install ansible

But it failed:

ansibleroot@[RDE1.3]:~/venv# ~/venv/${venv_name:-venv}/bin/pip install  -U setuptools
Collecting setuptools    

/root/venv/ansible_virt_env/include/site/python2.7/cffi: Package libffi was not found in the pkg-config search path. Perhaps you should add the directory containing `libffi.pc' to the PKG_CONFIG_PATH environment variable No package 'libffi' found Package libffi was not found in the pkg-config search path.

I installed libffi-dev: apt-get install libffi-dev

And ansible doen't work: /root/venv/ansible_virt_env/bin/ansible ERROR! Unexpected Exception: jinja2 the full traceback was:

Traceback (most recent call last):
  File "/root/venv/ansible_virt_env/bin/ansible", line 75, in <module>
    from ansible.cli.adhoc import AdHocCLI as mycli
  File "/root/venv/ansible_virt_env/local/lib/python2.7/site-packages/ansible/cli/adhoc.py", line 28, in <module>
    from ansible.executor.task_queue_manager import TaskQueueManager
  File "/root/venv/ansible_virt_env/local/lib/python2.7/site-packages/ansible/executor/task_queue_manager.py", line 28, in <module>
    from ansible.executor.play_iterator import PlayIterator
  File "/root/venv/ansible_virt_env/local/lib/python2.7/site-packages/ansible/executor/play_iterator.py", line 29, in <module>
    from ansible.playbook.block import Block
  File "/root/venv/ansible_virt_env/local/lib/python2.7/site-packages/ansible/playbook/__init__.py", line 25, in <module>
    from ansible.playbook.play import Play
  File "/root/venv/ansible_virt_env/local/lib/python2.7/site-packages/ansible/playbook/play.py", line 27, in <module>
    from ansible.playbook.base import Base
  File "/root/venv/ansible_virt_env/local/lib/python2.7/site-packages/ansible/playbook/base.py", line 35, in <module>
    from ansible.parsing.dataloader import DataLoader
  File "/root/venv/ansible_virt_env/local/lib/python2.7/site-packages/ansible/parsing/dataloader.py", line 32, in <module>
    from ansible.parsing.vault import VaultLib
  File "/root/venv/ansible_virt_env/local/lib/python2.7/site-packages/ansible/parsing/vault/__init__.py", line 67, in <module>
    from cryptography.hazmat.primitives.hashes import SHA256 as c_SHA256
  File "/root/venv/ansible_virt_env/local/lib/python2.7/site-packages/cryptography/hazmat/primitives/hashes.py", line 15, in <module>
    from cryptography.hazmat.backends.interfaces import HashBackend
  File "/root/venv/ansible_virt_env/local/lib/python2.7/site-packages/cryptography/hazmat/backends/__init__.py", line 7, in <module>
    import pkg_resources
  File "/root/venv/ansible_virt_env/local/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg/pkg_resources.py", line 2707, in <module>
    working_set.require(__requires__)
  File "/root/venv/ansible_virt_env/local/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg/pkg_resources.py", line 686, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/root/venv/ansible_virt_env/local/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg/pkg_resources.py", line 584, in resolve
    raise DistributionNotFound(req)
DistributionNotFound: jinja2

But jinja2 is present:

ansibleroot@[RDE1.3]:~/venv# /root/venv/ansible_virt_env/bin/python
Python 2.7.3 (default, Dec 18 2014, 19:10:20)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import jinja2
>>>
ansibleroot@[RDE1.3]:~/venv# /root/venv/ansible_virt_env/bin/pip list
ansible (2.0.2.0)
cffi (1.6.0)
cryptography (1.3.2)
distribute (0.6.24)
enum34 (1.1.4)
idna (2.1)
ipaddress (1.0.16)
Jinja2 (2.8)
MarkupSafe (0.23)
paramiko (2.0.0)
pip (8.1.1)
pyasn1 (0.1.9)
pycparser (2.14)
pycrypto (2.6.1)
PyYAML (3.11)
setuptools (21.0.0)
six (1.10.0)

Upvotes: 0

Views: 3605

Answers (2)

benebun
benebun

Reputation: 185

For what it's worth, I had the same problem with ansible 2.1.0.0 (installed via pip), Jinja 2.8, paramiko 2.0.1. Updating ansible to 2.2.1.0 solved the problem (resulting also in an update to Jinja2 to 2.8.1).

Upvotes: 0

guest
guest

Reputation: 21

Hi,you need install this version: pip install paramiko==1.10 pip install Jinja2==2.2

Upvotes: 2

Related Questions