GitHub user rahidzeynal added a comment to the discussion: SSHOperator error:
TemplateNotFound
Here's a professional and concise comment you can post in the GitHub issue to
share your findings and workaround for the Airflow `SSHOperator` templating
issue:
---
I faced the exact same issue with Airflow **2.7.2**, where passing an absolute
shell command like `/bin/bash /home/user/test.sh` to `SSHOperator` caused a
`TemplateNotFound` error. It seems that Airflow was trying to treat the entire
command string as a Jinja template path.
### Root Cause:
The `command` field in `SSHOperator` is templated by default. So if the command
string resembles a file path, Jinja tries to load it as a template from the
DAGs folder, which leads to the `TemplateNotFound` error.
### Workaround / Solution:
To bypass this templating behavior, I subclassed `SSHOperator` and disabled
templating for the `command` field:
```python
from airflow.providers.ssh.operators.ssh import SSHOperator
class NoTemplateSSHOperator(SSHOperator):
template_fields = () # Disable all templating for this operator
```
Then used it like this:
```python
execute_SAP = NoTemplateSSHOperator(
task_id='submit_sh',
ssh_conn_id='your_ssh_conn_id',
command='/bin/bash /path/to/sh/file/sample_sh_echo.sh',
conn_timeout=2400,
cmd_timeout=2400,
)
```
This effectively prevents Airflow from interpreting the command as a template
path.
---
Starting from Airflow 2.8, there's a better way to prevent templating without
modifying the operator — by wrapping the string with the built-in literal()
function.
```python
from airflow.utils.template import literal
execute_SAP = SSHOperator(
task_id='submit_sh',
ssh_conn_id='your_ssh_conn_id',
command=literal('/bin/bash /path/to/sh/file/sample_sh_echo.sh'),
conn_timeout=2400,
cmd_timeout=2400,
)
```
This approach is cleaner and avoids the need for subclassing.
---
GitHub link:
https://github.com/apache/airflow/discussions/52767#discussioncomment-13837280
----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: [email protected]