From dc48d6879d5baa214abc0a26e37753ada469292c Mon Sep 17 00:00:00 2001 From: Stefan Schatz Date: Wed, 11 Oct 2017 22:26:43 +0200 Subject: [PATCH] zabbix_host: added zabbix host property (description) (#25969) * zabbix_host: added zabbix host property (description) * zabbix_host: fixed error E309 version_added for new option (description) should be 2.4 * zabbix_host: deleted unwanted else for update description * lib/ansible/modules/monitoring/zabbix_host.py: increased version_added to 2.5 for option description * lib/ansible/modules/monitoring/zabbix_host.py: allow to change the description * lib/ansible/modules/monitoring/zabbix_host.py: added new lines back to fix pep8 issues --- lib/ansible/modules/monitoring/zabbix_host.py | 37 ++++++++++++++----- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/lib/ansible/modules/monitoring/zabbix_host.py b/lib/ansible/modules/monitoring/zabbix_host.py index f46794ebde..4996054286 100644 --- a/lib/ansible/modules/monitoring/zabbix_host.py +++ b/lib/ansible/modules/monitoring/zabbix_host.py @@ -64,6 +64,11 @@ options: - Visible name of the host in Zabbix. required: false version_added: '2.3' + description: + description: + - Description of the host in Zabbix. + required: false + version_added: '2.5' host_groups: description: - List of host groups the host is part of. @@ -172,6 +177,7 @@ EXAMPLES = ''' login_password: password host_name: ExampleHost visible_name: ExampleName + description: My ExampleHost Description host_groups: - Example group1 - Example group2 @@ -268,7 +274,7 @@ class Host(object): template_ids.append(template_id) return template_ids - def add_host(self, host_name, group_ids, status, interfaces, proxy_id, visible_name, tls_connect, + def add_host(self, host_name, group_ids, status, interfaces, proxy_id, visible_name, description, tls_connect, tls_accept, tls_psk_identity, tls_psk, tls_issuer, tls_subject): try: if self._module.check_mode: @@ -287,6 +293,9 @@ class Host(object): parameters['tls_issuer'] = tls_issuer if tls_subject: parameters['tls_subject'] = tls_subject + if description: + parameters['description'] = description + host_list = self._zapi.host.create(parameters) if len(host_list) >= 1: return host_list['hostids'][0] @@ -294,7 +303,7 @@ class Host(object): self._module.fail_json(msg="Failed to create host %s: %s" % (host_name, e)) def update_host(self, host_name, group_ids, status, host_id, interfaces, exist_interface_list, proxy_id, - visible_name, tls_connect, tls_accept, tls_psk_identity, tls_psk, tls_issuer, tls_subject): + visible_name, description, tls_connect, tls_accept, tls_psk_identity, tls_psk, tls_issuer, tls_subject): try: if self._module.check_mode: self._module.exit_json(changed=True) @@ -312,6 +321,9 @@ class Host(object): parameters['tls_issuer'] = tls_issuer if tls_subject: parameters['tls_subject'] = tls_subject + if description: + parameters['description'] = description + self._zapi.host.update(parameters) interface_list_copy = exist_interface_list if interfaces: @@ -428,7 +440,7 @@ class Host(object): # check all the properties before link or clear template def check_all_properties(self, host_id, host_groups, status, interfaces, template_ids, - exist_interfaces, host, proxy_id, visible_name, host_name): + exist_interfaces, host, proxy_id, visible_name, description, host_name): # get the existing host's groups exist_host_groups = self.get_host_groups_by_host_id(host_id) if set(host_groups) != set(exist_host_groups): @@ -455,6 +467,12 @@ class Host(object): if host['name'] != visible_name and host['name'] != host_name: return True + # The Zabbbix API returns an empty description as an empty string + if description is None: + description = '' + if host['description'] != description: + return True + return False # link or clear template of the host @@ -535,8 +553,8 @@ def main(): interfaces=dict(type='list', required=False), force=dict(type='bool', default=True), proxy=dict(type='str', required=False), - visible_name=dict(type='str', required=False) - + visible_name=dict(type='str', required=False), + description=dict(type='str', required=False) ), supports_check_mode=True ) @@ -551,6 +569,7 @@ def main(): http_login_password = module.params['http_login_password'] host_name = module.params['host_name'] visible_name = module.params['visible_name'] + description = module.params['description'] host_groups = module.params['host_groups'] link_templates = module.params['link_templates'] inventory_mode = module.params['inventory_mode'] @@ -648,11 +667,11 @@ def main(): if len(exist_interfaces) > interfaces_len: if host.check_all_properties(host_id, host_groups, status, interfaces, template_ids, - exist_interfaces, zabbix_host_obj, proxy_id, visible_name, host_name): + exist_interfaces, zabbix_host_obj, proxy_id, visible_name, description, host_name): host.link_or_clear_template(host_id, template_ids, tls_connect, tls_accept, tls_psk_identity, tls_psk, tls_issuer, tls_subject) host.update_host(host_name, group_ids, status, host_id, - interfaces, exist_interfaces, proxy_id, visible_name, tls_connect, tls_accept, + interfaces, exist_interfaces, proxy_id, visible_name, description, tls_connect, tls_accept, tls_psk_identity, tls_psk, tls_issuer, tls_subject) module.exit_json(changed=True, result="Successfully update host %s (%s) and linked with template '%s'" @@ -663,7 +682,7 @@ def main(): if host.check_all_properties(host_id, host_groups, status, interfaces, template_ids, exist_interfaces_copy, zabbix_host_obj, proxy_id, visible_name, host_name): host.update_host(host_name, group_ids, status, host_id, interfaces, exist_interfaces, proxy_id, - visible_name, tls_connect, tls_accept, tls_psk_identity, tls_psk, tls_issuer, + visible_name, description, tls_connect, tls_accept, tls_psk_identity, tls_psk, tls_issuer, tls_subject) host.link_or_clear_template(host_id, template_ids, tls_connect, tls_accept, tls_psk_identity, tls_psk, tls_issuer, tls_subject) @@ -691,7 +710,7 @@ def main(): module.fail_json(msg="Specify at least one interface for creating host '%s'." % host_name) # create host - host_id = host.add_host(host_name, group_ids, status, interfaces, proxy_id, visible_name, tls_connect, + host_id = host.add_host(host_name, group_ids, status, interfaces, proxy_id, visible_name, description, tls_connect, tls_accept, tls_psk_identity, tls_psk, tls_issuer, tls_subject) host.link_or_clear_template(host_id, template_ids, tls_connect, tls_accept, tls_psk_identity, tls_psk, tls_issuer, tls_subject)