Repository: ansible/ansible

Issue:
Module zabbix_host is not idempotent
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report

##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
zabbix_host

##### ANSIBLE VERSION
```
ansible 2.3.0.0
  config file = /media/sf_ansible/ansible.cfg
  configured module search path = [u'./library']
  python version = 2.7.6 (default, Oct 26 2016, 20:30:19) [GCC 4.8.4]
```
Zabbix version 3.2.6

##### CONFIGURATION
<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->

##### OS / ENVIRONMENT
“N/A”

##### SUMMARY
When trying to register Zabbix host for the second time, module tries to again create the same default interface (despite it did not change), what results in an error, as only one default interface per type is allowed.

##### STEPS TO REPRODUCE
<!---
For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used.
-->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
- name: 'Register host in Zabbix'
  delegate_to: 'localhost'
  zabbix_host:
    server_url: '{{ zabbix_url }}'
    login_user: '{{ zabbix_api_username }}'
    login_password: '{{ zabbix_api_password }}'
    status: 'enabled'
    state: 'present'
    force: true
    host_name: 'test-host'
    inventory_mode: 'automatic'
    link_templates:
      - 'Template OS Linux'
    host_groups:
      - 'Linux servers'
    interfaces:
      - type: '1' # Agent
        main: '1'
        useip: '1'
        dns: ''
        port: '10050'
        ip: '127.0.0.1'
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
Playbook can be successfully run twice, without side effects.

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

Running the first time:
```
TASK [Register host in Zabbix] *********************************************************************************************************************

task path: /media/sf_ansible/zabbix-bug.yml:13
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/monitoring/zabbix_host.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: ubuntu
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python && sleep 0'
changed: [localhost] => {
    "changed": true,
    "invocation": {
        "module_args": {
            "force": true,
            "host_groups": [
                "Linux servers"
            ],
            "host_name": "test-host",
            "http_login_password": null,
            "http_login_user": null,
            "interfaces": [
                {
                    "dns": "",
                    "ip": "127.0.0.1",
                    "main": "1",
                    "port": "10050",
                    "type": "1",
                    "useip": "1"
                }
            ],
            "inventory_mode": "automatic",
            "link_templates": [
                "Template OS Linux"
            ],
            "login_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "login_user": "Admin",
            "proxy": null,
            "server_url": "https://*******************",
            "state": "present",
            "status": "enabled",
            "timeout": 10,
            "visible_name": null
        }
    },
    "result": "Successfully added host test-host () and linked with template '['Template OS Linux']'"
}
META: ran handlers
META: ran handlers
```

Running the second time (without modifying any code):
```
TASK [Register host in Zabbix] *********************************************************************************************************************

task path: /media/sf_ansible/zabbix-bug.yml:13
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/monitoring/zabbix_host.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: ubuntu
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python && sleep 0'
fatal: [localhost]: FAILED! => {
    "changed": false,
    "failed": true,
    "invocation": {
        "module_args": {
            "force": true,
            "host_groups": [
                "Linux servers"
            ],
            "host_name": "test-host",
            "http_login_password": null,
            "http_login_user": null,
            "interfaces": [
                {
                    "dns": "",
                    "hostid": "10116",
                    "ip": "127.0.0.1",
                    "main": "1",
                    "port": "10050",
                    "type": "1",
                    "useip": "1"
                }
            ],
            "inventory_mode": "automatic",
            "link_templates": [
                "Template OS Linux"
            ],
            "login_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "login_user": "Admin",
            "proxy": null,
            "server_url": "https://*******************",
            "state": "present",
            "status": "enabled",
            "timeout": 10,
            "visible_name": null
        }
    },
    "msg": "Failed to update host test-host: (u'Error -32602: Invalid params., Host cannot have more than one default interface of the same type. while sending {\"params\": {\"hostid\": \"10116\", \"ip\": \"127.0.0.1\", \"useip\": \"1\", \"dns\": \"\", \"main\": \"1\", \"type\": \"1\", \"port\": \"10050\"}, \"jsonrpc\": \"2.0\", \"method\": \"hostinterface.create\", \"auth\": \"e392dd877ecaa0cc66a90639323a7426\", \"id\": 10}', -32602)"
}
```

Module zabbix_host is not idempotent
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report

##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
zabbix_host

##### ANSIBLE VERSION
```
ansible 2.3.0.0
  config file = /media/sf_ansible/ansible.cfg
  configured module search path = [u'./library']
  python version = 2.7.6 (default, Oct 26 2016, 20:30:19) [GCC 4.8.4]
```
Zabbix version 3.2.6

##### CONFIGURATION
<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->

##### OS / ENVIRONMENT
“N/A”

##### SUMMARY
When trying to register Zabbix host for the second time, module tries to again create the same default interface (despite it did not change), what results in an error, as only one default interface per type is allowed.

