在Ansible剧本

时间:2017-01-03 16:51:56

标签: python ansible anaconda conda virtual-environment

我正在尝试运行一个任务列表这里运行气流,但它可能是真正的)需要在现有的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激活。

如果使用python virtualenv代替conda,我想问题会是一样的。

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

2 个答案:

答案 0 :(得分:2)

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

另一方面,

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连接。

答案 1 :(得分:0)

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

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