简体   繁体   English

在Ansible playbook中激活Conda环境

[英]Activating a Conda environment in Ansible playbook

I am trying to run a list of tasks ( here running airflow but it could be anything really ) that require to be executed in a existing Conda environment. 我正在尝试运行一个需要在现有Conda环境中执行的任务列表这里运行气流,但它可能是真的 )。

I would like to do these tasks: 我想做这些任务:

- 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

Of course, this does not work as each task is independent and the conda environment activation in the first task is ignored by the following tasks. 当然,这不起作用,因为每个任务都是独立的,并且第一个任务中的conda environment激活被以下任务忽略。

I guess the issue would be the same if using a python virtualenv instead of conda . 我想如果使用python virtualenv而不是conda ,问题就会一样。

How can I achieve each task being run in the Conda environment? 如何实现在Conda环境中运行的每个任务?

Each of your commands will be executed in a different process. 您的每个命令都将在不同的进程中执行。

source command, on the other hand, is used for reading the environment variables into the current process only (and its children), so it will apply only to the activate conda environment task. 另一方面, source命令用于仅将环境变量读入当前进程(及其子进程),因此它仅适用于activate conda environment任务。

What you can try to do is: 你可以尝试做的是:

- 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

Before, check what's the full path to activate on the target machine with which activate (you need to do it before any environment is sourced). 之前,检查在activate的目标计算机上which activate的完整路径是什么(您需要在任何环境来源之前执行此操作)。 If Conda was installed in a user's space, you should use the same user for the Ansible connection. 如果Conda安装在用户的空间中,则应使用相同的用户进行Ansible连接。

Was looking out for something similar. 正在寻找类似的东西。 Found a neater solution than having multiple actions: 找到一个比多个动作更简洁的解决方案:

- name: Run commands in conda environment
  shell: source activate my_conda_env && airflow {{ item }}
  with_items:
    - initdb
    - webserver -p {{ airflow_webserver_port }}
    - scheduler

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM