简体   繁体   中英

How to pass a file to a Rundeck job with the Rundeck API?

I have a Rundeck job with a file option.

How to pass a file to this Rundeck job with the Rundeck HTTP API with curl ?

I know how to pass text to Rundeck text options.

But I don't find how to pass file to Rundeck file options.

Thanks.

David.

With RD CLI tool is easiest, just do:

rd run -j YourJobName -p YourProjectName -- -YourOptionName@ /path/to/yourfile.txt

Using API looks more complicated (two steps). Take a look .

PD: In the depth, RD CLI is a Rundeck API abstraction, internally does the same.

I analyzed how works rundeck-cli and wrote this snippet :

RD_URL = "https://xxxx"
RD_USER = 'XXX'
RD_PASSWORD = 'xxxx'
RD_JOBID = 'xxxx'

import json
import requests
import sys
import os.path

class Rundeck():

    def __init__(self, username, password):
        self.cookies = None
        self._login(username, password)

    def _login(self, username, password):
        r = requests.post('%s/j_security_check' % RD_URL,
                          data={'j_username': username,
                                'j_password': password
                               },
                          allow_redirects=False
                         )

        self.cookies = r.cookies

    def run(self, jobid, options=None):
        r = requests.post('%s/api/18/job/%s/run' % (RD_URL, jobid),
                          cookies=self.cookies,
                          headers={'Accept': 'application/json',
                                   'Content-Type': 'application/json'
                                  },
                          data=json.dumps({'options': options})
                         )
        if r.status_code != 200:
            raise Exception(r.text)

    def upload(self, jobid, option, f):
        fd = open(f, 'rb')
        f_name = os.path.basename(f)
        r = requests.post('%s/api/29/job/%s/input/file?optionName=%s&filename=%s' % (RD_URL,
                                                                                     jobid,
                                                                                     option,
                                                                                     f_name),
                          cookies=self.cookies,
                          headers={'Accept': 'application/json',
                                   'Content-Type': 'application/octet-stream'
                                  },
                          data=fd
                         )
        if r.status_code != 200:
            raise Exception(r.text)
        json_result = json.loads(r.text)
        return json_result['options'][option]


if __name__ == '__main__':
    rd = Rundeck(RD_USER, RD_PASSWORD)
    u = rd.upload(RD_JOBID, 'fichier', 'zz.txt')
    rd.run(RD_JOBID, options = {'fichier': u})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM