Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running mkvirtualenv using ansible

I am provisioning a machine using ansible. I managed to install virtualenv and virtualenvwrapper fine on the vm. However, I can't seem to create a virtualenv on the vm.

I am trying using

- name: create virtualenv test
  shell: >
    executable=/bin/zsh
    source `which virtualenvwrapper.sh` && mkvirtualenv test
  register: run_cmd

and

- name: create virtualenv test
  action: command mkvirtualenv test

but no luck. Any ideas?

like image 703
Mike Avatar asked Apr 09 '14 11:04

Mike


3 Answers

You can create an environment using mkvirtualenv like this. I was hoping to be able to use the toggleglobalsitepackages, but I found that toggling is not so convenient in an automated session.

- name: Make a virtualenv
  shell: . /usr/share/virtualenvwrapper/virtualenvwrapper.sh && mkvirtualenv {{ venv }}
  args:
    executable: /bin/bash
    creates: "{{ venvabs }}"
like image 115
SiggyF Avatar answered Nov 12 '22 21:11

SiggyF


Source only adds virtualenvwrappers to the shell its invoked in, which you then exit immediately. In any case, I would not use virtualenvwrapper for this. Invoke virtualenv directly.

like image 45
SingleNegationElimination Avatar answered Nov 12 '22 21:11

SingleNegationElimination


Additionally to @SiggyF's excellent answer, I would like to add: In case that it appears that this ansible task fails, as it happens somehow with me, you can use the failed_when feature (ansible 1.4+):

- name: Make virtualenv
  shell: "./usr/share/virtualenvwrapper/virtualenvwrapper.sh && mkvirtualenv {{ project }} --python={{ python }} --no-site-packages"
  args:
    executable: /bin/bash
    creates: "{{ virtualenv_dir }}/{{ project }}"
  register: mkvirtualenv
  failed_when: 'mkvirtualenv.changed and "New python executable" not in mkvirtualenv.stdout'
like image 5
Wtower Avatar answered Nov 12 '22 21:11

Wtower