Ansible : Deploy task in round robin fashion

Hello all,

I have group tgt-cluster includes 3 hosts. I have written down role to deploy container which is executing on tgt-cluster group. I am controlling the number of containers to deploy with with_sequence. My tasks looks like this.

- name: Deploy Container 
  docker_container:
    name: "C{{ item }}"
    image: "{{ image_name }}:{{ image_tag }}"
    recreate: yes
    detach: yes
    tty: yes
    interactive: yes
  with_sequence: count="{{ number_of_container_to_deploy }}" 

If I want to deploy one container, currently playbook is executing on all 3 hosts in tgt-clustergroup and I end up with 3 containers. So How should I created nested loop in this case to control the task execution on hosts on round robin fashion.

Say if we want to deploy 4 container… 1st container should be deployed on 1st host in group, 2nd container should be deployed on 2nd host in group, 3rd should be deployed on 3rd hosts in group and 4th container should be deployed back to the 1st host in group.

Need your help on this,

Thanks,

Tejas

I tried jinja to loop over the group of hosts to serve my purpose but didn’t get expected result.
Need your help on this.

So you could probably approach this a few different ways. If you know the host in the group, you could apply a when conditional

when: inventory_hostname == ""

That would allow you to evaluate which server to run it on for the given group. If you don’t care which one it gets run on, and you have a way to only run this on one group at a time, you could also probably use this:

run_once: yes

That way it would just run against the first server it came across, whichever one that was…