本文介绍了在远程机器上的 virtualenv 中始终运行 ansible 的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有更好的方法在远程机器上的 virtualenv 中运行 ansible?

Is there a better way to run ansible inside a virtualenv on the remote machines?

到目前为止,我看到的方法是手动或使用 ansible 本身修改 .bashrc 文件.

So far the way I can see is to modify the .bashrc file, manually or with ansible itself.

例如:

 tasks:
    - name: "Enable virtualenv in .bashrc"
      lineinfile: dest=.bashrc
                  line="source {{ PROJECT_HOME }}/venv/bin/activate"

    #
    # Put tasks that rely on this precondition here (?)
    #

    # Optionally, disable this later on
    - name: "Disable virtualenv in .bashrc"
      lineinfile: dest=.bashrc
                  line="source {{ PROJECT_HOME }}/venv/bin/activate"
                  state=absent

待办事项:检查是否可以使用 ssh 授权密钥完成此操作:http://binblog.info/2008/10/20/openssh-going-flexible-with-forced-commands/

TODO: Check if the ways it could be done using ssh authorized keys: http://binblog.info/2008/10/20/openssh-going-flexible-with-forced-commands/

推荐答案

这是一种为整个游戏启用 virtualenv 的方法;这个例子在一次游戏中构建了 virtualenv,然后开始使用它.

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.

不确定它有多干净,但它有效.我只是在 mikepurvis 在这里提到的内容的基础上进行了一些构建.

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 包装文件:

pyenv wrapper file:

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

这篇关于在远程机器上的 virtualenv 中始终运行 ansible 的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 08:07