首页 > 解决方案 > Airflow airflow.exceptions.AirflowException:无法创建远程临时文件 SSHExecuteOperator

问题描述

我正在尝试在 Airflow 中运行简单的 SSHExecutorOperator。

这是我的 .py 文件:

from airflow.contrib.hooks.ssh_hook import SSHHook
from datetime import timedelta

default_args = {
'owner': 'airflow',
'start_date':airflow.utils.dates.days_ago(2),
'retries': 3

}

dag = DAG('Nas_Hdfs', description='Simple tutorial DAG',
      schedule_interval=None,default_args=default_args,
      catchup=False)

sshHook = SSHHook(conn_id='101')
sshHook.no_host_key_check = True

t2 = SSHExecuteOperator(task_id="NAS_TO_HDFS_FILE_COPY",
bash_command="hostname ",
ssh_hook=sshHook,
    dag=dag
    )

t2

连接 ID 101 如下所示: 来自 UI 的连接配置

我收到以下错误:

ERROR - Failed to create remote temp file

这是完整的日志:

INFO - Subtask: --------------------------------------------------------------------------------
INFO - Subtask: Starting attempt 1 of 4
INFO - Subtask: --------------------------------------------------------------------------------
INFO - Subtask: 
INFO - Subtask: [2018-05-28 08:54:22,812] {models.py:1342} INFO - Executing <Task(SSHExecuteOperator): NAS_TO_HDFS_FILE_COPY> on 2018-05-28 08:54:12.876538
INFO - Subtask: [2018-05-28 08:54:23,303] {models.py:1417} ERROR - Failed to create remote temp file
INFO - Subtask: Traceback (most recent call last):
INFO - Subtask:   File "/opt/miniconda3/lib/python2.7/site-packages/airflow/models.py", line 1374, in run
INFO - Subtask:     result = task_copy.execute(context=context)
INFO - Subtask:   File "/opt/miniconda3/lib/python2.7/site-packages/airflow/contrib/operators/ssh_execute_operator.py", line 128, in execute
INFO - Subtask:     self.task_id) as remote_file_path:
INFO - Subtask:   File "/opt/miniconda3/lib/python2.7/site-packages/airflow/contrib/operators/ssh_execute_operator.py", line 64, in __enter__
INFO - Subtask:     raise AirflowException("Failed to create remote temp file")
INFO - Subtask: AirflowException: Failed to create remote temp file
INFO - Subtask: [2018-05-28 08:54:23,304] {models.py:1433} INFO - Marking task as UP_FOR_RETRY
INFO - Subtask: [2018-05-28 08:54:23,342] {models.py:1462} ERROR - Failed to create remote temp file
INFO - Subtask: Traceback (most recent call last):
INFO - Subtask:   File "/opt/miniconda3/bin/airflow", line 28, in <module>
INFO - Subtask:     args.func(args)
INFO - Subtask:   File "/opt/miniconda3/lib/python2.7/site-packages/airflow/bin/cli.py", line 422, in run
INFO - Subtask:     pool=args.pool,
INFO - Subtask:   File "/opt/miniconda3/lib/python2.7/site-packages/airflow/utils/db.py", line 53, in wrapper
INFO - Subtask:     result = func(*args, **kwargs)
INFO - Subtask:   File "/opt/miniconda3/lib/python2.7/site-packages/airflow/models.py", line 1374, in run
INFO - Subtask:     result = task_copy.execute(context=context)
INFO - Subtask:   File "/opt/miniconda3/lib/python2.7/site-packages/airflow/contrib/operators/ssh_execute_operator.py", line 128, in execute
INFO - Subtask:     self.task_id) as remote_file_path:
INFO - Subtask:   File "/opt/miniconda3/lib/python2.7/site-packages/airflow/contrib/operators/ssh_execute_operator.py", line 64, in __enter__
INFO - Subtask:     raise AirflowException("Failed to create remote temp file")
INFO - Subtask: airflow.exceptions.AirflowException: Failed to create remote temp file
INFO - Task exited with return code 1

非常感谢任何帮助!

编辑:我在我的气流用户 python shell 中运行了它,这是输出:

from airflow.contrib.hooks.ssh_hook import SSHHook 
sshHook = SSHHook(conn_id='101') 
sshHook.no_host_key_check = True 
sshHook.Popen(["-q", "mktemp", "--tmpdir", "tmp_XXXXXX"])

输出: SshHook 输出

标签: pythonairflowdirected-acyclic-graphsairflow-scheduler

解决方案


确保遵循以下 3 个步骤:

  1. 使用 ssh 密钥代替密码
  2. “key_file”使用 id_rsa 文件而不是 id_rsa.pub
  3. 气流需要所有者和权限 0600 才能触摸 id_rsa 和 id_rsa.pub 文件

推荐阅读