Dynamically change the order of hosts ansible runs on

1,136 views
Skip to first unread message

nfwlpw

unread,
May 2, 2014, 11:05:19 AM5/2/14
to ansible...@googlegroups.com
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?


James Cammarata

unread,
May 2, 2014, 11:33:01 AM5/2/14
to ansible...@googlegroups.com
To handle this, I would create a custom fact (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}.



--
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/41bb1f9a-060a-4d1e-a2ae-0a0eabd124d6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael DeHaan

unread,
May 2, 2014, 3:08:07 PM5/2/14
to ansible...@googlegroups.com
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.




Dmitry Makovey

unread,
May 2, 2014, 10:50:18 PM5/2/14
to ansible...@googlegroups.com
Add to that creation of dynamic group from hosts or ips:

and you can populate your "head_node" group early in playbook and then play by Michael's suggestion for full nirvana :)

Kalyan Chakravarthi

unread,
Jun 19, 2019, 9:35:23 AM6/19/19
to Ansible Project
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. 


On Friday, May 2, 2014 at 5:33:01 PM UTC+2, James Cammarata wrote:
To handle this, I would create a custom fact (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}.

On Fri, May 2, 2014 at 10:05 AM, nfwlpw <nfw...@gmail.com> wrote:
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?


--
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...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages