python - Ansible:给定变量中的整数列表,定义第二个列表,其中每个元素递增

标签 python functional-programming ansible jinja2

假设我们有一个 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/

相关文章:

python - 是否可以创建一个完全独立的 Python 包?

python - 尝试在 tensorflow 中创建 OCR,字母训练后要做什么?

python - 从 cpp、python 和 csv 文件中生成可执行文件?

python - 实现中序遍历时出现NameError

kotlin - Kotlin 箭头库中 Either.fold() 的返回类型

haskell - 翻转修复/修复

amazon-web-services - 如何在 EC2 实例中使用 sudo 从 ansible 运行任务

javascript - Flask json 响应在 ajax 成功处理程序中失败

haskell - (filter p xs, filter (not.p) xs) 的标准名称

ansible - 是否可以在任务中为角色定义变量?