From 8cf6f56a0103c51e93de9e26ce1982ce4aeb7ba0 Mon Sep 17 00:00:00 2001 From: Mikhail Naletov <36846182+okgolove@users.noreply.github.com> Date: Mon, 3 Jun 2019 22:23:20 +0300 Subject: [PATCH] Added sort option for zabbix_screen module (#56237) * Added sort option for zabbix_screen module * FIX: sort variable * Added changelog file * Fixed misspelling * Update lib/ansible/modules/monitoring/zabbix/zabbix_screen.py Changed spelling in the documentation Co-Authored-By: flowerysong --- .../56237-added-sort-option-for-zabbix_screen.yml | 2 ++ lib/ansible/modules/monitoring/zabbix/zabbix_screen.py | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/56237-added-sort-option-for-zabbix_screen.yml diff --git a/changelogs/fragments/56237-added-sort-option-for-zabbix_screen.yml b/changelogs/fragments/56237-added-sort-option-for-zabbix_screen.yml new file mode 100644 index 0000000000..d4cf798840 --- /dev/null +++ b/changelogs/fragments/56237-added-sort-option-for-zabbix_screen.yml @@ -0,0 +1,2 @@ +minor_changes: +- "zabbix_screen - added an option to sort hosts on a zabbix screen alphabetically" diff --git a/lib/ansible/modules/monitoring/zabbix/zabbix_screen.py b/lib/ansible/modules/monitoring/zabbix/zabbix_screen.py index 89da13ae5f..1b9257c637 100644 --- a/lib/ansible/modules/monitoring/zabbix/zabbix_screen.py +++ b/lib/ansible/modules/monitoring/zabbix/zabbix_screen.py @@ -38,6 +38,7 @@ options: - > The available states are: C(present) (default) and C(absent). If the screen already exists, and the state is not C(absent), the screen will be updated as needed. + - To sort hosts alphabetically, set the C(sort) option to C(true) required: true extends_documentation_fragment: @@ -150,11 +151,13 @@ class Screen(object): return hostGroup_id # get monitored host_id by host_group_id - def get_host_ids_by_group_id(self, group_id): + def get_host_ids_by_group_id(self, group_id, sort): host_list = self._zapi.host.get({'output': 'extend', 'groupids': group_id, 'monitored_hosts': 1}) if len(host_list) < 1: self._module.fail_json(msg="No host in the group.") else: + if sort: + host_list = sorted(host_list, key=lambda name: name['name']) host_ids = [] for i in host_list: host_id = i['hostid'] @@ -348,6 +351,7 @@ def main(): screen_name = zabbix_screen['screen_name'] screen_id = screen.get_screen_id(screen_name) state = "absent" if "state" in zabbix_screen and zabbix_screen['state'] == "absent" else "present" + sort = bool(zabbix_screen.get("sort", False)) if state == "absent": if screen_id: @@ -373,7 +377,7 @@ def main(): if 'graph_height' in zabbix_screen: graph_height = zabbix_screen['graph_height'] host_group_id = screen.get_host_group_id(host_group) - hosts = screen.get_host_ids_by_group_id(host_group_id) + hosts = screen.get_host_ids_by_group_id(host_group_id, sort) screen_item_id_list = [] resource_id_list = []