8

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?

0

2 Answers 2

12
- 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

1
  • 1
    Amazing, I was fooling myself and there was a dedicated looping command.
    – guillem
    Commented Feb 6, 2018 at 22:49
5

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.