6

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,768
  • 2
  • 30
  • 44

2 Answers2

10
- 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
  • 64,883
  • 27
  • 193
  • 198
3

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
  • 490
  • 1
  • 4
  • 13