Strong team from inside virtualenv?

It seems like it should be very simple:

tasks: - name: install python packages pip: name=${item} virtualenv=~/buildbot-env with_items: [ buildbot ] - name: create buildbot master command: buildbot create-master ~/buildbot creates=~/buildbot/buildbot.tac 

However, the command will not be executed if the virtualenv script initiator is not found first, and there seems to be no need to do this in the Ansible command module .

I experimented with a search to activate a script in various .profile, .bashrc, .bash_login, etc. files, with no luck. Alternatively, there is a shell command, but it looks like an awkward hack:

 - name: create buildbot master shell: source ~/buildbot-env/bin/activate && \ buildbot create-master ~/buildbot \ creates=~/buildbot/buildbot.tac executable=/bin/bash 

Is there a better way?

+29
virtualenv ansible
Nov 18 '13 at 4:33
source share
5 answers

It is best to use the full path to the installed script - it will run automatically in its virtual directory:

 tasks: - name: install python packages pip: name={{ item }} virtualenv={{ venv }} with_items: [ buildbot ] - name: create buildbot master command: "{{ venv }}/bin/buildbot create-master ~/buildbot creates=~/buildbot/buildbot.tac" 
+17
Mar 05 '14 at 4:33
source share

This is a generalized version of the wrapper method.

venv_exec.j2:

 #!/bin/bash source {{ venv }}/bin/activate $@ 

And then the player:

 tasks: - pip: name={{ item }} virtualenv={{ venv }} with_items: - buildbot - template: src=venv_exec.j2 dest={{ venv }}/exec mode=755 - command: "{{ venv }}/exec buildbot create-master {{ buildbot_master }}" 
+22
Dec 13 '13 at 17:22
source share

Here you can enable the virtual game for the whole game; In this example, a virtual file is created in one playback, then it starts using it.

Not sure how clean it is, but it works. I'm just thinking a bit about what mikepurvis is mentioned here.

 --- # Build virtualenv - hosts: all vars: PROJECT_HOME: "/tmp/my_test_home" ansible_python_interpreter: "/usr/local/bin/python" tasks: - name: "Create virtualenv" shell: virtualenv "{{ PROJECT_HOME }}/venv" creates="{{ PROJECT_HOME }}/venv/bin/activate" - name: "Copy virtualenv wrapper file" synchronize: src=pyvenv dest="{{ PROJECT_HOME }}/venv/bin/pyvenv" # Use virtualenv - hosts: all vars: PROJECT_HOME: "/tmp/my_test_home" ansible_python_interpreter: "/tmp/my_test_home/venv/bin/pyvenv" tasks: - name: "Guard code, so we are more certain we are in a virtualenv" shell: echo $VIRTUAL_ENV register: command_result failed_when: command_result.stdout == "" 

pyenv shell file:

 #!/bin/bash source "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/activate" python $@ 
+7
Dec 14 '13 at 1:36
source share

As I noted above, I create a script, let's say it is called buildbot.sh :

 source ~/buildbot-env/bin/activate buildbot create-master [and more stuff] 

Then run it on the remote control using the following task:

 - name: Create buildbot master script: buildbot.sh 

It still seems unnecessary to me, but perhaps it is cleaner than running it in a shell command. Your player looks cleaner if you donโ€™t see what the script does.

At least some modules seem to use virtualenv, since both django_manage and rax_clb already have a virtualenv parameter built in. Perhaps this is not such a big step so that Ansible can include a module like command-in-virtenv.

+1
Nov 18 '13 at 10:19
source share

Just run the virtual pip in the shell:

 shell: ~/buildbot-env/pip install ${item} 

It works like a charm. I have no idea what the pip module does with virtualenvs, but it seems pretty useless.

+1
Aug 16 '14 at 17:14
source share



All Articles