Ansible with_item |首先没有返回任何项目?

问题描述 投票:0回答:1

我有一个如下所示的JSON数组:

[
  {
    "item": {
              "item": "SERVERNAME" 
            } 
  },
  {
    "item": {
              "item": "SERVERNAME2" 
            } 
  }
]

在上一个API调用中,我正在使用

     with_items: "{{ cluster_server.results }}"

并且能够通过{{item.item.item}}捕获每个服务器名称

但是此API调用只需要第一个结果,因此使用

     with_items: "{{ cluster_server.results | first}}"

但它返回此错误:

fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'item'\n\nThe error appears to be in '/home/mycomp/Documents/ansible/build-auth.yml': line 177, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n   - copy:\n     ^ here\n"}

我在这里做错了什么?对Ansible来说还很新。

我有如下所示的JSON数组:[{...“ item”:{“ item”:“ SERVERNAME”}},{{item“:{” item“:” SERVERNAME2“}}]在一个...] >

loops ansible undefined
1个回答
0
投票

您正在覆盖item,这是您的循环变量。为循环变量使用另一个名称:

  - debug:
      msg: "{{ server.item.item }}"
    with_items:
      - "{{ cluster_server.results | first }}"
    loop_control:
      loop_var: server
© www.soinside.com 2019 - 2024. All rights reserved.