code: def _diff_and_merge_host_list(defined, existing):
      '''
      If Zabbix template is to be updated then list of assigned hosts must be provided in all or nothing manner to prevent
      some externally assigned hosts to be detached.
  
      :param defined: list of hosts defined in sls
      :param existing: list of hosts taken from live Zabbix
      :return: list to be updated (combinated or empty list)
      '''
      try:
          defined_host_ids = set([host['hostid'] for host in defined])
          existing_host_ids = set([host['hostid'] for host in existing])
      except KeyError:
          raise SaltException('List of hosts in template not defined correctly.')
  
      diff = defined_host_ids - existing_host_ids
      return [{'hostid': six.text_type(hostid)} for hostid in diff | existing_host_ids] if diff else [f _diff_and_merge_host_list(defined, existing):
      '''
      If Zabbix template is to be updated then list of assigned hosts must be provided in all or nothing manner to prevent
      some externally assigned hosts to be detached.
  
      :param defined: list of hosts defined in sls
      :param existing: list of hosts taken from live Zabbix
      :return: list to be updated (combinated or empty list)
      '''
      try:
          defined_host_ids = set([host['hostid'] for host in defined])
          existing_host_ids = set([host['hostid'] for host in existing])
      except KeyError:
          raise SaltException('List of hosts in template not defined correctly.')
  
      diff = defined_host_ids - existing_host_ids
      return [{'hostid': six.text_type(hostid)} for hostid in diff | existing_host_ids] if diff else []
docs: If Zabbix template is to be updated then list of assigned hosts must be provided in all or nothing manner to prevent
      some externally assigned hosts to be detached.
  
      :param defined: list of hosts defined in sls
      :param existing: list of hosts taken from live Zabbix
      :return: list to be updated (combinated or empty list)