gpt4 book ai didi

python - 在 Ansible 剧本中激活 Conda 环境

转载 作者:太空宇宙 更新时间:2023-11-03 15:59:11 25 4
gpt4 key购买 nike

我正在尝试运行需要在现有 Conda 环境中执行的任务列表(此处运行气流,但它实际上可以是任何东西)。

我想完成这些任务:

- name: activate conda environment
# does not work, just for the sake of understanding
command: source activate my_conda_env

- name: initialize the database
command: airflow initdb

- name: start the web server
command: 'airflow webserver -p {{ airflow_webserver_port }}'

- name: start the scheduler
command: airflow scheduler

当然,这是行不通的,因为每个任务都是独立的,第一个任务中的 conda 环境 激活会被后续任务忽略。

我想如果使用 python virtualenv 而不是 conda,问题也会一样。

如何实现在 Conda 环境中运行的每个任务?

最佳答案

您的每个命令都将在不同的进程中执行。

另一方面,

source 命令仅用于将环境变量读入当前进程(及其子进程),因此它仅适用于 activate conda environment 任务。

你可以尝试做的是:

- name: initialize the database
shell: source /full/path/to/conda/activate my_conda_env && airflow initdb
args:
executable: /bin/bash

- name: start the web server
shell: 'source /full/path/to/conda/activate my_conda_env && airflow webserver -p {{ airflow_webserver_port }}'
args:
executable: /bin/bash

- name: start the scheduler
shell: source /full/path/to/conda/activate my_conda_env && airflow scheduler
args:
executable: /bin/bash

之前,使用 which activate 检查目标机器上 activate 的完整路径是什么(您需要在获取任何环境之前执行此操作)。如果 Conda 安装在用户的空间中,您应该使用相同的用户进行 Ansible 连接。

关于python - 在 Ansible 剧本中激活 Conda 环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41448447/

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