2016-08-23 138 views
2

假設我們有一個Ansible變量,它是list_of_intsAnsible:給出變量中的整數列表,定義第二個列表,其中每個元素遞增

我想定義一個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上的拉請求?

回答

2

這將做到這一點:

--- 

- 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 
+0

喜@搶劫小時,其實這工作,謝謝! 但是,我真正想要達到的目標是以聲明方式做事,而沒有爲此使用任務。 這實際上不可能與Ansible 2.1,我想提交[公關#17251](https://github.com/ansible/ansible/pull/17251),看看這可能是有用的。 在此期間,有朋友建議此解決方案,這使得只用一個任務,而不是兩個: – muxator

+0

解決方案與單一的任務,使用默認設置()過濾器: 'incremented_list:「{{(incremented_list |默認([ ]))+ [item + incr]}}「' – muxator

相關問題