guillem
guillem

Reputation: 2988

Ansible with items in range

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?

Upvotes: 9

Views: 23442

Answers (2)

Milad Jahandideh
Milad Jahandideh

Reputation: 751

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 }}"

Upvotes: 6

techraf
techraf

Reputation: 68439

- 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

Upvotes: 13

Related Questions