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

半世苍凉 提交于 2019-12-25 04:45:33

问题


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 - I would like to have a solution that works for any number of hosts.

EDIT: It turned out that hostvars converted integer back to string, so comparison of them gave unexpected results. Reapplying int filter for each hostvars reference inside max filter helped. Still, questions remains how to fix code above to make it work for any number of hosts - is it possible without writing custom filters or creating temporary templates?


回答1:


I ended up with a solution which is not beautiful, but works.

- 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('') == ""


来源:https://stackoverflow.com/questions/28141936/ansible-find-max-value-and-run-action-based-on-a-result-only-on-one-host

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!