我正在尝试使用ansibles uri module和gitlab api进行gitlab项目。
最终目标是将JSON移动到模板中,并将来自多台机器的信息提交到gitlab项目中的不同文件。
但是现在我正在坚持使用uri模块。
我能够在ansible linux盒子上使用gitlab api和curl。
PAYLOAD=$(cat << 'JSON'
{
"branch": "master",
"commit_message": "some commit message",
"actions": [
{
"action": "create",
"file_path": "Leer/test",
"content": "some content"
}
]
}
JSON
)
curl --request POST --header "PRIVATE-TOKEN: xxxxxxxxxxxxxxxx" --header "Content-Type: application/json" --data "$PAYLOAD" http://gitlab/api/v4/projects/2/repository/commits
并将其转换为以下ansible角色git-commit:
- name: commit to gitlab
uri:
url: http://gitlab/api/v4/projects/2/repository/commits
method: POST
body_format: json
headers:
PRIVATE-TOKEN: "xxxxxxxxxxxxxxxx"
body: |
'{
"branch": "master",
"commit_message": "some ansible commit message",
"actions": [
{
"action": "create",
"file_path": "Leer/test2",
"content": "some new content"
}
]
}'
使用Playbook git.yml中的角色:
---
- hosts: 10.101.127.116
connection: local
gather_facts: no
become: true
roles:
- git-commit
并运行Playbook:
ansible-playbook -k git.yml
但我收到以下http错误400:
fatal: [10.101.127.116]: FAILED! =>
{
"changed": false,
"connection": "close",
"content": "",
"content_length": "0",
"content_type": "text/html; charset=utf-8", "date":
"Thu, 18 Apr 2019 13:11:13 GMT",
"msg": "Status code was 400 and not [200]: HTTP Error 400: Bad Request", "redirected": false,
"server": "nginx",
"status": 400,
"url": "http://gitlab/api/v4/projects/2/repository/commits",
"x_request_id": "WNfjPOBkVk5", "x_runtime": "0.002530"
}
我感谢任何帮助
您错误地使用YAML引用:
body: |
'{
意味着body
字面上是单引号字符,然后是一个开括号,因为yaml |
字符本身就是一个引用结构。
您可以删除两个单引号(单引号字符在JSON中不合法),或者您可以删除管道然后将单引号移动到body:
级别
您可能会发现使用YAML到JSON转换工具有助于查看文档的外观;有几个在线和remarshal在您的计算机上运行