##### STEPS TO REPRODUCE
<!---
For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used.
-->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
- name: 'Register host in Zabbix'
  delegate_to: 'localhost'
  zabbix_host:
    server_url: '{{ zabbix_url }}'
    login_user: '{{ zabbix_api_username }}'
    login_password: '{{ zabbix_api_password }}'
    status: 'enabled'
    state: 'present'
    force: true
    host_name: 'test-host'
    inventory_mode: 'automatic'
    link_templates:
      - 'Template OS Linux'
    host_groups:
      - 'Linux servers'
    interfaces:
      - type: '1' # Agent
        main: '1'
        useip: '1'
        dns: ''
        port: '10050'
        ip: '127.0.0.1'
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
Playbook can be successfully run twice, without side effects.

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

Running the first time:
```
TASK [Register host in Zabbix] *********************************************************************************************************************

task path: /media/sf_ansible/zabbix-bug.yml:13
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/monitoring/zabbix_host.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: ubuntu
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python && sleep 0'
changed: [localhost] => {
    "changed": true,
    "invocation": {
        "module_args": {
            "force": true,
            "host_groups": [
                "Linux servers"
            ],
            "host_name": "test-host",
            "http_login_password": null,
            "http_login_user": null,
            "interfaces": [
                {
                    "dns": "",
                    "ip": "127.0.0.1",
                    "main": "1",
                    "port": "10050",
                    "type": "1",
                    "useip": "1"
                }
            ],
            "inventory_mode": "automatic",
            "link_templates": [
                "Template OS Linux"
            ],
            "login_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "login_user": "Admin",
            "proxy": null,
            "server_url": "https://*******************",
            "state": "present",
            "status": "enabled",
            "timeout": 10,
            "visible_name": null
        }
    },
    "result": "Successfully added host test-host () and linked with template '['Template OS Linux']'"
}
META: ran handlers
META: ran handlers
```

Running the second time (without modifying any code):
```
TASK [Register host in Zabbix] *********************************************************************************************************************

task path: /media/sf_ansible/zabbix-bug.yml:13
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/monitoring/zabbix_host.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: ubuntu
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python && sleep 0'
fatal: [localhost]: FAILED! => {
    "changed": false,
    "failed": true,
    "invocation": {
        "module_args": {
            "force": true,
            "host_groups": [
                "Linux servers"
            ],
            "host_name": "test-host",
            "http_login_password": null,
            "http_login_user": null,
            "interfaces": [
                {
                    "dns": "",
                    "hostid": "10116",
                    "ip": "127.0.0.1",
                    "main": "1",
                    "port": "10050",
                    "type": "1",
                    "useip": "1"
                }
            ],
            "inventory_mode": "automatic",
            "link_templates": [
                "Template OS Linux"
            ],
            "login_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "login_user": "Admin",
            "proxy": null,
            "server_url": "https://*******************",
            "state": "present",
            "status": "enabled",
            "timeout": 10,
            "visible_name": null
        }
    },
    "msg": "Failed to update host test-host: (u'Error -32602: Invalid params., Host cannot have more than one default interface of the same type. while sending {\"params\": {\"hostid\": \"10116\", \"ip\": \"127.0.0.1\", \"useip\": \"1\", \"dns\": \"\", \"main\": \"1\", \"type\": \"1\", \"port\": \"10050\"}, \"jsonrpc\": \"2.0\", \"method\": \"hostinterface.create\", \"auth\": \"e392dd877ecaa0cc66a90639323a7426\", \"id\": 10}', -32602)"
}
```



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 --->
@klesniewski the error is the comparaison `string` vs `integer` here https://github.com/ansible/ansible/blob/07be5f2b67ff185b5f5fc911c17f5c1c5c1a4103/lib/ansible/modules/monitoring/zabbix_host.py#L248-L251. 

Fixed it with
```Python
for exist_interface in exist_interface_list:
                        interface_type = int(interface['type'])
                        exist_interface_type = int(exist_interface['type'])
                        if interface_type == exist_interface_type:
........
```
Fail allways and not going to `self._zapi.hostinterface.update(interface_str)` but `self._zapi.hostinterface.create(interface_str)`

I will send PR asap
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 --->
@klesniewski the error is the comparaison `string` vs `integer` here https://github.com/ansible/ansible/blob/07be5f2b67ff185b5f5fc911c17f5c1c5c1a4103/lib/ansible/modules/monitoring/zabbix_host.py#L248-L251. 

Fixed it with
```Python
for exist_interface in exist_interface_list:
                        interface_type = int(interface['type'])
                        exist_interface_type = int(exist_interface['type'])
                        if interface_type == exist_interface_type:
........
```
Fail allways and not going to `self._zapi.hostinterface.update(interface_str)` but `self._zapi.hostinterface.create(interface_str)`

I will send PR asap

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
@@ -331,7 +331,7 @@ def update_host(self, host_name, group_ids, status, host_id, interfaces, exist_i
                     flag = False
                     interface_str = interface
                     for exist_interface in exist_interface_list:
-                        interface_type = interface['type']
+                        interface_type = int(interface['type'])
                         exist_interface_type = int(exist_interface['type'])
                         if interface_type == exist_interface_type:
                             # update


Test patch: