# Find max value and run action based on a result only on one host

**URL:** <https://forum.ansible.com/t/find-max-value-and-run-action-based-on-a-result-only-on-one-host/17370>\
**Category:** Ansible Project\
**Created:** [January 25, 2015, 10:16pm UTC](https://forum.ansible.com/t/find-max-value-and-run-action-based-on-a-result-only-on-one-host/17370 "2015-01-25T22:16:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karol\_Stepniewski](https://avatars.discourse-cdn.com/v4/letter/k/7cd45c/32.png) [@Karol\_Stepniewski](https://forum.ansible.com/u/Karol_Stepniewski)\
**Post date:** [January 25, 2015, 10:16pm UTC](https://forum.ansible.com/t/find-max-value-and-run-action-based-on-a-result-only-on-one-host/17370/1 "2015-01-25T22:16:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Karol\_Stepniewski](https://avatars.discourse-cdn.com/v4/letter/k/7cd45c/32.png) [@Karol\_Stepniewski](https://forum.ansible.com/u/Karol_Stepniewski)\
**Post date:** [January 26, 2015, 7:42pm UTC](https://forum.ansible.com/t/find-max-value-and-run-action-based-on-a-result-only-on-one-host/17370/2 "2015-01-26T19:42:52Z")

</div>

I actually solved my problem, although I’m using some kind of workaround.

1. First thing - max value always returning the second value. It turned out that hostvars converts fact back to string, so before comparison you have to reapply int filter.

2. Second thing - finding max value for any number of hosts. I ended up using this approach:

`

- shell: “if [{{ hostvars[inventory\_hostname][‘seqno’] }} -lt {{ hostvars[item][‘seqno’] }} ]; then echo {{ hostvars[item][‘seqno’] }}; fi”  
with\_items: groups[‘db’]  
register: result\_c

- set\_fact: seqno\_max={{ hostvars[inventory\_hostname][‘seqno’] }}  
when: result\_c.results | map(attribute=‘stdout’) | join(‘’) == “”  
`

It’s not the best looking solution, but I guess it’s hard to get one with ansible currently. Ideally it would be great to have something like groups[“db”] | max(“seqno”)
