My PR added the {{ conn.my_conn_id.login }} syntax and it will be available in airflow 2.2.0 (not released yet as of 2021-09-22).
See the unreleased documentation for templates reference here
For 2.1.4 and earlier:
Improving on previous answers,
Define macro per DAG: {{conn.<conn_id>}}
you can get conn.<connection_name>.host syntax by using the following macro:
class ConnectionGrabber:
def __getattr__(self, name):
return Connection.get_connection_from_secrets(name)
dag = DAG(user_defined_macros={'connection': ConnectionGrabber()}
that injects the name connection into the jinja template context, connection is a ConnectionGrabber instance. This ConnectionGrabber provides dynamic/managed attributes, so when you request attribute my_conn_id (like connection.my_conn_id) it will perform a lookup of using the airflow.models.Connection.get_connection_from_secrets and return that, from there you can use the a.m.Connection attributes like host, login, password, etc.
Complete example where the connection mssql is accessed in jinja template with bash_command='echo {{connection.mssql.host }}':
from airflow.models import DAG,Connection
from airflow.operators.bash import BashOperator
from airflow.utils.dates import days_ago
class ConnectionGrabber:
def __getattr__(self, name):
return Connection.get_connection_from_secrets(name)
args = {'owner': 'airflow', 'retries': 3, 'start_date': days_ago(2)}
dag = DAG(
dag_id='test_connection',
default_args=args,
schedule_interval='0 0 * * *',
dagrun_timeout=timedelta(minutes=60),
user_defined_macros={'connection': ConnectionGrabber()}
)
task = BashOperator(task_id='read_connection', bash_command='echo {{connection.mssql.host }}', dag=dag)
Define macro in a plugin: {{macros.conn.value.<conn_id>}}
If you wanted to use this macro in all your DAGs you can wrap it in a plugin like so:
# $AIRFLOW_HOME/plugins/connection_macro.py
from airflow.plugins_manager import AirflowPlugin
from airflow.models import Connection
class ConnectionGrabber:
__name__ = "value"
def __str__(self):
return self.__name__
def __getattr__(self, name):
return Connection.get_connection_from_secrets(name)
class MacrosPlugin(AirflowPlugin):
name = "conn"
macros = [ConnectionGrabber()]
check that the plugin can be loaded with airflow plugins
airflow plugins
name | source | macros
=====+===========================+=======
conn | $PLUGINS_FOLDER/macros.py | value
Then you can use {{ macros.conn.value.<conn_id>.host }} in your jinja templates like so
task = BashOperator(task_id='read_connection', bash_command='echo macros.conn.value.mssql.host = {{macros.conn.value.mssql.host }}', dag=dag)
I also opened an issue to add the conn.value.<conn_id> syntax natively