# Dynamically change the order of hosts ansible runs on

**URL:** <https://forum.ansible.com/t/dynamically-change-the-order-of-hosts-ansible-runs-on/15144>\
**Category:** Ansible Project\
**Created:** [May 2, 2014, 3:05pm UTC](https://forum.ansible.com/t/dynamically-change-the-order-of-hosts-ansible-runs-on/15144 "2014-05-02T15:05:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andy\_Wang](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Andy\_Wang](https://forum.ansible.com/u/Andy_Wang)\
**Post date:** [May 2, 2014, 3:05pm UTC](https://forum.ansible.com/t/dynamically-change-the-order-of-hosts-ansible-runs-on/15144/1 "2014-05-02T15:05:19Z")

</div>

This might be a newbie question, but I can’t figure out how to without writing a wrapper around ansible, or invoking ansible multiple times

Let’s say this is my inventory:

[some\_service]  
A  
B  
C

Some\_service runs in synchronization mode so one is the master, which need to be shut down last.

The trouble is I do not know without actually running “some\_command” which one is the master, so the shutdown order might be B-\>C-\>A or A-\>C-\>B, etc.

Is it possible to have ansible runs normally over these hosts, read the exit status of “some\_command” and then delay executing shutdown on master until the others are done?

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [May 2, 2014, 3:33pm UTC](https://forum.ansible.com/t/dynamically-change-the-order-of-hosts-ansible-runs-on/15144/2 "2014-05-02T15:33:01Z")

</div>

To handle this, I would create a custom fact ([http://docs.ansible.com/playbooks\_variables.html#local-facts-facts-d](http://docs.ansible.com/playbooks_variables.html#local-facts-facts-d)) that returned whether the current host is in the master state. Then you could write two tasks as follows:

- name: shut down non-masters

# ← whatever method you’re using to shut things down, ie. service, etc.

when: not ansible\_local.master\_status.is\_master

- name: shut down the master last

# ← whatever method you’re using to shut things down, ie. service, etc.

when: ansible\_local.master\_status.is\_master

Assuming the local fact was named “master\_status.fact” and it returned JSON like {‘is\_master’: true}.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 2, 2014, 7:08pm UTC](https://forum.ansible.com/t/dynamically-change-the-order-of-hosts-ansible-runs-on/15144/3 "2014-05-02T19:08:07Z")

</div>

I’d do it by putting the head node into a group of it’s own, to make it more explicit

- hosts: cluster  
tasks:

- …

- hosts: cluster:!head\_node  
tasks:

- …

- hosts: head\_node  
tasks:

- …

Just personal preference but where I can avoid if conditionals, I try to. Ansible likes to avoid programming constructs when it can.

---

<div class="post-metadata">

**Author:** ![Dimon](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dimon](https://forum.ansible.com/u/Dimon)\
**Post date:** [May 3, 2014, 2:50am UTC](https://forum.ansible.com/t/dynamically-change-the-order-of-hosts-ansible-runs-on/15144/4 "2014-05-03T02:50:18Z")

</div>

Add to that creation of dynamic group from hosts or ips:  
[https://coderwall.com/p/cz-pjw](https://coderwall.com/p/cz-pjw)

and you can populate your “head\_node” group early in playbook and then play by Michael’s suggestion for full nirvana 🙂

---

<div class="post-metadata">

**Author:** ![Kalyan\_Chakravarthi](https://avatars.discourse-cdn.com/v4/letter/k/f1d935/32.png) [@Kalyan\_Chakravarthi](https://forum.ansible.com/u/Kalyan_Chakravarthi)\
**Post date:** [June 19, 2019, 1:35pm UTC](https://forum.ansible.com/t/dynamically-change-the-order-of-hosts-ansible-runs-on/15144/5 "2019-06-19T13:35:23Z")

</div>

Hi James,  
I have a doubt on your comment, Ansible runs playbook in order A,B,C for example and server B is a master server for example. How ansible runs shut down on C before B? It actually goes order A,B,C. Do you mean put the mentioned tasks in handler or tasks? I completely dint not get how we achieve this. Please give me some more details on this. Thanks.
