假设我们有一个 Ansible 变量,它是一个 list_of_ints
。
我想定义一个 incremented_list
,其元素是通过将第一个列表的元素递增固定量来获取的。
例如,如果这是第一个变量:
---
# file: somerole/vars/main.yml
list_of_ints:
- 1
- 7
- 8
假设增量为 100,所需的第二个列表将包含以下内容:
incremented_list:
- 101
- 107
- 108
我正在考虑以下内容:
incremented_list: "{{ list_of_ints | map('add', 100) | list }}"
遗憾的是,Ansible 有 custom filters for logarithms or powers ,但不适用于基本算术,因此我可以轻松计算这些数字的 log10,但不能增加它们。
除了 https://github.com/ansible/ansible/blob/v2.1.1.0-1/lib/ansible/plugins/filter/mathstuff.py 上的拉取请求之外,还有任何想法吗? ?
最佳答案
这样就可以了:
---
- hosts: localhost
connection: local
vars:
incremented_list: []
list_of_ints:
- 1
- 7
- 8
incr: 100
tasks:
- set_fact:
incremented_list: "{{ incremented_list + [ item + incr ] }}"
no_log: False
with_items: "{{ list_of_ints }}"
- name: show cntr
debug: var=incremented_list
关于python - Ansible:给定变量中的整数列表,定义第二个列表,其中每个元素递增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39111787/