Rolling upgrades based on dynamic-attribute

I’m trying to manage a set of hosts that make up a dynamic inventory with Ansible.

Based on the contents of a database that I can query from Ansible code - these hosts get assigned numbers is a sequence 0,1,2 …

I would like to apply a playbook to this inventory using this clustering - in other words, playbook gets applied to hosts with sequence number 0, after this batch is finished (playbook successfully applied to all) the playbook is applied to hosts with sequence number 1 and s.o.

I’m faulty new to Ansible and would really appreciate some pointers towards the idiomatic way of achieving this.

Thank you
Rady