11-AWX API

1.认证相关

1.创建token

[root@ansibleAWX001 root]# curl -ku admin:password -H "Content-Type: application/json" -X POST -d '{"description":"Tower CLI", "application":null, "scope":"write"}' http://192.x.x.x/api/v2/users/1/personal_tokens/ | python -m json.tool
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   558  100   494  100    64   1761    228 --:--:-- --:--:-- --:--:--  1758
{
    "application": null,
    "created": "2020-06-02T06:10:20.869744Z",
    "description": "Tower CLI",
    "expires": "3019-10-04T06:10:20.866403Z",
    "id": 1,
    "modified": "2020-06-02T06:10:20.882142Z",
    "refresh_token": null,
    "related": {
        "activity_stream": "/api/v2/tokens/1/activity_stream/",
        "user": "/api/v2/users/1/"
    },
    "scope": "write",
    "summary_fields": {
        "user": {
            "first_name": "",
            "id": 1,
            "last_name": "",
            "username": "admin"
        }
    },
    "token": "E8Cmcj1TmKWXQ3IBQ97Ozyq555evo2",   # 10.92.128.231 AWX测试环境的token
    "type": "o_auth2_access_token",
    "url": "/api/v2/tokens/1/",
    "user": 1
}
[root@ansibleAWX001 root]#

参考:https://github.com/ansible/awx/blob/devel/docs/auth/oauth.md

2.项目

2.1 创建项目

import requests
  
url = "http://localhost/api/v2/projects/"
payload = '{
    //允许在使用此项目的作业模板中更改配置管理分支或修订,默认为False
    "allow_override": true,
    //项目描述信息
    "description": "this is a test project",
    //机构id,管理员会提前创建好
    "organization": 1,
    //项目名称
    "name": "projectName"
}'
headers = {'Authorization': "Bearer xxxxxxxxxx"}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)

备注
a.新创建的项目,需要管理员给使用的用户授权后,才可以对此项目进行操作。

2.2 删除项目

method:DELETE 
url = "http://localhost/api/v2/projects/13/"

2.3 修改项目

import requests
  
url = "http://localhost/api/v2/projects/13/"
payload = '{ 
   //设置项目关联的PLAYBOOK目录 
   "local_path": "servicePool"
}' 
headers = {'Authorization': "Bearer xxxxxxxxxx"} 
response = requests.request("PATCH", url, data=payload, headers=headers) 
print(response.text) 

2.4 查看项目

method:GET 
url = "http://localhost/api/v2/projects/"

 

3.清单

3.1清单

3.1.1创建清单

import requests 
url = "http://localhost/api/v2/inventories/"
payload = '{ 
   "name": "servicePoolInventory", 
   "description": "this is a test inventory"
   "organization": 1
}' 
headers = {'Authorization': "Bearer xxxxxxxxxx"} 
response = requests.request("POST", url, data=payload, headers=headers) 
print(response.text)

备注
a.新创建的项目,需要管理员给使用的用户授权后,才可以对此项目进行操作。 

3.1.2删除清单
method:DELETE 
http://localhost/api/v2/inventories/3/

3.1.3修改清单

method:PATCH 
http://localhost/api/v2/inventories/3/

3.1.4查看清单

method:GET
http://localhost/api/v2/inventories/

3.1.5搜索清单

method: GET
//搜索name是servicePoolInventory(不区分大小写)的清单,正常情况下返回1个,如果返回数量大于1,需要人工确认处理。 
http://localhost/api/v2/inventories?search=servicePoolInventory

  

3.2Group

3.2.1创建Group

import requests
url = "http://localhost/api/v2/inventories/3/groups/"
payload = '{
    "description": "Hello world",
    "name": "servicePoolGroup1"
}'
headers = {'Authorization': "Bearer xxxxxxxxxx"}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)

3.2.2搜索group

method: GET
//搜索name是servicePoolScaleUp(不区分大小写)的清单,正常情况下返回1个,如果返回数量大于1,需要人工确认处理。 
http://localhost/api/v2/inventories/3/groupsearch=servicePoolScaleUp

  

3.3Host

3.3.1在某个Inventory下创建Host

import requests
url = "http://localhost/api/v2/inventories/3/hosts/"
payload = '{
    "description": "Centos 6",
    "name": "192.168.56.103"
}'
headers = {'Authorization': "Bearer xxxxxxxxxx"}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)

备注 a.也可以直接使用2.3.3接口

3.3.2删除Host

method: DELETE 
url = "http://localhost/api/v2/hosts/6/"

3.3.3添加Host到某个Group

import requests
url = "http://localhost/api/v2/groups/4/hosts/"
payload = '{
    "name": "192.168.56.103",
    "description": "Centos 6",
    //指定清单ID
    "inventory": 3
}'
headers = {'Authorization': "Bearer xxxxxxxxxx"}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)

备注:

a.若主机不存在,会新建。

3.3.4Host从某个Group解绑

import requests
url = "http://localhost/api/v2/groups/5/hosts/"
payload = '{
    "id":11,
    "disassociate": true
}'
headers = {'Authorization': "Bearer xxxxxxxxxx"}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)  
备注:
a.解绑后主机还存在

3.3.5查看Host

method: GET 
url = "http://localhost/api/v2/hosts/"

 

4.凭证

凭证事先创建好,不需要通过接口创建,在模板中直接引用即可

5.模板

5.1.获取模板名对应的id

import requests
url = "http://10.92.0.3:8080/api/v2/job_templates/"
headers = {
 'Authorization': 'Bearer xxxxxxx',
 'Content-Type': 'application/json'
}
response = requests.request("GET", url, headers=headers)
print(response.text.encode('utf8'))

5.1.1 根据模板名称查询模板id(通过url传参数)

import requests

url = "http://10.92.0.3:8080/api/v2/job_templates?name=taobaoIMBusiness"

payload = "{\n  \"extra_vars\": {\n    \"hostGroup\": \"all\",\n    \"job_tags\": \"GenerateFilebeatConfigFile\"\n  }\n}"
headers = {
  'Authorization': 'Bearer HgehKIYboobXD1rFccXDbI6wZSxHIg',
  'Content-Type': 'application/json'
}

response = requests.request("GET", url, headers=headers, data = payload)

print(response.text.encode('utf8'))

  

5.2.更新模板的机器选择限制条件

import requests
  
url = "http://localhost/api/v2/job_templates/9"
//传入Group的名字 
payload = '{"limit": "serviceCommon"}"
headers = {'Authorization': "Bearer xxxxxxxxxx"}
response = requests.request("PATCH", url, data=payload, headers=headers)
print(response.text)

5.3.启动任务

import requests

url = "http://192.x.x.x/api/v2/job_templates/21/launch/"

payload = "{\n  \"extra_vars\": {\n    \"hostGroup\": [\"Common\"]\n  }\n}"
headers = {
  'Authorization': 'Bearer E8Cmcj1TmKWXQ3IBQ97Ozyq555evo2',
  'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data = payload)

print(response.text.encode('utf8'))

传入job_tags启动模板

import requests
url = "http://10.92.0.3:8080/api/v2/job_templates/21/launch/"
payload = "{\n \"extra_vars\": {\n \"hostGroup\": \"all\",\n \"job_tags\": \"GenerateFilebeatConfigFile\"\n }\n}"
headers = {
 'Authorization': 'Bearer xxxxxxxx',
 'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data = payload)
print(response.text.encode('utf8'))

参考:https://docs.ansible.com/ansible-tower/3.6.4/html_zh/towerapi/api_ref.html#/Job_Templates/Job_Templates_job_templates_launch_create

  

posted @ 2020-06-02 16:12  番茄土豆西红柿  阅读(123)  评论(0)    收藏  举报
TOP