gpt4 book ai didi

ansible - 在远程机器上的 virtualenv 中始终运行 ansible 的最佳方法?

转载 作者:行者123 更新时间:2023-12-04 08:35:22 24 4
gpt4 key购买 nike

有没有更好的方法可以在远程机器上的 virtualenv 中运行 ansible?

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

例如:

 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

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

最佳答案

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

不确定它有多干净,但它有效。我只是在 mikepurvis 在这里提到的内容的基础上建立了一点。

---
# 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 包装文件:
#!/bin/bash
source "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/activate"
python $@

关于ansible - 在远程机器上的 virtualenv 中始终运行 ansible 的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20575084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com