【问题标题】:Trying to Pass Dynamic Value to Rundeck Server尝试将动态值传递给 Rundeck 服务器
【发布时间】:2020-08-11 07:25:26
【问题描述】:

rundeck 中的变量(servicename), runningservices(sshd) 我想在这里传递给 rundeck 服务器。

import requests

runningservice=sshd

headers = {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    'X-Rundeck-Auth-Token': 'API',
}
data = '{"argString":"-servicename runningservice "}'
response = requests.post('http://IP:PORT/api/16/job/JOBID/executions', headers=headers, data=data)

【问题讨论】:

    标签: python linux variables curl rundeck


    【解决方案1】:

    您可以通过以下方式尝试:

    具有三个选项的工作定义:

    <joblist>
      <job>
        <context>
          <options preserveOrder='true'>
            <option name='opt1' value='hello' />
            <option name='opt2' value='you' />
            <option name='opt3' value='all' />
          </options>
        </context>
        <defaultTab>nodes</defaultTab>
        <description></description>
        <executionEnabled>true</executionEnabled>
        <id>062cbd2c-76b6-488b-874e-45fe9a6ea6d0</id>
        <loglevel>INFO</loglevel>
        <name>HelloWorld</name>
        <nodeFilterEditable>false</nodeFilterEditable>
        <plugins />
        <scheduleEnabled>true</scheduleEnabled>
        <sequence keepgoing='false' strategy='node-first'>
          <command>
            <exec>echo "${option.opt1} ${option.opt2} ${option.opt3}"</exec>
          </command>
        </sequence>
        <uuid>062cbd2c-76b6-488b-874e-45fe9a6ea6d0</uuid>
      </job>
    </joblist>
    

    Python代码(检查数据部分):

    import requests
    import pprint
    
    # host definition
    rdeck_instance = "localhost"
    rdeck_port = "4440"
    rdeck_api = "35"
    rdeck_token = "aHbbJ98lhp9g6xU5HJ9T6qpgphPK19r3"
    rdeck_action = "executions"
    jobid = "062cbd2c-76b6-488b-874e-45fe9a6ea6d0"
    
    # variable strings (for testing, in this case, you can put from the result from any source: another API call, fuzzy table output, etc.)
    string1="one"
    string2="two"
    string3="three"
    
    s = requests.Session()
    r = s.post("http://" + rdeck_instance +  ":" + rdeck_port +"/api/" + rdeck_api + "/job/" + jobid + "/" + rdeck_action + "?authtoken=" + rdeck_token, 
    headers = {"Accept" : "application/json"}, 
    data = { "argString" : "-opt1 {} -opt2 {} -opt3 {}".format(string1, string2, string3)})
    
    # json output
    pprint.pprint(r.json())
    

    结果:

    {'argstring': '-opt1 one -opt2 two -opt3 three',
     'date-started': {'date': '2020-08-11T13:45:23Z', 'unixtime': 1597153523995},
     'description': 'echo "${option.opt1} ${option.opt2} ${option.opt3}"',
     'executionType': 'user',
     'href': 'http://localhost:4440/api/35/execution/16',
     'id': 16,
     'job': {'averageDuration': 406,
             'description': '',
             'group': '',
             'href': 'http://localhost:4440/api/35/job/062cbd2c-76b6-488b-874e-45fe9a6ea6d0',
             'id': '062cbd2c-76b6-488b-874e-45fe9a6ea6d0',
             'name': 'HelloWorld',
             'options': {'opt1': 'one', 'opt2': 'two', 'opt3': 'three'},
             'permalink': 'http://localhost:4440/project/ProjectEXAMPLE/job/show/062cbd2c-76b6-488b-874e-45fe9a6ea6d0',
             'project': 'ProjectEXAMPLE'},
     'permalink': 'http://localhost:4440/project/ProjectEXAMPLE/execution/show/16',
     'project': 'ProjectEXAMPLE',
     'status': 'running',
     'user': 'admin'}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-12
      • 2015-02-28
      • 1970-01-01
      • 2017-10-31
      • 2022-01-14
      • 2016-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多