GitHub user enchant3dmango added a comment to the discussion: Is
on_execute_callback can be used to initialize operators parameters at execution
?
Your code works, but it’s a hack. Prefer setting env directly in the operator
or using templating. Keep callbacks for side effects, not for parameter wiring.
In your case, I recommend pulling the connection inside the bash_command or
setting it at the DAG parse time.
1. Pull the connection inside the bash_command
```python
from airflow.hooks.base import BaseHook
vc = BaseHook.get_connection("my_connection")
bash = BashOperator(
task_id="also_run_this",
bash_command=f'echo {vc.host}'
)
```
2. Set env at the DAG parse time
```python
from airflow.hooks.base import BaseHook
vc = BaseHook.get_connection("my_connection")
bash = BashOperator(
task_id="also_run_this",
bash_command="echo $varenv",
env={"varenv": vc.host},
)
```
GitHub link:
https://github.com/apache/airflow/discussions/58493#discussioncomment-15192313
----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: [email protected]