Back-references in Ansible loops

时间:2019-04-17 01:53:38

标签: loops ansible

Is it possible to reference the "previous" value of a loop variable in an Ansible playbook?

What I would like to do is find some form of loop that effectively makes the following functional:

---
vars:
  nums: 
    - 1 
    - 2 
    - 3 
    - 4
tasks:
  - name: show fibonacci
    command: echo {{ item }} * {{ item.prev }}
    loop: nums

1 个答案:

答案 0 :(得分:0)

使用include_tasks。下面的游戏

tasks:
  - name: show fibonacci
    include_tasks: fibonacci.yml
    with_sequence: start=1 end=4

带有包含文件fibonacci.yml

- debug:
    msg: "{{ item|int * item_prev|default('1')|int }}"
- set_fact:
    item_prev: "{{ item }}"

给予:

"msg": "1"
"msg": "2"
"msg": "6"
"msg": "12"