ansible:为virtualenv添加一些项目特定的路径

时间:2016-03-28 18:26:26

标签: python pip ansible ansible-playbook

我需要将projectlib目录添加到ansible playbook中的virtualenv中。 我正在安装这样的依赖项:

- pip: 
    name: virtualenv

- pip:
    requirements: "{{project_dir}}requirements/development.txt"
    virtualenv: "{{ virtualenv_path }}"
    state: latest

在开发机器上,我可以这样做:

$ add2virtualenv project
$ add2virtualenv lib

但我不想在服务器上安装virtualenvwrapper。 那么,在这种情况下,用ansible正确设置virtualenv的惯用方法是什么?

1 个答案:

答案 0 :(得分:0)

您可以使用以下shell脚本,而不是add2virtualenv

doing this
  

将指定的目录添加到Python的路径中   目前活跃的virtualenv。

     

语法:

     

add2virtualenv directory1 directory2 ...

     

有时需要共享未安装的已安装软件包   系统site-packages目录,不应安装   在每个virtualenv。一种可能的解决方案是对源进行符号链接   进入环境site-packages目录,但也很容易   通过将它们包含在.pth中,将额外的目录添加到PYTHONPATH中   使用add2virtualenv在site-packages中的文件。

Check out the source for a big project, such as Django.
Run: add2virtualenv path_to_source.
Run: add2virtualenv.
A usage message and list of current “extra” paths is printed.
     

目录名称将添加到名为的路径文件中   _virtualenv_path_extensions.pth位于环境的site-packages目录中。

Shell脚本:

# Path management for packages outside of the virtual env.
# Based on a contribution from James Bennett and Jannis Leidel.
#
# add2virtualenv directory1 directory2 ...
#
# Adds the specified directories to the Python path for the
# currently-active virtualenv. This will be done by placing the
# directory names in a path file named
# "virtualenv_path_extensions.pth" inside the virtualenv's
# site-packages directory; if this file does not exist, it will be
# created first.
#
#:help:add2virtualenv: add directory to the import path
function add2virtualenv {
    virtualenvwrapper_verify_workon_home || return 1
    virtualenvwrapper_verify_active_environment || return 1

    site_packages="`virtualenvwrapper_get_site_packages_dir`"

    if [ ! -d "${site_packages}" ]
    then
        echo "ERROR: currently-active virtualenv does not appear to have a site-packages directory" >&2
        return 1
    fi

    # Prefix with _ to ensure we are loaded as early as possible,
    # and at least before easy_install.pth.
    path_file="$site_packages/_virtualenv_path_extensions.pth"

    if [ "$*" = "" ]
    then
        echo "Usage: add2virtualenv dir [dir ...]"
        if [ -f "$path_file" ]
        then
            echo
            echo "Existing paths:"
            cat "$path_file" | grep -v "^import"
        fi
        return 1
    fi

    remove=0
    if [ "$1" = "-d" ]
    then
        remove=1
        shift
    fi

    if [ ! -f "$path_file" ]
    then
        echo "import sys; sys.__plen = len(sys.path)" > "$path_file" || return 1
        echo "import sys; new=sys.path[sys.__plen:]; del sys.path[sys.__plen:]; p=getattr(sys,'__egginsert',0); sys.path[p:p]=new; sys.__egginsert = p+len(new)" >> "$path_file" || return 1
    fi

    for pydir in "$@"
    do
        absolute_path="$(virtualenvwrapper_absolutepath "$pydir")"
        if [ "$absolute_path" != "$pydir" ]
        then
            echo "Warning: Converting \"$pydir\" to \"$absolute_path\"" 1>&2
        fi

        if [ $remove -eq 1 ]
        then
            sed -i.tmp "\:^$absolute_path$: d" "$path_file"
        else
            sed -i.tmp '1 a\
'"$absolute_path"'
' "$path_file"
        fi
        rm -f "${path_file}.tmp"
    done
    return 0
}