# Run command on 1 server from group A, then run another command on 1 from group B, in loop

**URL:** <https://forum.ansible.com/t/run-command-on-1-server-from-group-a-then-run-another-command-on-1-from-group-b-in-loop/14990>\
**Category:** Ansible Project\
**Created:** [April 10, 2014, 2:32pm UTC](https://forum.ansible.com/t/run-command-on-1-server-from-group-a-then-run-another-command-on-1-from-group-b-in-loop/14990 "2014-04-10T14:32:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vladimir\_Mihailenco](https://avatars.discourse-cdn.com/v4/letter/v/8baadc/32.png) [@Vladimir\_Mihailenco](https://forum.ansible.com/u/Vladimir_Mihailenco)\
**Post date:** [April 10, 2014, 2:32pm UTC](https://forum.ansible.com/t/run-command-on-1-server-from-group-a-then-run-another-command-on-1-from-group-b-in-loop/14990/1 "2014-04-10T14:32:36Z")

</div>

Hello,

I have 2 Ansible groups:

- nginx\_servers.
- api\_servers.

I am trying to automate following:

1. Select 1 API server.
2. Mark selected API server as down in the all nginx configs. Reload nginx.
3. Restart selected API server.
4. Mark selected API server as up in the all nginx configs.
5. Goto 1. until all API servers are restarted.

Should I use Ansible for this? Any suggestions? hints? code samples?

---

<div class="post-metadata">

**Author:** ![Paul\_Durivage](https://avatars.discourse-cdn.com/v4/letter/p/94ad74/32.png) [@Paul\_Durivage](https://forum.ansible.com/u/Paul_Durivage)\
**Post date:** [April 10, 2014, 4:22pm UTC](https://forum.ansible.com/t/run-command-on-1-server-from-group-a-then-run-another-command-on-1-from-group-b-in-loop/14990/2 "2014-04-10T16:22:04Z")

</div>

I don’t have any examples for you, but Ansible is definitely the right tool for this kind of orchestration. I’m sure someone here can provide some samples.

You’ll want to write a playbook that enumerates these steps. Use the “serial” directive in your play so that only one server is touched at once before moving on to the next.

This documentation page should be helpful: [http://docs.ansible.com/playbooks\_conditionals.html#applying-when-to-roles-and-includes](http://docs.ansible.com/playbooks_conditionals.html#applying-when-to-roles-and-includes)

---

<div class="post-metadata">

**Author:** ![Mark\_Janssen](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@Mark\_Janssen](https://forum.ansible.com/u/Mark_Janssen)\
**Post date:** [April 11, 2014, 1:17pm UTC](https://forum.ansible.com/t/run-command-on-1-server-from-group-a-then-run-another-command-on-1-from-group-b-in-loop/14990/3 "2014-04-11T13:17:39Z")

</div>

This should be easy with ansible… make sure you use ‘serial 1’ … so you only work on one host at a time.  
Then use ‘delegate\_to’ tasks to remove/add the hosts to your nginx config/reload configs

Mark
