通常我們部署airflow調度系統的時候,默認是直接以admin用戶登錄進來的,而且不需要輸入賬號密碼
如果業務要求必須通過不同的用戶登錄進來,可以采用以下的方法給airflow添加用戶
在 airflow.cfg 文件中 [webserver] 下添加如下配置
[webserver] authenticate = True auth_backend = airflow.contrib.auth.backends.password_auth
在這里提醒一下,authenticate這個配置項在配置文件里面很多地方都有,非常容易配錯了,如果配置錯了,很容易掉坑里
個人建議先在配置文件找到[webserver],然后直接添加內容,然后保存退出
接下來通過命令行添加用戶,我這里的airflow是部署在容器里面的,先進入airflow所在的容器,如果部署在服務器上就直接在服務器里執行
以下是添加用戶的命令
python
import airflow from airflow import models, settings from airflow.contrib.auth.backends.password_auth import PasswordUser user = PasswordUser(models.User()) user.username = 'usertest1' user.email = 'usertest1@163.com' user.password = 'usertest1' user.superuser = 1 //賦予管理員權限,如果是普通用戶就不需要這個 session = settings.Session() session.add(user) session.commit() session.close() exit()
考慮到現在新的airflow版本用的是python3,在執行語句
from airflow.contrib.auth.backends.password_auth import PasswordUser
的時候會報錯,大概也是是沒有對應的包,可以通過 pip install 包名 來下載對應的依賴包。
下面是具體例子
root@airflowdataxv2-copy-dfd9d84f4-9hr9b:/usr/local/airflow# python Python 3.7.5 (default, Nov 15 2019, 02:58:08) [GCC 6.3.0 20170516] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import airflow /usr/local/lib/python3.7/site-packages/airflow/configuration.py:226: FutureWarning: The task_runner setting in [core] has the old default value of 'BashTaskRunner'. This value has been changed to 'StandardTaskRunner' in the running config, but please update your config before Apache Airflow 2.0. FutureWarning /usr/local/lib/python3.7/site-packages/airflow/configuration.py:606: DeprecationWarning: Specifying both AIRFLOW_HOME environment variable and airflow_home in the config file is deprecated. Please use only the AIRFLOW_HOME environment variable and remove the config file entry. warnings.warn(msg, category=DeprecationWarning) /usr/local/lib/python3.7/site-packages/airflow/utils/sqlalchemy.py:40: DeprecationWarning: get: Accessing configuration method 'get' directly from the configuration module is deprecated. Please access the configuration from the 'configuration.conf' object via 'conf.get' tz = conf.get("core", "default_timezone") /usr/local/lib/python3.7/site-packages/airflow/utils/timezone.py:30: DeprecationWarning: get: Accessing configuration method 'get' directly from the configuration module is deprecated. Please access the configuration from the 'configuration.conf' object via 'conf.get' tz = conf.get("core", "default_timezone") /usr/local/lib/python3.7/site-packages/airflow/config_templates/airflow_local_settings.py:65: DeprecationWarning: The elasticsearch_host option in [elasticsearch] has been renamed to host - the old setting has been used, but please update your config. ELASTICSEARCH_HOST = conf.get('elasticsearch', 'HOST') /usr/local/lib/python3.7/site-packages/airflow/config_templates/airflow_local_settings.py:67: DeprecationWarning: The elasticsearch_log_id_template option in [elasticsearch] has been renamed to log_id_template - the old setting has been used, but please update your config. ELASTICSEARCH_LOG_ID_TEMPLATE = conf.get('elasticsearch', 'LOG_ID_TEMPLATE') /usr/local/lib/python3.7/site-packages/airflow/config_templates/airflow_local_settings.py:69: DeprecationWarning: The elasticsearch_end_of_log_mark option in [elasticsearch] has been renamed to end_of_log_mark - the old setting has been used, but please update your config. ELASTICSEARCH_END_OF_LOG_MARK = conf.get('elasticsearch', 'END_OF_LOG_MARK') [2020-04-08 17:41:17,838] {settings.py:252} INFO - settings.configure_orm(): Using pool settings. pool_size=5, max_overflow=10, pool_recycle=1800, pid=21338 >>> from airflow import models, settings >>> from airflow.contrib.auth.backends.password_auth import PasswordUser >>> user = PasswordUser(models.User()) >>> user.username = 'usertest' >>> user.email = 'usertest@163.com' >>> user.password = 'usertest' >>> session = settings.Session() >>> session.add(user) >>> session.commit() >>> user = PasswordUser(models.User()) >>> user.username = 'usertest1' >>> user.email = 'usertest1@163.com' >>> user.password = 'usertest1' >>> session = settings.Session() >>> session.add(user) >>> session.commit() >>> session.close() >>> exit()
然后重啟一下web,進入瀏覽器

點擊右上角的退出

這個時候就可以通過用戶名密碼登錄了!!
