使用ansible运行mkvirtualenv

时间:2014-04-09 11:21:33

标签: virtualenv ansible

我正在使用ansible配置一台机器。我设法在虚拟机上安装virtualenv和virtualenvwrapper。但是,我似乎无法在虚拟机上创建虚拟现实。

我正在尝试使用

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

- name: create virtualenv test
  action: command mkvirtualenv test

但没有运气。有什么想法吗?

3 个答案:

答案 0 :(得分:13)

您可以像这样使用mkvirtualenv创建环境。我希望能够使用toggleglobalsitepackages,但我发现在自动化会话中切换不太方便。

- name: Make a virtualenv
  shell: . /usr/share/virtualenvwrapper/virtualenvwrapper.sh && mkvirtualenv {{ venv }}
  args:
    executable: /bin/bash
    creates: "{{ venvabs }}"

答案 1 :(得分:5)

Source只将virtualenvwrappers添加到其调用的shell中,然后立即退出。无论如何,我不会为此使用virtualenvwrapper。直接调用virtualenv

答案 2 :(得分:4)

除了@ SiggyF的优秀答案之外,我想补充一点:如果这个ansible任务看起来失败了,就像我在某种程度上发生的那样,你可以使用failed_when功能(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'
相关问题