A general question about looping

I know I can do this from a shell script. I wonder if I can do this in pure Ansible.
I was thinking about clusters recently. I know I can use the VM module to spin up a VM.
If I wanted 1,000 nodes, how would I loop inside Ansible for the nodes?
Using the shell I would do something like (not tested):

for i in `seq 000 999`
do
  ansible-playbook -e nodename=“beofulfnode$i” create-a-node.yml
done

How to do this looping in pure Ansible?

Mike

Have a look to with_sequence

It’s something close to this, but “create-a-node.yml” isn’t a playbook in this case, but rather just a task file.

  • name: loop over beofulf nodes
    include_tasks: create-a-node.yml
    with_sequence: start=000 end=999
    vars:
    nodename: “beofulfnode{{ item }}”

Apparently the recommendation these days is to use the range filter:

https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#with-sequence

<b>  - name: loop over beofulf nodes
    include_tasks: create-a-node.yml
    loop: "{{ range(0, 1000) | list }}"
    vars:
      nodename: "{{ 'beofulfnode%03d' | format(item) }}"</b>

Thank you to everyone. :slight_smile:

Mike