geckos
geckos

Reputation: 6299

How ansible find python modules on target and is it possible to use virtualenv to install modules dependencies?

I need to create some snake-oil certificates on some playbook and I'm using openssl_privatekey, openssl_csr and openssl_certificate modules for this. The problem is that this module depens on PyOpenSSL and that CentOS versions of PyOpenSSL are outdated.

To workaround this I install PyOpenSSL from pip, which works, but may overwrite an already existing PyOpenSSL module if that was installed with yum.

On my playbook I'm doing this:

- name: 'Install PyOpenSSL'
  pip:
    name: PyOpenSSL
    state: present
    version: '>= 0.15'

Now, is there a way to install this in a virtual environment, so that ansible is aware of it? If so I can simply remove the virtual environment in the play's end.

Upvotes: 1

Views: 596

Answers (1)

larsks
larsks

Reputation: 312610

You can create a virtualenv with the pip module. You can probably make Ansible aware of that virtualenv by setting the ansible_python_interpreter fact to point at the python binary in the virtualenv, so something like:

---
- hosts: localhost
  gather_facts: false
  tasks:
    - name: install modules
      pip:
        state: present
        name: "{{ modules }}"
        virtualenv: /tmp/venv
      vars:
        modules:
          - pyopenssl

    - set_fact:
        ansible_python_interpreter: /tmp/venv/bin/python

Caveat: I haven't tested this thoroughly. It didn't break anything.

Upvotes: 2

Related Questions