Ansible command from inside virtualenv?

后端 未结 5 885
不知归路
不知归路 2021-01-30 08:49

This seems like it should be really simple:

tasks:
- name: install python packages
  pip: name=${item} virtualenv=~/buildbot-env
  with_items: [ buildbot ]
- nam         


        
5条回答
  •  情深已故
    2021-01-30 09:32

    Here's a way to enable the virtualenv for an entire play; this example builds the virtualenv in one play, then starts using it the next.

    Not sure how clean it is, but it works. I'm just building a bit on what mikepurvis 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 wrapper file:

    #!/bin/bash
    source "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/activate"
    python $@
    

提交回复
热议问题