【问题标题】:ansible integer type variable doesn't work but direct number doesansible 整数类型变量不起作用,但直接数字起作用
【发布时间】:2021-09-28 11:07:46
【问题描述】:

有一个使用 uri moduleportainer API 的 ansible 任务,正文如下:

  ansible.builtin.uri:
    body_format: json
    headers:
      accept: application/json
      Content-Type: multipart/form-data
      Authorization: "Bearer {{ portainer_auth['json']['jwt'] }}"
    body:
      Name: "{{ item['Name'] }}"
      EndpointCreationType: "{{ item['EndpointCreationType'] }}"
      URL: "{{ item['URL'] | default(omit, true) | string }}"
      PublicURL: "{{ item['PublicURL'] | default(omit, true) }}"
      GroupID: "{{ item['GroupID'] }}"

抛出错误:

"json": "cannot unmarshal string into Go struct field endpointUpdatePayload.GroupID of type int", 
"message": "Invalid request payload"

但是这行得通,所以问题似乎出在 ansible 变量中

  ansible.builtin.uri:
    body_format: json
    headers:
      accept: application/json
      Content-Type: multipart/form-data
      Authorization: "Bearer {{ portainer_auth['json']['jwt'] }}"
    body:
      Name: "{{ item['Name'] }}"
      EndpointCreationType: "{{ item['EndpointCreationType'] }}"
      URL: "{{ item['URL'] | default(omit, true) | string }}"
      PublicURL: "{{ item['PublicURL'] | default(omit, true) }}"
      GroupID: 2

body_format: form-multipart 则错误为

"json": "details": "invalid character '-' in numeric literal", 
"message": "Invalid request payload"

有什么想法吗?

【问题讨论】:

  • "{{ item['GroupID'] | int }}"

标签: api ansible uri portainer


【解决方案1】:

我遇到了同样的问题。

看来你的格式化方法是错误的。
你也忘记了 URI 的方法。

试试这个:


  ansible.builtin.uri:
    url: <portainer_server_url>:<portainer_port>/api/endpoints # default port 9000
    method: POST
    return_content: yes
    body_format: form-multipart
    headers:
      accept: application/json
      Content-Type: multipart/form-data
      Authorization: "Bearer {{ {{ (auth_token.content|from_json).jwt }}"
    body:
      Name: "{{ item.Name }}"
      EndpointCreationType: "{{ item.EndpointCreationType }}"
      URL: "{{ item.URL | string }}"
      PublicURL: "{{ item.PublicURL | default(omit, true) }}"
      GroupID: 2
   with_items:
    - "{{ hostvars[inventory_hostname].endpoints }}"

使用这样的库存结构(yaml):

all:
  hosts:
    node01:
      endpoints:
        - Name: local
          EndpointCreationType: 1
          URL: "unix:///var/run/docker.sock"
          PublicURL: <node1_IP|FQDN>
        - Name: node02
          EndpointCreationType: 1
          URL: "tcp://<node02_IP|FQDN:<docker_API_port>" default port= 2375
          PublicURL: <node02_IP|FQDN>

它对我来说很好;-)

【讨论】:

    猜你喜欢
    • 2022-06-13
    • 1970-01-01
    • 2015-12-07
    • 2021-12-02
    • 2014-03-25
    • 1970-01-01
    • 1970-01-01
    • 2015-03-08
    • 2017-08-12
    相关资源
    最近更新 更多