how to capture SSH operator output to a variable in airflow using xcoms or any other way

Question:

I am new to xcoms in airflow and unable to get exact information , we have an ssh operator task as below with "echo" command

task_id = SSHOperator(
    task_id="task_id",
    ssh_conn_id="CONNECTION_ID",
    command="echo Failed"
)

is there any way to capture the above task output to a variable like

variablea = "Failed"

Any suggestion pls.. I tried xcom push=True but it is displaying blank value..

Asked By: Arya

||

Answers:

While trying to use xcom push it displays something output as "VHJ1ZQo=" or with different characters. Any suggestion pls

SSH operator returns bytes object if pickling is not enabled (see source code) if enable_xcom_pickling is false (which is the default) the return value from the command is being encoded with Base64 and then pushed to Xcom. If you will decode VHJ1ZQo= from Base64 you will get True.

You need to decode the value you read from Xcom before you use it.
You probably just need to run

import base64
....
xcom_pull_value = func_to_get_xcom() # replace how you retrieve the xcom
value = base64.b64decode(xcom_pull_value).decode('utf-8')
print(value)

to get the value after decoding from base64.

Answered By: Elad Kalif
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.