Ansible : Deploy task in round robin fashion

32 views
Skip to first unread message

Tejas Gadaria

unread,
Dec 8, 2018, 11:37:17 PM12/8/18
to Ansible Project
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

Tejas Gadaria

unread,
Dec 10, 2018, 11:18:58 AM12/10/18
to ansible...@googlegroups.com
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.


--
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 post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/94049513-df9a-4b5f-8a21-99297d70b4b2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Eric Brewer

unread,
Dec 17, 2018, 2:05:41 PM12/17/18
to Ansible Project
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.....
Reply all
Reply to author
Forward
0 new messages