要将GitHub存储库与AWX同步,您需要使用GitHub凭证。以下是使用AWX API创建GitHub凭证的示例代码:
import requests
# AWX API相关信息
awx_api_url = 'http://awx.example.com/api/v2'
awx_username = 'admin'
awx_password = 'password'
# GitHub凭证信息
github_token = 'your_github_token'
github_organization = 'your_github_organization'
github_repository = 'your_github_repository'
# 创建GitHub凭证
def create_github_credential():
# 获取AWX认证token
auth = (awx_username, awx_password)
response = requests.post(f'{awx_api_url}/authtoken/', auth=auth)
token = response.json()['token']
# 创建GitHub凭证
headers = {
'Authorization': f'Token {token}',
'Content-Type': 'application/json'
}
data = {
'name': 'GitHub Credential',
'description': 'Credential for GitHub',
'organization': github_organization,
'credential_type': 42, # GitHub凭证类型为42
'inputs': {
'token': github_token,
'organization': github_organization,
'repository': github_repository
}
}
response = requests.post(f'{awx_api_url}/credentials/', headers=headers, json=data)
response.raise_for_status()
print('GitHub Credential created successfully.')
# 创建GitHub凭证
create_github_credential()
请确保将示例代码中的awx.example.com
替换为您的AWX API URL,并将admin
和password
替换为AWX管理员的用户名和密码。另外,将your_github_token
、your_github_organization
和your_github_repository
替换为您的GitHub凭证信息。
运行上述代码后,它将通过AWX API创建一个名为"GitHub Credential"的GitHub凭证,并与指定的GitHub存储库关联。