将cURL命令转换为ansible

将cURL命令转换为ansible,curl,ansible,devops,Curl,Ansible,Devops,我有两个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,"co

我有两个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

有人能发现我做错了什么吗?

我们可以使用调试端点(如)诊断此问题,您可以使用该端点查看请求发送的确切数据

使用以下剧本,我们首先使用
curl
发出请求,然后使用
uri
模块发出请求,在每种情况下都存储来自
httpbin
的响应,以便我们可以比较它们:

---
- 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"
 }
但是当使用
uri
模块时,
invokeContext
的值是一个字符串:

$ jq -r .data output-task1.json | jq .invokeContext
"{\"action\": \"someAction\", \"source\": \"path/to/resource\"}"
之所以发生这种情况,是因为您正在通过
到\u json
过滤器传递
调用\u上下文
变量的值:

invokeContext: "{{ invocation_context | to_json }}"
这意味着当任务中的
主体
数据结构被序列化为JSON时,
invokeContext
(已转换为JSON字符串)将被双重转换。你想要这个:

invokeContext: "{{ invocation_context }}"
您还存在一些布尔值与字符串冲突

使用
curl
时,您将
持久化属性设置为布尔
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

$diff-u playbook以什么方式不工作?它在ansible中运行,但端点没有被正确调用,结果是它没有在服务器上正确运行命令。非常感谢。它还没完全起作用。这可能与-v-ik标志有关吗?我不确定他们做什么。
$ diff -u <(jq -Sr .data output-curl1.json | jq -S .) <(jq -Sr .data output-task1.json | jq -S .)
$ # no output from the previous command