Skip to content

List Rundeck Projects

September 6th, 2018 - ITSoftwareTutorial(1 min)

The other day I found out that Rundeck has an API which allows it to be controlled remotely by scripts. I’ve decided to write a script to list all the projects defined in a rundeck instance.

Prerequisites

Before you start, you need:

  • The instance’s URL (server and port)
  • An api key for the user
  • Make sure the user has enough credentials to list the projects

The code

I’ve picked up Python as language for the task just because I’m more familiar with it in this context. Therefore, another dependency is Python 3 and something like virtualenv to allow installation of 3rd party packages.

The code is quite trivial:

import requests
def listProjects(server, port, api_key):
    url =  server +':'+port+'/api/1/projects'
    headers = {'Content-Type': 'application/json',
        'X-RunDeck-Auth-Token': api_key }
    r = requests.get(url, headers=headers, verify=False)
    return r.text.encode('utf-8')

This function will return an XML containing all the projects (or an error if the user is not allowed to list projects via API).

If you need to get the names only, you can process the XML:

import xml.etree.ElementTree as ET
def getProjectNames(projectsinfo_xml):
    project_names = []
    root = ET.fromstring(projectsinfo_xml)
    for projects in root:
        for name in projects.findall('project'):
            project_names.append(name.find('name').text)
    log.info(project_names)
    return project_names

Now, all you need to do is:

projects = getProjectNames(
    listProjects(host, port, api_key)
)

Simple.

HTH,

Share on
Reddit
Linked in
Whatsapp

A little experiment: