Search code examples
rundeck

Best Way to Execute a Python Script With Rundeck


Good day all,

I am trying to figure out how to execute a python script using Rundeck.

Currently I have setup a node step that executes the following command

python3 /home/ansible/reboot_cctv.py

The permissions for the python script are

-rwxrwxrwx 1 ansible ansible 455 Feb 24 16:14 reboot_cctv.py

I have it set to execute locally, but am getting the following error when i run the job

python3: can't open file '/home/ansible/reboot_cctv.py': [Errno 13] Permission denied
Result: 2
Failed: NonZeroResultCode: Result code was 2

This is the full job definition

- defaultTab: nodes
  description: ''
  executionEnabled: true
  id: fb24247a-0e30-4533-ad00-01ed4cbc00ff
  loglevel: INFO
  name: CCTV Restart Python
  nodeFilterEditable: false
  plugins:
    ExecutionLifecycle: null
  scheduleEnabled: true
  sequence:
    commands:
    - exec: python3 /home/ansible/reboot_cctv.py
    keepgoing: false
    strategy: node-first
  uuid: fb24247a-0e30-4533-ad00-01ed4cbc00ff

I am not sure what other modifications I need to make in order for this to work.

Any input or advice is grately appreciated


Solution

  • The rundeck user needs access to the python script file.

    You have two options.

    The first option, running an inline python script (defined within the rundeck job)

    1. Create a new job.
    2. Add a "script" step.
    3. Paste the python script on the "Enter the entire script to execute" box.
    4. Click on the "advanced" link and then define the full python interpreter path on the "Invocation String" textbox (e.g: /usr/bin/python3) and the .py extension on the "File Extension" textbox.
    5. Save your job and run.

    The second option is to call a script stored in the Rundeck server filesystem (closer to your approach). This option and your approach need that the rundeck user (created at the moment of installing Rundeck via RPM/DEB) can reach the path of your python script.

    1. Make sure that the python script is reachable by the rundeck user (considering that your Rundeck instance is an RPM or DEB based installation).
    2. Create a new job.
    3. Add a "Script file or URL" step, and set the python script path on the "File Path/URL" textbox.
    4. Similar to the first option, click on the "advanced" link and then define the full python interpreter path on the "Invocation String" textbox (e.g: /usr/bin/python3) and the .py extension on the "File Extension" textbox.
    5. Save your job and run.