我正在使用的剧本正在收集 Netapp 数据,然后将其存储在字典列表中。此后,我试图从同一个列表中取出所需的值。最后,我试图从所需的
index
中获得max
的list(list_of_available_size)
。在这个任务中我得到一个错误。
下面是我正在使用的剧本。
---
- 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
下面是我得到的错误。
TASK [Max available size aggr] **************************************************************************************
ok: [nas.foo.com]
TASK [debug] ********************************************************************************************************
ok: [nas.foo.com] => {
"max_size": "1491563708416"
}
TASK [index of max available size aggr] *****************************************************************************
fatal: [nas.foo.com]: FAILED! => {"msg": "template error while templating string: No filter named 'index'.. String: {{ list_of_available_size | index(max_size) }}"}
PLAY RECAP **********************************************************************************************************
nas.foo.com : ok=10 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
[b00193@vmu81181 nas-ansible-netapp]$
有人可以告诉我如何从列表中获取最高数字的索引吗?
这可能不是很有效,但如果我跟着你,我想你真的只想获得整数列表的最大值的索引。
您可以安装一些模块来执行此操作,但仅使用纯 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 }}"
注意:从技术上讲,这只是获取包含该值的 first 值,我认为这就是您想要的。
如何在 Ansible 中获取列表值的索引? ...如何从列表中获取最高数字的索引?
您可以安装一些模块来执行此操作
这些可能已经安装好了。没有任何循环且仅使用 Ansible 过滤器的更有效方法是以下最小示例
---
- hosts: localhost
become: false
gather_facts: false
vars:
nums:
- 37
- 109
- 302
- 31
- 100093
tasks:
- name: Show max value
debug:
msg: "{{ nums | max }}"
- name: Show index of max value (count from zero)
debug:
msg: "{{ nums | ansible.utils.index_of('eq', nums | max) }}"
导致输出
TASK [Show max value] *********************************
ok: [localhost] =>
msg: '100093'
TASK [Show index of max value (count from zero)] ******
ok: [localhost] =>
msg: '4'
总结
总之滤镜不会
index(LIST | max)
而是
LIST | ansible.utils.index_of('eq', LIST | max)
更多文档
带有实用程序的 Ansible Collection 可简化剧本中数据的管理、操作和验证......支持的 ansible-core 版本:2.9.10 或更新版本
index_of
过滤器 – 在列表中查找符合某些条件的项目索引
还有一个查找插件可用