【问题标题】:Unable to get the index of a list value in ansible无法在ansible中获取列表值的索引
【发布时间】:2023-02-26 04:42:42
【问题描述】:

我正在使用的剧本正在收集 netapp 数据,然后将其存储在字典列表中。 此后,我试图从同一个列表中取出所需的值。最后,我试图从所需列表(list_of_available_size)中获取 max 的索引。在此任务期间,我收到一个错误。

以下是我正在使用的剧本。

---
- hosts: exec-node
  collections:
    - netapp.ontap

  vars:
    list_of_available_size: []
    list_of_aggr_name: []
  vars_files:
    - secretvars.yaml

  tasks:
    - name: Gather aggregate info
      netapp.ontap.na_ontap_rest_info:
        hostname: "nas.foo.com"
        username: "{{ username }}"
        password: "{{ password }}"
        https: true
        fields:
          - 'space'
        validate_certs: false
        gather_subset:
          - storage/aggregates
      register: result
    - set_fact:
        aggrdetails: "{{ result['ontap_info']['storage/aggregates']['records'] }}"
    - debug: var=aggrdetails
    - name: Available size check in the aggregates
      loop: "{{ aggrdetails }}"
      set_fact:
        list_of_available_size: "{{ list_of_available_size+[item['space']['block_storage']['available']] | map('int') }}"
    - debug: var=list_of_available_size
    - name: aggregare listing
      loop: "{{ aggrdetails }}"
      set_fact:
        list_of_aggr_name: "{{ list_of_aggr_name+[item['name']] }}"
    - debug: var=list_of_aggr_name
    - name: Max available size aggr
      set_fact:
        max_size: "{{ list_of_available_size | max }}"
    - debug: var=max_size
    - name: index of max available size aggr
      set_fact:
        aggr_index_required: "{{ list_of_available_size | index(max_size) }}"
    - debug: var=aggr_index_required

以下是我得到的错误。

任务 [最大可用大小 aggr] ******************************************* ********************************************** 好的:[nas.foo.com]

任务 [调试] ************************************************* ****************************************************** ********* 好的:[nas.foo.com] => { “最大尺寸”:“1491563708416” }

任务 [最大可用大小聚合的索引] ****************************************** ************************************ 致命的:[nas.foo.com]:失败! => {“msg”:“模板化字符串时出现模板错误:没有名为‘index’的过滤器。字符串:{{ list_of_available_size | index(max_size) }}”}

播放回顾 *************************************************** ****************************************************** ********* nas.foo.com : ok=10 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

[b00193@vmu81181 nas-ansible-netapp]$

有人可以让我知道如何从列表中获取最高编号的索引吗?

【问题讨论】:

  • 确实没有index过滤器。 .index() 是 Python 列表的一种方法,但是,您应该尝试使用 my_list.index(foo) 而不是 my_list | index(foo)
  • 我也尝试使用 python 语法。但我现在得到下面提到的错误。失败的! => {"msg": "字段 'args' 的值无效或者是:'1491906289664' 不在列表中\n\n
  • 如果你可以制作一个人们可以运行的剧本,静态地包含从你的 NAS 返回的数据,那么有人可能会很容易地帮助你解析数据结构。
  • 嗨@MattBlaha 下面是列表的内容,我试图在其中找到特定值的索引。 list_of_available_size: [1485972381696, 1491860144128] & 下面是我试图在上面的列表中找到索引的特定值。 “最大尺寸”:“1491860144128”

标签: list ansible


【解决方案1】:

这可能不是非常有效,但如果我跟着你,我认为你真的只想获得整数列表的最大值的索引。您可以安装一些模块来执行此操作,但要仅使用纯 Ansible 来执行此操作,它可以工作并且易于阅读:

---
- hosts: localhost
  gather_facts: false
  vars:
    nums:
      - 37
      - 109
      - 302
      - 31
      - 100093

  tasks:
    - name: Get index of max value
      set_fact:
        idx: "{{ index }}"
      when: item == max | int 
      loop: "{{ nums }}"
      loop_control:
        index_var: index
      vars:
        max: "{{ nums | max }}"
    - name: debug idx
      debug:
        msg: "{{ idx }}"

(注意:这在技术上只是得到了第一的包含该值的值,我认为这就是您想要的。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    • 2023-01-22
    • 2021-10-06
    • 1970-01-01
    • 2019-06-06
    相关资源
    最近更新 更多