Ansible creating a virtualenv

前端 未结 3 1108
半阙折子戏
半阙折子戏 2020-12-25 09:53

How do you create a virtualenv for a specific python version using ansible. Is there a command in the ansible standard library?

I would like something like:

相关标签:
3条回答
  • 2020-12-25 10:02

    You can do it with the pip module and a specific virtualenv binary:

    - pip: virtualenv=/path/to/venv virtualenv_command=/path/to/virtualenv3 ...
    
    0 讨论(0)
  • 2020-12-25 10:07

    I have at times experienced some erratic behaviour with specifying virtualenv_command (e.g.: getting a python 2.7 executable in my virtualenv even though I specified to run the command with virtualenv-3.4.

    If you experience that problem, you can create the virtualenv manually with the command module:

    - name: Manually create the initial virtualenv
      command:
        cmd: virtualenv /user/home/venvs/myenv -p python3.4
        creates: "/user/home/venvs/myenv"
    

    (note: by specifying creates this command will only run in the case that a virtualenv does not exist at /user/home/venvs/myenv).

    Then you can install your requirements as normal with the pip command:

    - name: Install requirements
      pip: 
        requirements=/my_app/requirements.txt 
        virtualenv=/user/home/venvs/myenv
    

    Update

    I've found that specifying the virtualenv_python (available in Ansible 2.0+) also seems to work in the case stated above. For example:

    - name: Install requirements
      pip: 
        requirements: /my_app/requirements.txt
        virtualenv: /user/home/venvs/myenv
        virtualenv_python: python3.4
    

    (with this you shouldn't need to manually create the virtualenv first)

    0 讨论(0)
  • 2020-12-25 10:19

    With ansible 2.0 you can specify a python version for your virtualenv with virtualenv_python

    For example:

    - name: Initiate virtualenv
      pip: virtualenv="{{ virtualenv_dir }}" 
           virtualenv_python=python3.4
           requirements={{ app_dir }}/requirements.txt
    
    0 讨论(0)
提交回复
热议问题