Hello,

I have a group of hosts named "db" with number of nodes that may vary. Each 
node has a fact ("seqno") which is an integer number.

I need to compare this fact among all hosts and choose maximum value, then 
run some actions on one (and only one) host that has this maximum value. In 
case of multiple nodes having the same value, first node should be chosen.

I tried this approach:

- name: find max seqno value
  set_fact: seqno_max={{ [hostvars[groups['db'][0]]['seqno'], hostvars[
groups['db'][1]]['seqno']] | max }}


- name: find single hostname to use as a node with max seqno
  set_fact: seqno_max_host={{ hostvars[item]['inventory_hostname'] }}
  with_items: groups['db'][::-1] # reverse list. if two nodes have the same 
seqno, use first node as starting point.
  when: hostvars[item]['seqno'] == seqno_max


- name: Some actions based on a result of previous tasks
  action: # Run some actions
  when:  seqno_max_host == inventory_hostname



But for some reason "max" operator always return the second value. Also 
this approach is valid only if you have arbitrary specified number of hosts 
- It would be best to have a solution that works for any number of hosts. 
Is there a better approach to that?

Karol

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/009f7069-1033-4ae0-93df-796c8167c93d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to