【发布时间】: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”