safex
safex

Reputation: 2514

programatically set connections / variables in airflow

Is there a way to set connections / variables programtically in airflow? I am aware this is defeating the very purpose of not exposing these details in the code, but to debug it would really help me big time if I could do something like the following pseudo code:

# pseudo code
from airflow import connections
connections.add({name:'...',
                 user:'...'})

Upvotes: 0

Views: 1176

Answers (1)

balderman
balderman

Reputation: 23825

Connection is DB entity and you can create it. See below

from airflow import settings
from airflow.models import Connection
conn = Connection(
        conn_id=conn_id,
        conn_type=conn_type,
        host=host,
        login=login,
        password=password,
        port=port
) 
session = settings.Session() 
session.add(conn)
session.commit() 

As for variables - just use the API. See example below

from airflow.models import Variable
Variable.set("my_key", "my_value")

A good blog post on this topic can be found here.

Upvotes: 2

Related Questions