5

I would like to achieve something like this with ansible

- debug:
    msg: "{{ item }}"
  with_items:
    - "0"
    - "1"

But to be generated from a range(2) instead of having hardcoded the iterations. How would yo do that?

guillem
  • 2,413
  • 1
  • 29
  • 41

2 Answers2

8
- debug:
    var: item
  with_sequence: 0-1

or

  with_sequence: start=0 end=1

or

  with_sequence: start=0 count=2

Pay attention that sequences are string values, not integers (you can cast with item|int)

Reference: Looping over Integer Sequences

techraf
  • 59,327
  • 25
  • 171
  • 185
0

Because with_sequence is replaced by loop and the range function you can also use loop with range function like this example:

- hosts: localhost
  tasks:
    - name: loop with range functions
      ansible.builtin.debug:
        msg: "{{ 'number: %s' | format(item) }}"
      loop: "{{ range(0, 2, 1)|list }}"
Milad Jahandideh
  • 81
  • 1
  • 1
  • 6