A general question about looping

14 views
Skip to first unread message

Mike Eggleston

unread,
Dec 10, 2021, 4:05:31 PM12/10/21
to ansible...@googlegroups.com
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

Jorge Rúa

unread,
Dec 10, 2021, 4:29:24 PM12/10/21
to ansible...@googlegroups.com
Have a look to with_sequence 

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/A6093B7A-C19D-4060-9320-A2F50540677B%40gmail.com.

Todd Lewis

unread,
Dec 10, 2021, 4:55:17 PM12/10/21
to Ansible Project
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 }}"

Dick Visser

unread,
Dec 11, 2021, 3:23:08 AM12/11/21
to ansible...@googlegroups.com
Apparently the recommendation these days is to use the range filter:


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Todd Lewis

unread,
Dec 13, 2021, 8:11:22 AM12/13/21
to Ansible Project
  - name: loop over beofulf nodes
    include_tasks: create-a-node.yml
    loop: "{{ range(0, 1000) | list }}"
    vars:
      nodename: "{{ 'beofulfnode%03d' | format(item) }}"

Mike Eggleston

unread,
Dec 13, 2021, 9:11:55 AM12/13/21
to ansible...@googlegroups.com
Thank you to everyone. :)

Mike

Reply all
Reply to author
Forward
0 new messages