这篇文章介绍一下如何在Rundeck中使用Rundeck Rest Api,并结合具体的实例进行操作。
Rundeck提供了较好的对外暴露的WebApi,可以进行更方便地集成而不需要cli。而诸如curl则基本上是所有linux的标配,另外程序之中也可以直接使用Api,rundeck的api相关信息可参看如下:
API支持XML和JSON方式,虽然一些export/import的功能也支持YAML或者文本格式(text/plain),整体在所有API级别上还是使用XML和JSON方式。而在version 14,全部支持JSON格式,而只有一个例外(/api/V/project/[PROJECT]/jobs/export)。
Rundeck对api的使用可以通过用户名和密码或者 token的方式来进行。
首先需要需要向如下URL进行用户名和密码的post操作
这种方式首先需要生成token,使用生成的token,可以使用如下两种方式来进行使用。
首先需要设定token,可以通过GUI先行生成相关token,登录之后在右上角的user profile的界面进行token的添加
根据如下信息创建token
可以看到如下token的信息
liumiaocn:~ liumiao$ curl http://localhost:32044/api/30/projects?authtoken=vcopxmhraTC9vlNw1p5VJTlxNhQcVNbc
<result success='true' apiversion='30'>
<projects count='2'>
<project>
<name>rundeck-test-cli-projectname>
<description>description>
project>
<project>
<name>rundeck-test-projectname>
<description>This project is to introduce Rundeck functiondescription>
project>
projects>
result>liumiaocn:~ liumiao$
liumiaocn:~ liumiao$ curl http://localhost:32044/api/1/projects?authtoken=vcopxmhraTC9vlNw1p5VJTlxNhQcVNbc -H "Accept: application/json" |jq .
...省略
[
{
"url": "http://localhost:32044/api/30/project/rundeck-test-cli-project",
"name": "rundeck-test-cli-project",
"description": ""
},
{
"url": "http://localhost:32044/api/30/project/rundeck-test-project",
"name": "rundeck-test-project",
"description": "This project is to introduce Rundeck function"
}
]
liumiaocn:~ liumiao$
准备如下Json格式的project创建信息
liumiaocn:~ liumiao$ cat rundeck-project.json
{
"name": "rundeck-test-webapi-project",
"config":
{
"project.lable":"Rundeck Test WebApi Project"
}
}
liumiaocn:~ liumiao$
liumiaocn:~ liumiao$ curl -H "Content-Type: application/json" -X POST --data-binary "@rundeck-project.json" http://localhost:32044/api/30/projects?authtoken=vcopxmhraTC9vlNw1p5VJTlxNhQcVNbc
{"url":"http://localhost:32044/api/30/project/rundeck-test-webapi-project","name":"rundeck-test-webapi-project","description":"","config":{"service.FileCopier.default.provider":"jsch-scp","project.ssh-keypath":"/home/rundeck/.ssh/id_rsa","service.NodeExecutor.default.provider":"jsch-ssh","project.lable":"Rundeck Test WebApi Project","project.name":"rundeck-test-webapi-project","project.ssh-authentication":"privateKey","resources.source.1.type":"local"}}liumiaocn:~ liumiao$
liumiaocn:~ liumiao$
liumiaocn:~ liumiao$ curl http://localhost:32044/api/30/projects?authtoken=vcopxmhraTC9vlNw1p5VJTlxNhQcVNbc
<projects count='3'>
<project url='http://localhost:32044/api/30/project/rundeck-test-cli-project'>
<name>rundeck-test-cli-projectname>
<description>description>
project>
<project url='http://localhost:32044/api/30/project/rundeck-test-project'>
<name>rundeck-test-projectname>
<description>This project is to introduce Rundeck functiondescription>
<label>Rundeck Test Projectlabel>
project>
<project url='http://localhost:32044/api/30/project/rundeck-test-webapi-project'>
<name>rundeck-test-webapi-projectname>
<description>description>
project>
projects>liumiaocn:~ liumiao$
确认创建的Project详细信息
liumiaocn:~ liumiao$ curl http://localhost:32044/api/30/project/rundeck-test-webapi-project?authtoken=vcopxmhraTC9vlNw1p5VJTlxNhQcVNbc -H "Accept: application/json" |jq .
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 457 0 457 0 0 11146 0 --:--:-- --:--:-- --:--:-- 11425
{
"url": "http://localhost:32044/api/30/project/rundeck-test-webapi-project",
"name": "rundeck-test-webapi-project",
"description": "",
"config": {
"service.FileCopier.default.provider": "jsch-scp",
"project.ssh-keypath": "/home/rundeck/.ssh/id_rsa",
"service.NodeExecutor.default.provider": "jsch-ssh",
"project.lable": "Rundeck Test WebApi Project",
"project.name": "rundeck-test-webapi-project",
"project.ssh-authentication": "privateKey",
"resources.source.1.type": "local"
}
}
liumiaocn:~ liumiao$
https://www.rundeck.com/open-source