Execute Rundeck job from Python script

589 views
Skip to first unread message

Rahul Saxena

unread,
Aug 14, 2023, 12:27:03 PM8/14/23
to rundeck...@googlegroups.com
Hi Team,

I am trying to execute a Rundeck job from a Python script but I am getting a 403 error. I can execute the same job from UI with the same login details without any issue but from Python script I get 403.  Below is the Python code:

import requests
from requests.auth import HTTPBasicAuth

# Replace these values with your Rundeck instance details
RUNDECK_URL = "https://rundeckurl.test.com"
RUNDECK_USERNAME = "testuser
RUNDECK_PASSWORD = "testpassword"

# Replace these values with the actual job details
PROJECT_NAME = "Self-Service-Project"
JOB_ID = "b9364f08-6a88-473c-9386-03e60343cfa0"
OPTIONS = {"host_name:myhost"}

URL = f"{RUNDECK_URL}/api/24/job/{JOB_ID}/run"
URL_PARAMS = {"project": PROJECT_NAME}
OPTIONS_LIST = list(OPTIONS)

# Trigger the job using cURL
response = requests.post(
    URL,
    params=URL_PARAMS,
    json=OPTIONS_LIST,
    headers={"Content-Type": "application/json"},
    auth=HTTPBasicAuth(RUNDECK_USERNAME, RUNDECK_PASSWORD),
)
print(response)

if response.status_code == 200:
    print("Job triggered successfully")
else:
    print(f"Failed to trigger job. Status code: {response.status_code}")

Can somebody point me where I am getting this wrong?

Thanks,
Rahul

rac...@rundeck.com

unread,
Aug 14, 2023, 1:22:54 PM8/14/23
to rundeck-discuss
Hi Rahul,

Do you see any clue by executing the job in debug mode? Is that python code running in an inline script step based job? What error are you seeing? Also, make sure to use the right python interpreter in the account that manage rundeck in your server.

Greetings.

rac...@rundeck.com

unread,
Aug 14, 2023, 1:25:35 PM8/14/23
to rundeck-discuss
Additionally, make sure that the Rundeck server / Rundeck account has permissions to access the remote server. 403 usually is an target service permissions error. So, try to discard any firewall/target service issue.

Regards.

Rahul Saxena

unread,
Aug 14, 2023, 10:59:05 PM8/14/23
to rundeck...@googlegroups.com
Just to confirm that if I trigger the same job from Rundeck UI (job ID that is used in the script), it works without any issue.
I am using the same Python version from Rundeck server machine but I am not sure what is causing 403.

On Rundeck UI, it is not inline script rather I am executing Python script using command option.

Do you have any working example if my use case?  

--
You received this message because you are subscribed to the Google Groups "rundeck-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rundeck-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rundeck-discuss/907ddbd3-4b57-4f15-8005-7828b7e873c4n%40googlegroups.com.

rac...@rundeck.com

unread,
Aug 16, 2023, 9:32:36 AM8/16/23
to rundeck-discuss

Hi Rahul,

Now I understand the context. Not sure if the HTTP Basic Auth library is the best method to authenticate. Anyway, I left an example that uses password method (more here).

import requests # host definition rdeck_instance = "localhost" rdeck_port = "4440" rdeck_api = "44" rdeck_username = "admin" rdeck_password = "admin" rdeck_job_id = "279fe1b1-07b7-4272-9512-6eae512bb9a1" # connect to instance s = requests.Session() r = s.post("http://" + rdeck_instance + ":" + rdeck_port +"/j_security_check", data={"j_username": rdeck_username, "j_password": rdeck_password}) r.status_code r.url # print data (for debug) print ('######################') print (r.status_code) print (r.url) print ('######################') r = s.post("http://" + rdeck_instance + ":" + rdeck_port +"/api/" + rdeck_api + "/job/" + rdeck_job_id + "/run",headers = {'Accept': 'application/json'}) # json response print(r.json())

Tested on Rundeck 4.15.

Feel free to modify it if you like. I’m not a python expert but this works :-)

Hope it helps!

rac...@rundeck.com

unread,
Aug 16, 2023, 10:24:47 AM8/16/23
to rundeck-discuss

Here the version “with options”

import requests # host definition rdeck_instance = "localhost" rdeck_port = "4440" rdeck_api = "44" rdeck_username = "admin" rdeck_password = "admin" rdeck_job_id = "279fe1b1-07b7-4272-9512-6eae512bb9a1" rdeck_option_name = "opt1" rdeck_option_value = "world!!" # connect to instance s = requests.Session() r = s.post("http://" + rdeck_instance + ":" + rdeck_port +"/j_security_check", data={ "j_username": rdeck_username, "j_password": rdeck_password }) r.status_code r.url # print data (for debug) print ('######################') print (r.status_code) print (r.url) print ('######################') r = s.post("http://" + rdeck_instance + ":" + rdeck_port +"/api/" + rdeck_api + "/job/" + rdeck_job_id + "/run", data={ "argString": "-%s %s" %(rdeck_option_name, rdeck_option_value) }, headers = {'Accept': 'application/json'}) # json response print(r.json())

Also tested on Rundeck 4.15.

Regards.

Rahul Saxena

unread,
Aug 16, 2023, 11:57:12 AM8/16/23
to rundeck...@googlegroups.com
Thanks for the update.  I just have one more question before I try your code. Can I use the Rundeck server URL?  My Rundeck URL is: http://rundecktest.connect.com and the user gets authenticated via LDAP.

Should i use below or it has to be localhost with port 4440.  Since I have LDAP configured, admin/admin credentials will not work.


Thanks,
Rahul

rac...@rundeck.com

unread,
Aug 16, 2023, 12:05:57 PM8/16/23
to rundeck-discuss
Sure Rahul, you can change the protocol (https), url (saved on the variable), and the creds. Another good approach could be change the script to receive the user/password via python parameters.

Regards.

Reply all
Reply to author
Forward
0 new messages