We are using Airflow as a scheduler. I want to invoke a simple bash operator in a DAG. The bash script needs a password as an argument to do further processing.
How can I store a password securely in Airflow (config/variables/connection
) and access it in dag definition file?
I am new to Airflow and Python so a code snippet will be appreciated.
You can store the password in a Hook - this will be encrypted so long as you have setup your fernet key. To access this password: from airflow.
The Airflow BashOperator does exactly what you are looking for. It is a very simple but powerful operator, allowing you to execute either a bash script, a command or a set of commands from your DAGs.
BaseHook. Abstract base class for hooks, hooks are meant as an interface to. DiscoverableHook. Interface that providers can implement to be discovered by ProvidersManager.
You can store the password in a Hook - this will be encrypted so long as you have setup your fernet key.
Here is how you can create a connection via the UI:
Then:
To access this password:
from airflow.hooks.base_hook import BaseHook # Deprecated in Airflow 2 connection = BaseHook.get_connection("username_connection") password = connection.password # This is a getter that returns the unencrypted password.
Update since Airflow 2 launch
The library airflow.hooks.base_hook
has been deprecated and you must use airflow.hooks.base
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With