|
|
@ -1,7 +1,7 @@
|
|
|
|
#!/usr/bin/python
|
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
#
|
|
|
|
# (c) 2014, René Moser <mail@renemoser.net>
|
|
|
|
# (c) 2013-2014, Epic Games, Inc.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
#
|
|
|
@ -22,193 +22,189 @@
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
---
|
|
|
|
module: zabbix_group
|
|
|
|
module: zabbix_group
|
|
|
|
short_description: Add or remove a host group to Zabbix.
|
|
|
|
short_description: Zabbix host groups creates/deletes
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- This module uses the Zabbix API to add and remove host groups.
|
|
|
|
- Create host groups if they don't exist.
|
|
|
|
version_added: '1.8'
|
|
|
|
- Delete existing host groups if they exist.
|
|
|
|
|
|
|
|
version_added: "1.8"
|
|
|
|
|
|
|
|
author:
|
|
|
|
|
|
|
|
- "(@cove)"
|
|
|
|
|
|
|
|
- "Tony Minfei Ding"
|
|
|
|
|
|
|
|
- "Harrison Gu (@harrisongu)"
|
|
|
|
requirements:
|
|
|
|
requirements:
|
|
|
|
- "python >= 2.6"
|
|
|
|
- "python >= 2.6"
|
|
|
|
- zabbix-api
|
|
|
|
- zabbix-api
|
|
|
|
options:
|
|
|
|
options:
|
|
|
|
state:
|
|
|
|
|
|
|
|
description:
|
|
|
|
|
|
|
|
- Whether the host group should be added or removed.
|
|
|
|
|
|
|
|
required: false
|
|
|
|
|
|
|
|
default: present
|
|
|
|
|
|
|
|
choices: [ 'present', 'absent' ]
|
|
|
|
|
|
|
|
host_group:
|
|
|
|
|
|
|
|
description:
|
|
|
|
|
|
|
|
- Name of the host group to be added or removed.
|
|
|
|
|
|
|
|
required: true
|
|
|
|
|
|
|
|
default: null
|
|
|
|
|
|
|
|
aliases: [ ]
|
|
|
|
|
|
|
|
server_url:
|
|
|
|
server_url:
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- Url of Zabbix server, with protocol (http or https) e.g.
|
|
|
|
- Url of Zabbix server, with protocol (http or https).
|
|
|
|
https://monitoring.example.com/zabbix. C(url) is an alias
|
|
|
|
C(url) is an alias for C(server_url).
|
|
|
|
for C(server_url). If not set environment variable
|
|
|
|
|
|
|
|
C(ZABBIX_SERVER_URL) is used.
|
|
|
|
|
|
|
|
required: true
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
aliases: [ "url" ]
|
|
|
|
aliases: [ 'url' ]
|
|
|
|
|
|
|
|
login_user:
|
|
|
|
login_user:
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- Zabbix user name. If not set environment variable
|
|
|
|
- Zabbix user name.
|
|
|
|
C(ZABBIX_LOGIN_USER) is used.
|
|
|
|
|
|
|
|
required: true
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
|
|
|
|
login_password:
|
|
|
|
login_password:
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- Zabbix user password. If not set environment variable
|
|
|
|
- Zabbix user password.
|
|
|
|
C(ZABBIX_LOGIN_PASSWORD) is used.
|
|
|
|
|
|
|
|
required: true
|
|
|
|
required: true
|
|
|
|
|
|
|
|
state:
|
|
|
|
|
|
|
|
description:
|
|
|
|
|
|
|
|
- Create or delete host group.
|
|
|
|
|
|
|
|
- Possible values are: present and absent.
|
|
|
|
|
|
|
|
required: false
|
|
|
|
|
|
|
|
default: "present"
|
|
|
|
|
|
|
|
choices: [ "present", "absent" ]
|
|
|
|
|
|
|
|
timeout:
|
|
|
|
|
|
|
|
description:
|
|
|
|
|
|
|
|
- The timeout of API request(seconds).
|
|
|
|
|
|
|
|
default: 10
|
|
|
|
|
|
|
|
host_groups:
|
|
|
|
|
|
|
|
description:
|
|
|
|
|
|
|
|
- List of host groups to create or delete.
|
|
|
|
|
|
|
|
required: true
|
|
|
|
|
|
|
|
aliases: [ "host_group" ]
|
|
|
|
notes:
|
|
|
|
notes:
|
|
|
|
- The module has been tested with Zabbix Server 2.2.
|
|
|
|
- Too many concurrent updates to the same group may cause Zabbix to return errors, see examples for a workaround if needed.
|
|
|
|
author: '"René Moser (@resmo)" <mail@renemoser.net>'
|
|
|
|
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
EXAMPLES = '''
|
|
|
|
---
|
|
|
|
# Base create host groups example
|
|
|
|
# Add a new host group to Zabbix
|
|
|
|
- name: Create host groups
|
|
|
|
- zabbix_group: host_group='Linux servers'
|
|
|
|
local_action:
|
|
|
|
server_url=https://monitoring.example.com/zabbix
|
|
|
|
module: zabbix_group
|
|
|
|
login_user=ansible
|
|
|
|
server_url: http://monitor.example.com
|
|
|
|
login_password=secure
|
|
|
|
login_user: username
|
|
|
|
|
|
|
|
login_password: password
|
|
|
|
# Add a new host group, login data is provided by environment variables:
|
|
|
|
state: present
|
|
|
|
# ZABBIX_LOGIN_USER, ZABBIX_LOGIN_PASSWORD, ZABBIX_SERVER_URL:
|
|
|
|
host_groups:
|
|
|
|
- zabbix_group: host_group=Webservers
|
|
|
|
- Example group1
|
|
|
|
|
|
|
|
- Example group2
|
|
|
|
# Remove a host group from Zabbix
|
|
|
|
|
|
|
|
- zabbix_group: host_group='Linux servers'
|
|
|
|
# Limit the Zabbix group creations to one host since Zabbix can return an error when doing concurent updates
|
|
|
|
state=absent
|
|
|
|
- name: Create host groups
|
|
|
|
server_url=https://monitoring.example.com/zabbix
|
|
|
|
local_action:
|
|
|
|
login_user=ansible
|
|
|
|
module: zabbix_group
|
|
|
|
login_password=secure
|
|
|
|
server_url: http://monitor.example.com
|
|
|
|
|
|
|
|
login_user: username
|
|
|
|
|
|
|
|
login_password: password
|
|
|
|
|
|
|
|
state: present
|
|
|
|
|
|
|
|
host_groups:
|
|
|
|
|
|
|
|
- Example group1
|
|
|
|
|
|
|
|
- Example group2
|
|
|
|
|
|
|
|
when: inventory_hostname==groups['group_name'][0]
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
from zabbix_api import ZabbixAPI
|
|
|
|
from zabbix_api import ZabbixAPI, ZabbixAPISubClass
|
|
|
|
|
|
|
|
from zabbix_api import Already_Exists
|
|
|
|
|
|
|
|
|
|
|
|
HAS_ZABBIX_API = True
|
|
|
|
HAS_ZABBIX_API = True
|
|
|
|
except ImportError:
|
|
|
|
except ImportError:
|
|
|
|
HAS_ZABBIX_API = False
|
|
|
|
HAS_ZABBIX_API = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_group(zbx, host_group):
|
|
|
|
class HostGroup(object):
|
|
|
|
try:
|
|
|
|
def __init__(self, module, zbx):
|
|
|
|
result = zbx.hostgroup.create(
|
|
|
|
self._module = module
|
|
|
|
{
|
|
|
|
self._zapi = zbx
|
|
|
|
'name': host_group
|
|
|
|
|
|
|
|
}
|
|
|
|
# create host group(s) if not exists
|
|
|
|
)
|
|
|
|
def create_host_group(self, group_names):
|
|
|
|
except BaseException as e:
|
|
|
|
try:
|
|
|
|
return 1, None, str(e)
|
|
|
|
group_add_list = []
|
|
|
|
return 0, result['groupids'], None
|
|
|
|
for group_name in group_names:
|
|
|
|
|
|
|
|
result = self._zapi.hostgroup.exists({'name': group_name})
|
|
|
|
|
|
|
|
if not result:
|
|
|
|
def get_group(zbx, host_group):
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
if self._module.check_mode:
|
|
|
|
result = zbx.hostgroup.get(
|
|
|
|
self._module.exit_json(changed=True)
|
|
|
|
{
|
|
|
|
self._zapi.hostgroup.create({'name': group_name})
|
|
|
|
'filter':
|
|
|
|
group_add_list.append(group_name)
|
|
|
|
{
|
|
|
|
except Already_Exists:
|
|
|
|
'name': host_group,
|
|
|
|
return group_add_list
|
|
|
|
}
|
|
|
|
return group_add_list
|
|
|
|
}
|
|
|
|
except Exception, e:
|
|
|
|
)
|
|
|
|
self._module.fail_json(msg="Failed to create host group(s): %s" % e)
|
|
|
|
except BaseException as e:
|
|
|
|
|
|
|
|
return 1, None, str(e)
|
|
|
|
# delete host group(s)
|
|
|
|
|
|
|
|
def delete_host_group(self, group_ids):
|
|
|
|
return 0, result[0]['groupid'], None
|
|
|
|
try:
|
|
|
|
|
|
|
|
if self._module.check_mode:
|
|
|
|
|
|
|
|
self._module.exit_json(changed=True)
|
|
|
|
def delete_group(zbx, group_id):
|
|
|
|
self._zapi.hostgroup.delete(group_ids)
|
|
|
|
try:
|
|
|
|
except Exception, e:
|
|
|
|
zbx.hostgroup.delete([ group_id ])
|
|
|
|
self._module.fail_json(msg="Failed to delete host group(s), Exception: %s" % e)
|
|
|
|
except BaseException as e:
|
|
|
|
|
|
|
|
return 1, None, str(e)
|
|
|
|
# get group ids by name
|
|
|
|
return 0, None, None
|
|
|
|
def get_group_ids(self, host_groups):
|
|
|
|
|
|
|
|
group_ids = []
|
|
|
|
|
|
|
|
|
|
|
|
def check_group(zbx, host_group):
|
|
|
|
group_list = self._zapi.hostgroup.get({'output': 'extend', 'filter': {'name': host_groups}})
|
|
|
|
try:
|
|
|
|
for group in group_list:
|
|
|
|
result = zbx.hostgroup.exists(
|
|
|
|
group_id = group['groupid']
|
|
|
|
{
|
|
|
|
group_ids.append(group_id)
|
|
|
|
'name': host_group
|
|
|
|
return group_ids, group_list
|
|
|
|
}
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
except BaseException as e:
|
|
|
|
|
|
|
|
return 1, None, str(e)
|
|
|
|
|
|
|
|
return 0, result, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
argument_spec=dict(
|
|
|
|
state=dict(default='present', choices=['present', 'absent']),
|
|
|
|
server_url=dict(required=True, aliases=['url']),
|
|
|
|
host_group=dict(required=True, default=None),
|
|
|
|
login_user=dict(required=True),
|
|
|
|
server_url=dict(default=None, aliases=['url']),
|
|
|
|
login_password=dict(required=True, no_log=True),
|
|
|
|
login_user=dict(default=None),
|
|
|
|
host_groups=dict(required=True, aliases=['host_group']),
|
|
|
|
login_password=dict(default=None),
|
|
|
|
state=dict(default="present", choices=['present','absent']),
|
|
|
|
|
|
|
|
timeout=dict(type='int', default=10)
|
|
|
|
),
|
|
|
|
),
|
|
|
|
supports_check_mode=True,
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if not HAS_ZABBIX_API:
|
|
|
|
if not HAS_ZABBIX_API:
|
|
|
|
module.fail_json(msg='Missing requried zabbix-api module (check docs or install with: pip install zabbix-api)')
|
|
|
|
module.fail_json(msg="Missing requried zabbix-api module (check docs or install with: pip install zabbix-api)")
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
server_url = module.params['server_url']
|
|
|
|
login_user = module.params['login_user'] or os.environ['ZABBIX_LOGIN_USER']
|
|
|
|
login_user = module.params['login_user']
|
|
|
|
login_password = module.params['login_password'] or os.environ['ZABBIX_LOGIN_PASSWORD']
|
|
|
|
login_password = module.params['login_password']
|
|
|
|
server_url = module.params['server_url'] or os.environ['ZABBIX_SERVER_URL']
|
|
|
|
host_groups = module.params['host_groups']
|
|
|
|
except KeyError, e:
|
|
|
|
|
|
|
|
module.fail_json(msg='Missing login data: %s is not set.' % e.message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
host_group = module.params['host_group']
|
|
|
|
|
|
|
|
state = module.params['state']
|
|
|
|
state = module.params['state']
|
|
|
|
|
|
|
|
timeout = module.params['timeout']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
zbx = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# login to zabbix
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
zbx = ZabbixAPI(server_url)
|
|
|
|
zbx = ZabbixAPI(server_url, timeout=timeout)
|
|
|
|
zbx.login(login_user, login_password)
|
|
|
|
zbx.login(login_user, login_password)
|
|
|
|
except BaseException as e:
|
|
|
|
except Exception, e:
|
|
|
|
module.fail_json(msg='Failed to connect to Zabbix server: %s' % e)
|
|
|
|
module.fail_json(msg="Failed to connect to Zabbix server: %s" % e)
|
|
|
|
|
|
|
|
|
|
|
|
changed = False
|
|
|
|
hostGroup = HostGroup(module, zbx)
|
|
|
|
msg = ''
|
|
|
|
|
|
|
|
|
|
|
|
group_ids = []
|
|
|
|
if state == 'present':
|
|
|
|
group_list = []
|
|
|
|
(rc, exists, error) = check_group(zbx, host_group)
|
|
|
|
if host_groups:
|
|
|
|
if rc != 0:
|
|
|
|
group_ids, group_list = hostGroup.get_group_ids(host_groups)
|
|
|
|
module.fail_json(msg='Failed to check host group %s existance: %s' % (host_group, error))
|
|
|
|
|
|
|
|
if not exists:
|
|
|
|
if state == "absent":
|
|
|
|
if module.check_mode:
|
|
|
|
# delete host groups
|
|
|
|
changed = True
|
|
|
|
if group_ids:
|
|
|
|
else:
|
|
|
|
delete_group_names = []
|
|
|
|
(rc, group, error) = create_group(zbx, host_group)
|
|
|
|
hostGroup.delete_host_group(group_ids)
|
|
|
|
if rc == 0:
|
|
|
|
for group in group_list:
|
|
|
|
changed = True
|
|
|
|
delete_group_names.append(group['name'])
|
|
|
|
else:
|
|
|
|
module.exit_json(changed=True,
|
|
|
|
module.fail_json(msg='Failed to get host group: %s' % error)
|
|
|
|
result="Successfully deleted host group(s): %s." % ",".join(delete_group_names))
|
|
|
|
|
|
|
|
else:
|
|
|
|
if state == 'absent':
|
|
|
|
module.exit_json(changed=False, result="No host group(s) to delete.")
|
|
|
|
(rc, exists, error) = check_group(zbx, host_group)
|
|
|
|
else:
|
|
|
|
if rc != 0:
|
|
|
|
# create host groups
|
|
|
|
module.fail_json(msg='Failed to check host group %s existance: %s' % (host_group, error))
|
|
|
|
group_add_list = hostGroup.create_host_group(host_groups)
|
|
|
|
if exists:
|
|
|
|
if len(group_add_list) > 0:
|
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True, result="Successfully created host group(s): %s" % group_add_list)
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
module.exit_json(changed=False)
|
|
|
|
(rc, group_id, error) = get_group(zbx, host_group)
|
|
|
|
|
|
|
|
if rc != 0:
|
|
|
|
|
|
|
|
module.fail_json(msg='Failed to get host group: %s' % error)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(rc, _, error) = delete_group(zbx, group_id)
|
|
|
|
|
|
|
|
if rc == 0:
|
|
|
|
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
module.fail_json(msg='Failed to remove host group: %s' % error)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exit_json(changed=changed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
main()
|
|
|
|
main()
|
|
|
|