我有两个cURL命令,试图将其转换为Ansible。他们看起来像这样:
curl -v -ik --header "Content-Type: application/json" --header "X-Application-Username: my_username" --header "X-Application-Password: my_password" --request PUT --data '{"enabled":"False","persisted" : true,"concurrentExecution" : false, "type" : "cron","schedule" : "0/10 * * * * ?","invokeService" : "provisioner","invokeContext" : { "action" : "someAction","source" : "path/to/resource"}}' "https://localhost:8443/path/to/url/that/I/need"
curl -v -ik --header "Content-Type: application/json" --header "X-Application-Username: my_username" --header "X-Application-Password: my_password" --request PUT --data '{"enabled":"True","persisted" : true,"concurrentExecution" : false, "type" : "cron","schedule" : "0/10 * * * * ?","invokeService" : "provisioner","invokeContext" : { "action" : "someAction","source" : "path/to/resource"}}' "https://localhost:8443/path/to/url/that/I/need"
我已尝试转换为ansible,但无法正常工作。到目前为止,我想出的是:
- name: Run cURL commands
hosts: role_app_server[1]
vars:
endpoint: "https://localhost:8443/path/to/url/that/I/need"
cron_schedule: "0/10 * * * * ?"
invocation_context:
action: "someAction"
source: "path/to/resource"
tasks:
- name: First task
uri:
url: "{{ endpoint }}"
headers:
Content-Type: "application/json"
X-Application-Username: "my_username"
X-Application-Password: "my_password"
method: PUT
body:
enabled: "False"
persisted: "true"
concurrentExecution: "false"
type: "cron"
schedule: "{{ cron_schedule }}"
invokeService: "provisioner"
invokeContext: "{{ invocation_context | to_json }}"
body_format: json
validate_certs: no
- name: 2nd task
uri:
url: "{{ endpoint }}"
headers:
Content-Type: "application/json"
X-Application-Username: "my_username"
X-Application-Password: "my_password"
method: PUT
body:
enabled: "True"
persisted: "true"
concurrentExecution: "false"
type: "cron"
schedule: "{{ cron_schedule }}"
invokeService: "provisioner"
invokeContext: "{{ invocation_context | to_json }}"
validate_certs: no
body_format: json
有人可以发现我在做什么吗?
答案 0 :(得分:2)
我们可以使用https://httpbin.org之类的调试终结点来诊断此问题,您可以使用它来准确查看请求发送的数据。
使用以下剧本,我们首先使用curl
发出请求,然后使用uri
模块发出请求,每种情况下都存储来自httpbin
的响应,以便我们可以进行比较:< / p>
---
- name: Run cURL commands
hosts: localhost
gather_facts: false
vars:
endpoint: "https://httpbin.org/put"
cron_schedule: "0/10 * * * * ?"
invocation_context:
action: "someAction"
source: "path/to/resource"
tasks:
- name: First task (curl)
command: >-
curl -k -o output-curl1.json
--header "Content-Type: application/json"
--header "X-Application-Username: my_username"
--header "X-Application-Password: my_password"
--request PUT
--data '{"enabled":"False","persisted" : true,"concurrentExecution" : false, "type" : "cron","schedule" : "0/10 * * * * ?","invokeService" : "provisioner","invokeContext" : { "action" : "someAction","source" : "path/to/resource"}}'
"{{ endpoint }}"
- name: First task (uri)
uri:
url: "{{ endpoint }}"
headers:
Content-Type: "application/json"
X-Application-Username: "my_username"
X-Application-Password: "my_password"
method: PUT
body:
enabled: "False"
persisted: "true"
concurrentExecution: "false"
type: "cron"
schedule: "{{ cron_schedule }}"
invokeService: "provisioner"
invokeContext: "{{ invocation_context | to_json }}"
body_format: json
validate_certs: no
return_content: true
register: output1
- copy:
content: "{{ output1.content }}"
dest: ./output-task1.json
您可以手动检查结果,但是如果我们结合使用jq
和diff
来突出显示差异,则可能会更容易:
$ diff -u <(jq -Sr .data output-curl1.json | jq -S .) <(jq -Sr .data output-task1.json | jq -S .)
--- /dev/fd/63 2019-04-07 18:00:46.597729568 -0400
+++ /dev/fd/62 2019-04-07 18:00:46.599729606 -0400
@@ -1,12 +1,9 @@
{
- "concurrentExecution": false,
+ "concurrentExecution": "false",
"enabled": "False",
- "invokeContext": {
- "action": "someAction",
- "source": "path/to/resource"
- },
+ "invokeContext": "{\"action\": \"someAction\", \"source\": \"path/to/resource\"}",
"invokeService": "provisioner",
- "persisted": true,
+ "persisted": "true",
"schedule": "0/10 * * * * ?",
"type": "cron"
}
这突出了一些区别。最大的似乎是invokeContext
属性的内容。使用curl
时,该属性的值为JSON对象:
$ jq -r .data output-curl1.json | jq .invokeContext
{
"action": "someAction",
"source": "path/to/resource"
}
但是在使用uri
模块时,invokeContext
的值是一个字符串:
$ jq -r .data output-task1.json | jq .invokeContext
"{\"action\": \"someAction\", \"source\": \"path/to/resource\"}"
之所以发生这种情况,是因为您正在通过invocation_context
过滤器传递to_json
变量的值:
invokeContext: "{{ invocation_context | to_json }}"
这意味着当您任务中的body
数据结构序列化为JSON时,您已经将已经转换为JSON字符串的invokeContext
-转换。您想要这个:
invokeContext: "{{ invocation_context }}"
您还存在一些布尔值与字符串冲突。
使用curl
时,您将persisted
属性设置为布尔值true
,但是在Ansible任务中,您将其设置为字符串值"true"
。代替:
persisted: "true"
您要
persisted: true
最后,您在concurrentExecution
属性上遇到了相同的问题,该属性应该是:
concurrentExecution: false
所有这些更改之后,第一个任务变为:
- name: First task (uri)
uri:
url: "{{ endpoint }}"
headers:
Content-Type: "application/json"
X-Application-Username: "my_username"
X-Application-Password: "my_password"
method: PUT
body:
enabled: "False"
persisted: true
concurrentExecution: false
type: "cron"
schedule: "{{ cron_schedule }}"
invokeService: "provisioner"
invokeContext: "{{ invocation_context }}"
body_format: json
validate_certs: no
return_content: true
register: output1
如果我们重复前面的diff
命令,我们将看到curl
和uri
模块发送的数据是相同的:
$ diff -u <(jq -Sr .data output-curl1.json | jq -S .) <(jq -Sr .data output-task1.json | jq -S .)
$ # no output from the previous command