将cURL命令转换为ansible

里克·桑德森

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

谁能发现我在做什么错?

幼虫

我们可以使用https://httpbin.org之类的调试终结点来诊断此问题,您可以使用该终结点来确切查看您的请求正在发送什么数据。

使用以下剧本,我们首先通过模块发出请求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

您可以手动检查结果,但是如果我们结合起来jqdiff强调不同之处,可能会更容易

$ 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时,invokeContext已经转换为JSON字符串数据结构将进行两次转换。你要这个:

invokeContext: "{{ invocation_context }}"

您还存在一些布尔值与字符串冲突。

使用时curlpersisted属性设置为boolean 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命令,我们将看到模块发送的数据curluri模块发送的数据是相同的:

$ diff -u <(jq -Sr .data output-curl1.json | jq -S .) <(jq -Sr .data output-task1.json | jq -S .)
$ # no output from the previous command

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章