请求正文为 int 而不是 string

发布于 2025-01-12 04:09:47 字数 1222 浏览 0 评论 0原文

我正在尝试将注册值(来自 csv)传递到正文中。然而,每当尝试这样做时,它都会返回一个字符串 - 但 API 期望一个 int。即使尝试将其转换为 int 时,它仍然会产生字符串。

- name: Creating vlan
    cisco.intersight.intersight_rest_api:
      resource_path: /fabric/Vlans
      update_method: post
      api_body: {
    "AutoAllowOnUplinks": true,
    "EthNetworkPolicy": "{{ ethpolicies.api_response.Moid }}",
    "IsNative": false,
    "MulticastPolicy": "{{ multicastpolicies.api_response.Moid }}",
    "Name": "{{ item.Name }}",
    "VlanId": "{{ item.VLAN | int}}"
  }
    register: vlancreate
    with_items:
      - "{{ vlanlist.list }}"

预期结果

"invocation": {
    "module_args": {
        "api_body": {
            "AutoAllowOnUplinks": true,
            "EthNetworkPolicy": "...",
            "IsNative": false,
            "MulticastPolicy": "...",
            "Name": "test2",
            "VlanId": 100 <- int

实际结果

"invocation": {
    "module_args": {
        "api_body": {
            "AutoAllowOnUplinks": true,
            "EthNetworkPolicy": "...",
            "IsNative": false,
            "MulticastPolicy": "...",
            "Name": "test2",
            "VlanId": "100" <- string

I'm trying to pass a registered value (from a csv) into a body. However, whenever attempting to do so it want to return a string - but API expect an int. Even when attmpting to convert it to an int, it still results in string.

- name: Creating vlan
    cisco.intersight.intersight_rest_api:
      resource_path: /fabric/Vlans
      update_method: post
      api_body: {
    "AutoAllowOnUplinks": true,
    "EthNetworkPolicy": "{{ ethpolicies.api_response.Moid }}",
    "IsNative": false,
    "MulticastPolicy": "{{ multicastpolicies.api_response.Moid }}",
    "Name": "{{ item.Name }}",
    "VlanId": "{{ item.VLAN | int}}"
  }
    register: vlancreate
    with_items:
      - "{{ vlanlist.list }}"

Expected result

"invocation": {
    "module_args": {
        "api_body": {
            "AutoAllowOnUplinks": true,
            "EthNetworkPolicy": "...",
            "IsNative": false,
            "MulticastPolicy": "...",
            "Name": "test2",
            "VlanId": 100 <- int

Actual result

"invocation": {
    "module_args": {
        "api_body": {
            "AutoAllowOnUplinks": true,
            "EthNetworkPolicy": "...",
            "IsNative": false,
            "MulticastPolicy": "...",
            "Name": "test2",
            "VlanId": "100" <- string

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

复古式 2025-01-19 04:09:47

动态创建字典,从而避免对字符串进行 Jinja 求值。例如,创建变量api_body_default(下面简化用于测试)并将其与列表中的项目组合。给定列表,

    vlanlist:
      list:
        - Name: test
          VlanId: 100

任务

    - name: Creating vlan
      cisco.intersight.intersight_rest_api:
        resource_path: /fabric/Vlans
        update_method: post
        api_key_id: my_api_key_id
        api_private_key: my_api_private_key
        api_body: "{{ api_body_default|combine(item) }}"
      register: vlancreate
      loop: "{{ vlanlist.list }}"
      vars:
        api_body_default: {
          "AutoAllowOnUplinks": true,
          "IsNative": false
        }

应该按预期进行

  vlancreate:
    changed: true
    msg: All items completed
    results:
    - ansible_loop_var: item
      api_response: {}
      changed: true
      failed: false
      invocation:
        module_args:
          api_body:
            AutoAllowOnUplinks: true
            IsNative: false
            Name: test
            VlanId: 100
          api_key_id: my_api_key_id
          api_private_key: VALUE_SPECIFIED_IN_NO_LOG_PARAMETER
          api_uri: https://intersight.com/api/v1
          list_body: []
          query_params: {}
          resource_path: /fabric/Vlans
          return_list: false
          state: present
          update_method: post
          use_proxy: true
          validate_certs: true
      item:
        Name: test
        VlanId: 100
      trace_id: ''
    skipped: false

Create the dictionary on the fly thus avoiding the Jinja evaluation to strings. For example, create variable api_body_default (below simplified for testing) and combine it with the items from the list. Given the list

    vlanlist:
      list:
        - Name: test
          VlanId: 100

the task

    - name: Creating vlan
      cisco.intersight.intersight_rest_api:
        resource_path: /fabric/Vlans
        update_method: post
        api_key_id: my_api_key_id
        api_private_key: my_api_private_key
        api_body: "{{ api_body_default|combine(item) }}"
      register: vlancreate
      loop: "{{ vlanlist.list }}"
      vars:
        api_body_default: {
          "AutoAllowOnUplinks": true,
          "IsNative": false
        }

should work as expected

  vlancreate:
    changed: true
    msg: All items completed
    results:
    - ansible_loop_var: item
      api_response: {}
      changed: true
      failed: false
      invocation:
        module_args:
          api_body:
            AutoAllowOnUplinks: true
            IsNative: false
            Name: test
            VlanId: 100
          api_key_id: my_api_key_id
          api_private_key: VALUE_SPECIFIED_IN_NO_LOG_PARAMETER
          api_uri: https://intersight.com/api/v1
          list_body: []
          query_params: {}
          resource_path: /fabric/Vlans
          return_list: false
          state: present
          update_method: post
          use_proxy: true
          validate_certs: true
      item:
        Name: test
        VlanId: 100
      trace_id: ''
    skipped: false
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文