

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# Apache Airflow CLI 命令参考
<a name="airflow-cli-command-reference"></a>

本主题介绍了 Amazon MWAA 上支持和不支持的 Apache Airflow CLI 命令。

**提示**  
REST API 比 CLI 更现代，专为与外部系统的编程集成而设计。REST 是与 Apache Airflow 交互的首选方式。

**Contents**
+ [先决条件](#airflow-cli-command-prereqs)
  + [访问](#access-airflow-ui-prereqs-access)
  + [AWS CLI](#access-airflow-ui-prereqs-cli)
+ [更改了哪些内容？](#airflow-cli-command-changed)
+ [支持的 CLI 命令](#airflow-cli-commands)
  + [支持的命令](#airflow-cli-commands-supported)
  + [使用解析 DAG 的命令](#parsing-support)
+ [代码示例](#airflow-cli-command-examples)
  + [设置、获取或删除 Apache Airflow v2 变量](#example-airflow-cli-commands-bash)
  + [触发 DAG 时添加配置](#example-airflow-cli-commands-trigger)
  + [在通往堡垒主机的 SSH 隧道上运行 CLI 命令。](#example-airflow-cli-commands-private)

## 先决条件
<a name="airflow-cli-command-prereqs"></a>

下一节介绍了使用本页上的命令和脚本所需的初步步骤。

### 访问
<a name="access-airflow-ui-prereqs-access"></a>
+ 在 AWS Identity and Access Management (IAM) 中访问 [Apache Airflow 用户界面访问策略：亚马逊 MWAAWeb ServerAccess](access-policies.md#web-ui-access) 中的 Amazon MWAA 权限策略的 AWS 账户。
+ 在 AWS Identity and Access Management (IAM) 中访问 Amazon MWAA 权限策略 [完整的 API 和控制台访问政策：Amazon MWAAFull ApiAccess](access-policies.md#full-access-policy) 的 AWS 账户。

### AWS CLI
<a name="access-airflow-ui-prereqs-cli"></a>

AWS Command Line Interface (AWS CLI) 是一种开源工具，您可以用来在命令行 Shell 中使用命令与 AWS 服务进行交互。要完成本节中的步骤，您需要以下满足以下条件：
+ [AWS CLI – 安装版本 2](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html)。
+ [AWS CLI – 使用 `aws configure` 进行快速配置](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)。

## 更改了哪些内容？
<a name="airflow-cli-command-changed"></a>
+ **v3：Airflow 架构**。Apache Airflow v3 引入了突破性的架构更改，以提高安全性和可扩展性，并简化维护。要了解更多信息，请参阅[升级到 Airflow 3](https://airflow.apache.org/docs/apache-airflow/stable/installation/upgrading_to_airflow3.html)。
+ **v2：Airflow CLI 命令结构**。Apache Airflow v2 CLI 的组织方式是将相关命令分组为子命令，这意味着如果您想升级到 Apache Airflow v2，则需要更新 Apache Airflow v1 脚本。例如，Apache Airflow v1 中的 `unpause` 已更新为 Apache Airflow v2 中的 `dags unpause`。要了解更多信息，请参阅 [2.0 中的 Airflow CLI 更改](http://airflow.apache.org/docs/apache-airflow/2.0.2/upgrading-to-2.html#airflow-cli-changes-in-2-0)。

## 支持的 CLI 命令
<a name="airflow-cli-commands"></a>

下一节列出了 Amazon MWAA 上可用的 Apache Airflow CLI 命令。

### 支持的命令
<a name="airflow-cli-commands-supported"></a>

------
#### [ Apache Airflow v3 ]


| 次要版本 | 命令 | 
| --- | --- | 
|  v3.0.6  |  [资产详情](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#details)  | 
|  v3.0.6  |  [资产列表](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#list)  | 
|  v3.0.6  |  [资产实体化](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#materialize)  | 
|  v3.0.6  |  [回填创建](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#create)  | 
|  v3.0.6  |  [备忘单](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#cheat-sheet)  | 
|  v3.0.6  |  [添加连接](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#add)  | 
|  v3.0.6  |  [删除连接](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#delete)  | 
|  v3.0.6  |  [删除 dags](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#delete_repeat1)  | 
|  v3.0.6  |  [dags 列表](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#list_repeat2)  | 
|  v3.0.6  |  [dags 列表-任务](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#list-jobs)  | 
|  v3.0.6  |  [dags 列表导入错误](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#list-import-errors)  | 
|  v3.0.6  |  [dags 列表-运行次数](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#list-runs)  | 
|  v3.0.6  |  [dags 下次-执行](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#next-execution)  | 
|  v3.0.6  |  [暂停 dag](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#pause)  | 
|  v3.0.6  |  [报告 dags](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#report)  | 
|  v3.0.6  |  [重新序列化 dags](https://airflow.apache.org/docs/apache-airflow/2.4.3/cli-and-env-variables-ref.html#reserialize)  | 
|  v3.0.6  |  [显示 dags](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#show)  | 
|  v3.0.6  |  [陈述 dags](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#state)  | 
|  v3.0.6  |  [测试 dags](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#test)  | 
|  v3.0.6  |  [触发 dags](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#trigger)  | 
|  v3.0.6  |  [取消暂停 dags](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#unpause)  | 
|  v3.0.6  |  [清理 db](https://airflow.apache.org/docs/apache-airflow/2.4.3/cli-and-env-variables-ref.html#clean)  | 
|  v3.0.6  |  [提供商的行为](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#behaviours)  | 
|  v3.0.6  |  [获得提供商](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#get_repeat2)  | 
|  v3.0.6  |  [提供商挂钩](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#hooks)  | 
|  v3.0.6  |  [提供商链接](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#links)  | 
|  v3.0.6  |  [提供商列表](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#list_repeat4)  | 
|  v3.0.6  |  [提供程序通知](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#notifications)  | 
|  v3.0.6  |  [提供商密钥](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#secrets)  | 
|  v3.0.6  |  [提供商触发器](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#triggerer)  | 
|  v3.0.6  |  [提供商小部件](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#widgets)  | 
|  v3.0.6  |  [角色添加权限](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#add-perms)  | 
|  v3.0.6  |  [角色删除权限](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#del-perms)  | 
|  v3.0.6  |  [角色创建](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#create)  | 
|  v3.0.6  |  [列出角色](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#list_repeat5)  | 
|  v3.0.6  |  [清除任务](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#clear)  | 
|  v3.0.6  |  [任务失败-部署](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#failed-deps)  | 
|  v3.0.6  |  [列出任务](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#list_repeat6)  | 
|  v3.0.6  |  [渲染任务](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#render)  | 
|  v3.0.6  |  [陈述任务](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#state_repeat1)  | 
|  v3.0.6  |  [dag 运行的任务状态](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#states-for-dag-run)  | 
|  v3.0.6  |  [测试任务](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#test_repeat1)  | 
|  v3.0.6  |  [删除变量](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#delete_repeat4)  | 
|  v3.0.6  |  [获取变量](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#get_repeat3)  | 
|  v3.0.6  |  [设置变量](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#set_repeat1)  | 
|  v3.0.6  |  [列出变量](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#list_repeat8)  | 
|  v3.0.6  |  [version](http://airflow.apache.org/docs/apache-airflow/1.10.12/cli-ref.html#version)  | 

------
#### [ Apache Airflow v2 ]


| 次要版本 | 命令 | 
| --- | --- | 
|  v2.0\$1  |  [备忘单](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#cheat-sheet)  | 
|  v2.0\$1  |  [添加连接](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#add)  | 
|  v2.0\$1  |  [删除连接](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#delete)  | 
|  v2.2\$1（[注意](#parsing-support)）  |  [回填 dags](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#backfill)  | 
|  v2.0\$1  |  [删除 dags](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#delete_repeat1)  | 
|  v2.2\$1（[注意](#parsing-support)）  |  [dags 列表](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#list_repeat2)  | 
|  v2.0\$1  |  [dags 列表-任务](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#list-jobs)  | 
|  v2.6\$1  |  [dags 列表导入错误](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#list-import-errors)  | 
|  v2.2\$1（[注意](#parsing-support)）  |  [dags 列表-运行次数](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#list-runs)  | 
|  v2.2\$1（[注意](#parsing-support)）  |  [dags 下次-执行](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#next-execution)  | 
|  v2.0\$1  |  [暂停 dag](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#pause)  | 
|  v2.0\$1  |  [报告 dags](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#report)  | 
|  v2.4\$1  |  [重新序列化 dags](https://airflow.apache.org/docs/apache-airflow/2.4.3/cli-and-env-variables-ref.html#reserialize)  | 
|  v2.0\$1  |  [显示 dags](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#show)  | 
|  v2.0\$1  |  [陈述 dags](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#state)  | 
|  v2.0\$1  |  [测试 dags](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#test)  | 
|  v2.0\$1  |  [触发 dags](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#trigger)  | 
|  v2.0\$1  |  [取消暂停 dags](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#unpause)  | 
|  v2.4\$1  |  [清理 db](https://airflow.apache.org/docs/apache-airflow/2.4.3/cli-and-env-variables-ref.html#clean)  | 
|  v2.0\$1  |  [提供商的行为](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#behaviours)  | 
|  v2.0\$1  |  [获得提供商](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#get_repeat2)  | 
|  v2.0\$1  |  [提供商挂钩](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#hooks)  | 
|  v2.0\$1  |  [提供商链接](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#links)  | 
|  v2.0\$1  |  [提供商列表](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#list_repeat4)  | 
|  v2.8\$1  |  [提供程序通知](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#notifications)  | 
|  v2.6\$1  |  [提供商密钥](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#secrets)  | 
|  v2.7\$1  |  [提供商触发器](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#triggerer)  | 
|  v2.0\$1  |  [提供商小部件](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#widgets)  | 
|  v2.6\$1  |  [角色添加权限](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#add-perms)  | 
|  v2.6\$1  |  [角色删除权限](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#del-perms)  | 
|  v2.6\$1  |  [角色创建](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#create)  | 
|  v2.0\$1  |  [列出角色](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#list_repeat5)  | 
|  v2.0\$1  |  [清除任务](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#clear)  | 
|  v2.0\$1  |  [任务失败-部署](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#failed-deps)  | 
|  v2.0\$1  |  [列出任务](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#list_repeat6)  | 
|  v2.0\$1  |  [渲染任务](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#render)  | 
|  v2.0\$1  |  [运行任务](https://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#run)  | 
|  v2.0\$1  |  [陈述任务](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#state_repeat1)  | 
|  v2.0\$1  |  [dag 运行的任务状态](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#states-for-dag-run)  | 
|  v2.0\$1  |  [测试任务](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#test_repeat1)  | 
|  v2.0\$1  |  [删除变量](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#delete_repeat4)  | 
|  v2.0\$1  |  [获取变量](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#get_repeat3)  | 
|  v2.0\$1  |  [设置变量](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#set_repeat1)  | 
|  v2.0\$1  |  [列出变量](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#list_repeat8)  | 
|  v2.0\$1  |  [版本](http://airflow.apache.org/docs/apache-airflow/1.10.12/cli-ref.html#version)  | 

------

### 使用解析 DAG 的命令
<a name="parsing-support"></a>

如果环境运行的是 Apache Airflow v2.0.2，则如果 DAG 使用的插件依赖于通过 `requirements.txt` 安装的程序包，则解析 DAG 的 CLI 命令将会失败：

**Apache Airflow v2.0.2**
+ `dags backfill`
+ `dags list`
+ `dags list-runs`
+ `dags next-execution`

如果 DAG 不使用依赖于通过 `requirements.txt` 安装的程序包的插件，则可以使用这些 CLI 命令。

## 代码示例
<a name="airflow-cli-command-examples"></a>

下一节包含使用 Apache Airflow CLI 的不同方法的示例。

### 设置、获取或删除 Apache Airflow v2 变量
<a name="example-airflow-cli-commands-bash"></a>

您可以使用以下示例代码设置、获取或删除 `<script> <mwaa env name> get | set | delete <variable> <variable value> </variable> </variable>` 格式的变量。

```
[ $# -eq 0 ] && echo "Usage: $0 MWAA environment name " && exit

if [[ $2 == "" ]]; then
    dag="variables list"

elif  [ $2 == "get" ] ||  [ $2 == "delete" ] ||  [ $2 == "set" ]; then
    dag="variables $2 $3 $4 $5"

else
    echo "Not a valid command"
    exit 1
fi

CLI_JSON=$(aws mwaa --region $AWS_REGION create-cli-token --name $1) \
    && CLI_TOKEN=$(echo $CLI_JSON | jq -r '.CliToken') \
    && WEB_SERVER_HOSTNAME=$(echo $CLI_JSON | jq -r '.WebServerHostname') \
    && CLI_RESULTS=$(curl --request POST "https://$WEB_SERVER_HOSTNAME/aws_mwaa/cli" \
    --header "Authorization: Bearer $CLI_TOKEN" \
    --header "Content-Type: text/plain" \
    --data-raw "$dag" ) \
    && echo "Output:" \
    && echo $CLI_RESULTS | jq -r '.stdout' | base64 --decode \
    && echo "Errors:" \
    && echo $CLI_RESULTS | jq -r '.stderr' | base64 --decode
```

### 触发 DAG 时添加配置
<a name="example-airflow-cli-commands-trigger"></a>

您可以在 Apache Airflow v2 中使用以下示例代码在触发 DAG 时添加配置，例如 `airflow trigger_dag 'dag_name' —conf '{"key":"value"}'`。

```
import boto3
import json
import requests 
import base64
				
  mwaa_env_name = 'YOUR_ENVIRONMENT_NAME'
  dag_name = 'YOUR_DAG_NAME'
  key = "YOUR_KEY"
  value = "YOUR_VALUE"
  conf = "{\"" + key + "\":\"" + value + "\"}"
				
  client = boto3.client('mwaa')
				
  mwaa_cli_token = client.create_cli_token(
    Name=mwaa_env_name
  )
				
  mwaa_auth_token = 'Bearer ' + mwaa_cli_token['CliToken']
  mwaa_webserver_hostname = 'https://{0}/aws_mwaa/cli'.format(mwaa_cli_token['WebServerHostname'])
  raw_data = "trigger_dag {0} -c '{1}'".format(dag_name, conf)
				
  mwaa_response = requests.post(
    mwaa_webserver_hostname,
    headers={
      'Authorization': mwaa_auth_token,
      'Content-Type': 'text/plain'
    },
    data=raw_data
  )
				
  mwaa_std_err_message = base64.b64decode(mwaa_response.json()['stderr']).decode('utf8')
  mwaa_std_out_message = base64.b64decode(mwaa_response.json()['stdout']).decode('utf8')
				
  print(mwaa_response.status_code)
  print(mwaa_std_err_message)
  print(mwaa_std_out_message)
```

### 在通往堡垒主机的 SSH 隧道上运行 CLI 命令。
<a name="example-airflow-cli-commands-private"></a>

根据以下示例使用连接到 Linux 堡垒主机的 SSH 隧道代理运行 Airflow CLI 命令。

**使用 curl**

1. 

   ```
   ssh -D 8080 -f -C -q -N YOUR_USER@YOUR_BASTION_HOST
   ```

1. 

   ```
   curl -x socks5h://0:8080 --request POST https://YOUR_HOST_NAME/aws_mwaa/cli --header YOUR_HEADERS --data-raw YOUR_CLI_COMMAND
   ```