Repository: ansible/ansible

Issue:
zabbix_host: cannot set host from being monitored by a proxy to being monitored by the server
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
zabbix_host

##### ANSIBLE VERSION
```
ansible 2.3.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.5 (default, Nov  6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
```

##### CONFIGURATION
I think everything is default.

##### OS / ENVIRONMENT
Running ansible on Centos 7, and managing Centos 6/7.

##### SUMMARY
Suppose you have a Zabbix host that is being monitored by a proxy and you want to update it so that it is monitored by the server instead of a proxy. If run an ansible playbook using the `zabbix_host` module, and you set `force: yes` and `proxy: null`, the resulting Zabbix host will still be monitored by the proxy.

##### STEPS TO REPRODUCE
Run the following playbook.
```yaml
---
- hosts: localhost
  vars:
    zabbix_hostname: "test.example.com"
    zabbix_proxy: "my-zabbix-proxy"
  tasks:
    - name: Create Zabbix host
      zabbix_host:
        host_name: "{{ zabbix_hostname }}"
        host_groups:
          - "Linux servers"
        link_templates:
          - "Template OS Linux"
        interfaces:
          - type: 1
            main: 1
            useip: 0
            ip: ""
            dns: "{{ zabbix_hostname }}"
            port: 10050
        proxy: "{{ zabbix_proxy }}"
        force: yes
        login_user: "Admin"
        login_password: "zabbix"
        server_url: "http://myzabbixinstance.com"
```
So this successfully creates a Zabbix host that is being monitored by `my-zabbix-proxy` (as expected). 
Now, rerun the playbook after setting the variable `zabbix_proxy: null`. 

##### EXPECTED RESULTS
The expected result is that it should be monitored by `(no proxy)`, ie. the Zabbix server.

##### ACTUAL RESULTS
The Zabbix host is still being monitored by `my-zabbix-proxy`. 


zabbix_host: cannot set host from being monitored by a proxy to being monitored by the server
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
zabbix_host

##### ANSIBLE VERSION
```
ansible 2.3.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.5 (default, Nov  6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
```

##### CONFIGURATION
I think everything is default.

##### OS / ENVIRONMENT
Running ansible on Centos 7, and managing Centos 6/7.

##### SUMMARY
Suppose you have a Zabbix host that is being monitored by a proxy and you want to update it so that it is monitored by the server instead of a proxy. If run an ansible playbook using the `zabbix_host` module, and you set `force: yes` and `proxy: null`, the resulting Zabbix host will still be monitored by the proxy.

##### STEPS TO REPRODUCE
Run the following playbook.
```yaml
---
- hosts: localhost
  vars:
    zabbix_hostname: "test.example.com"
    zabbix_proxy: "my-zabbix-proxy"
  tasks:
    - name: Create Zabbix host
      zabbix_host:
        host_name: "{{ zabbix_hostname }}"
        host_groups:
          - "Linux servers"
        link_templates:
          - "Template OS Linux"
        interfaces:
          - type: 1
            main: 1
            useip: 0
            ip: ""
            dns: "{{ zabbix_hostname }}"
            port: 10050
        proxy: "{{ zabbix_proxy }}"
        force: yes
        login_user: "Admin"
        login_password: "zabbix"
        server_url: "http://myzabbixinstance.com"
```
So this successfully creates a Zabbix host that is being monitored by `my-zabbix-proxy` (as expected). 
Now, rerun the playbook after setting the variable `zabbix_proxy: null`. 

##### EXPECTED RESULTS
The expected result is that it should be monitored by `(no proxy)`, ie. the Zabbix server.

##### ACTUAL RESULTS
The Zabbix host is still being monitored by `my-zabbix-proxy`. 




Discussion:
cc @eikef
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
cc @cove @harrisongu
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
cc @D3DeFi
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
I also tried removing the `proxy` field instead of setting it to `null` so that it would use the default value (according in [documentation](http://docs.ansible.com/ansible/latest/zabbix_host_module.html), the default is None). Still does not work.
cc @eikef
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
cc @cove @harrisongu
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
cc @D3DeFi
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
I also tried removing the `proxy` field instead of setting it to `null` so that it would use the default value (according in [documentation](http://docs.ansible.com/ansible/latest/zabbix_host_module.html), the default is None). Still does not work.

Resolving patch:
diff --git a/lib/ansible/modules/monitoring/zabbix_host.py b/lib/ansible/modules/monitoring/zabbix_host.py
--- a/lib/ansible/modules/monitoring/zabbix_host.py
+++ b/lib/ansible/modules/monitoring/zabbix_host.py
@@ -300,7 +300,7 @@ def update_host(self, host_name, group_ids, status, host_id, interfaces, exist_i
                 self._module.exit_json(changed=True)
             parameters = {'hostid': host_id, 'groups': group_ids, 'status': status, 'tls_connect': tls_connect,
                           'tls_accept': tls_accept}
-            if proxy_id:
+            if proxy_id >= 0:
                 parameters['proxy_hostid'] = proxy_id
             if visible_name:
                 parameters['name'] = visible_name
@@ -448,7 +448,7 @@ def check_all_properties(self, host_id, host_groups, status, interfaces, templat
         if set(list(template_ids)) != set(exist_template_ids):
             return True
 
-        if host['proxy_hostid'] != proxy_id:
+        if int(host['proxy_hostid']) != int(proxy_id):
             return True
 
         if host['name'] != visible_name:
@@ -602,7 +602,7 @@ def main():
         if proxy:
             proxy_id = host.get_proxyid_by_proxy_name(proxy)
         else:
-            proxy_id = None
+            proxy_id = 0
 
         # get host id by host name
         zabbix_host_obj = host.get_host_by_host_name(host_name)


Test patch: