6

我正在尝试运行需要在现有 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 environment第一个任务中的激活被以下任务忽略。

我想如果使用 apython virtualenv而不是conda.

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

4

2 回答 2

8

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

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

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

于 2017-01-03T21:27:02.130 回答
1

正在寻找类似的东西。找到了一个比多个操作更简洁的解决方案:

- name: Run commands in conda environment
  shell: source activate my_conda_env && airflow {{ item }}
  with_items:
    - initdb
    - webserver -p {{ airflow_webserver_port }}
    - scheduler
于 2018-03-24T06:50:50.277 回答