0

I have a python script test2.py to connect to a remote server and execute the command. as below. This works on the command line.

Passing parameters as JSON and getting the response in JSON this works when executed as below in the command line.

python3.6 test2.py  '{"hostname": "<server>", "username":"<test>", "password":"<test1>", "command1":"hostname"}'

I am trying to execute same through the airflow

from __future__ import print_function
from airflow.operators import BashOperator
from airflow.models import DAG
from datetime import datetime, timedelta


default_args = {
    'owner': 'airflow',
    'depends_on_past': False,
    'start_date': datetime(2018, 9, 1),
    'email_on_failure': False,
    'email_on_retry': False,
    'schedule_interval': '@daily',
    'retries': 1,
    'retry_delay': timedelta(seconds=5),
}

dag = DAG(
    dag_id='DAG-3',
    default_args=default_args,
    dagrun_timeout=timedelta(minutes=10)
    )

cmd_command = "python3.6 /root/test2.py '{{"hostname": "<server>", "username":"<test>", "password":"<test1>", "command1":"hostname"}}'"


t = BashOperator(
     task_id = 'some_id',
     bash_command = cmd_command,
     dag = dag)
 

I am seeing below error related to syntax.?

cmd_command = "python3.6 /root/test2.py '{{"hostname": "<server>", "username":"<test>", "password":"<test1>", "command1":"hostname"}}'"
                                                       ^
SyntaxError: invalid syntax

Can you please help

Thank you

1
  • From security standpoint, why is test2.py taking the database credentials as argument? Those variables should be accessed as environment variables inside the test2.py. Commented Jul 7, 2022 at 15:03

2 Answers 2

1

You use double quotes for JSON, but Python interprets them as start or end of a string. One way to resolve this is to escape double quotes inside JSON:

cmd_command = "python3.6 /root/test2.py '{\"hostname\": \"<server>\", \"username\":\"<test>\", \"password\":\"<test1>\", \"command1\":\"hostname\"}'"
Sign up to request clarification or add additional context in comments.

Comments

0

I agree with Sergiy, you've got repeated " in your python line.

  cmd_args= r'{"hostname": "<server>", "username":"<test>", "password":"<test1>", "command1":"hostname"}'
  cmd_command = f"python3.6 /root/test2.py '{cmd_args}'"

where

another approach is to pass arguments by env variables:

bash_task = BashOperator(
    task_id="bash_task",
    bash_command="$PYTHON /root/test2.py '$my_params'",
    env={"my_params": r'{"hostname": "<server>", "username":"<test>", "password":"<test1>", "command1":"hostname"}', 
         "PYTHON": 'python3.6'},
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.