我有一个通过airflow API在DAG AIRFLOW中调用python脚本的用例。这个python脚本将通过JSON接收参数。我可以在shell上运行python,它会返回预期的结果。
我的宣传短片如下-
curl -X POST -H "Cache-Control: no-cache" -H "Content-Type: application/json" http://localhost:8080/api/experimental/dags/DAG-3/dag_runs -d '{"conf":"{\"hostname\":\"<servername>\", \"username\":\"<username>\", \"password\":\"password\", \"command1\":\"hostname\" }"}'我的DAG如下:
dag = DAG(
dag_id='DAG-3',
default_args=default_args,
dagrun_timeout=timedelta(minutes=10)
)
#cmd_command = "python3.6 /root/test21.py '{\"hostname\": \"json_data(hostname)\", \"username\":\"json_data(username)\", \"password\":\"json_data(password)\", \"command1\":\"json_data(command)\"}'"
cmd_command = "python3.6 /root/test21.py '{{ hostname }}' '{{ username }}' '{{ password }}' '{{ command1 }}'"
t = BashOperator(
task_id = 'execute_script',
bash_command = cmd_command,
dag = dag)通过这个,我看到API POST中的参数没有被选中,我这样做是正确的吗?当我在API中硬编码包被执行时,我不确定如何通过POST将其作为cmd_command传递
请帮帮忙。
发布于 2020-07-18 01:47:22
需要通过dag_run.conf访问conf。
cmd_command = "python3.6 /root/test21.py '{{ dag_run.conf.hostname }}' '{{ dag_run.conf.username }}' '{{ dag_run.conf.password }}' '{{ dag_run.conf.command1 }}'"
t = BashOperator(
task_id = 'execute_script',
bash_command = cmd_command,
dag = dag)https://stackoverflow.com/questions/62958529
复制相似问题