Looping through hosts in a playbook

瀏覽次數:135 次
跳到第一則未讀訊息

Roger Sherman

未讀,
2015年11月25日 下午2:24:022015/11/25
收件者:Ansible Project
I'm working on a playbook, that will be deploying a java app to four hosts. I want it to push the app out to each node, making sure the correct directories exist to push the new version to - that's the easy part, and it can do all four of the nodes at once, no issue there. But then I want it to loop through the list of hosts, taking them out of the load balancer, changing the app version that is linked on each node, and then put them back into the load balancer, one at a time. All of the tasks are easily enough performed, but the looping part I'm not sure about. I've googled looping through hosts, but mainly I'm finding articles on how to loop through hosts in a template.

Is it possible to run through hosts in a loop as a task, or alternatively, can the tasks be run from a template? I suppose I could just set the whole playbook to do just one host at a time (serial: 1), or do multiple ansible runs, limiting the run to one host at a time, but that seems a bit inelegant - it seems like the tasks that can safely be run in parallel should be, and the tasks that can't should be looped. Am I overthinking this?

Alexander Artemenko

未讀,
2015年11月26日 凌晨12:44:452015/11/26
收件者:Ansible Project
Roger, did you try to use "with_items" (http://docs.ansible.com/ansible/playbooks_loops.html#standard-loops)?

среда, 25 ноября 2015 г., 22:24:02 UTC+3 пользователь Roger Sherman написал:

Philippe Eveque

未讀,
2015年11月26日 清晨7:40:212015/11/26
收件者:ansible...@googlegroups.com
that seems close to what you'r trying to achiev

2015-11-25 20:24 GMT+01:00 Roger Sherman <slammin...@gmail.com>:
I'm working on a playbook, that will be deploying a java app to four hosts. I want it to push the app out to each node, making sure the correct directories exist to push the new version to - that's the easy part, and it can do all four of the nodes at once, no issue there. But then I want it to loop through the list of hosts, taking them out of the load balancer, changing the app version that is linked on each node, and then put them back into the load balancer, one at a time. All of the tasks are easily enough performed, but the looping part I'm not sure about. I've googled looping through hosts, but mainly I'm finding articles on how to loop through hosts in a template.

Is it possible to run through hosts in a loop as a task, or alternatively, can the tasks be run from a template? I suppose I could just set the whole playbook to do just one host at a time (serial: 1), or do multiple ansible runs, limiting the run to one host at a time, but that seems a bit inelegant - it seems like the tasks that can safely be run in parallel should be, and the tasks that can't should be looped. Am I overthinking 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/62291520-6f0a-4520-a682-4fbdd57b23a1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Paul Markham

未讀,
2015年11月26日 晚上10:27:562015/11/26
收件者:Ansible Project
You can have multiple plays in a playbook. Put the tasks that can be done in parallel in one play and the tasks that need to be done one at a time in another play with "serial: 1".

Roger Sherman

未讀,
2015年11月27日 下午3:18:422015/11/27
收件者:Ansible Project
Hi Alexander,

I don't think "with_items" is the way to go here - if I use "with_items", it's going to try to execute on all of the hosts at once, unless it can be used in a method that I haven't thought of.

Roger Sherman

未讀,
2015年11月27日 下午3:19:252015/11/27
收件者:Ansible Project
Thanks Philippe, I'll look at that.

Roger Sherman

未讀,
2015年11月27日 下午3:20:182015/11/27
收件者:Ansible Project
Oh, that's an interesting idea, thanks Paul - I'll do some experimenting with that.
回覆所有人
回覆作者
轉寄
0 則新訊息