【问题标题】:Ansible with_item | first returns no items?Ansible with_item |首先不返回任何项目?
【发布时间】:2020-02-28 00:02:09
【问题描述】:

我有一个 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 来说相当陌生。

【问题讨论】:

    标签: loops ansible undefined


    【解决方案1】:

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

      - debug:
          msg: "{{ server.item.item }}"
        with_items:
          - "{{ cluster_server.results | first }}"
        loop_control:
          loop_var: server
    

    【讨论】:

    • 顺便说一句,如果您只需要第一项,则根本不需要循环。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-15
    • 2017-10-26
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    相关资源
    最近更新 更多