zabbix_maintenance fixes and parameter names update
This commit is contained in:
parent
edebadc305
commit
ccf031f1c5
1 changed files with 51 additions and 50 deletions
|
@ -25,7 +25,7 @@ module: zabbix_maintenance
|
||||||
short_description: Create Zabbix maintenance windows
|
short_description: Create Zabbix maintenance windows
|
||||||
description:
|
description:
|
||||||
- This module will let you create Zabbix maintenance windows.
|
- This module will let you create Zabbix maintenance windows.
|
||||||
version_added: "1.5"
|
version_added: "1.6"
|
||||||
author: Alexander Bulimov
|
author: Alexander Bulimov
|
||||||
requirements:
|
requirements:
|
||||||
- zabbix-api python module
|
- zabbix-api python module
|
||||||
|
@ -36,38 +36,39 @@ options:
|
||||||
required: true
|
required: true
|
||||||
default: null
|
default: null
|
||||||
choices: [ "present", "absent" ]
|
choices: [ "present", "absent" ]
|
||||||
server:
|
server_url:
|
||||||
description:
|
description:
|
||||||
- Url of Zabbix server, with schema (http or https).
|
- Url of Zabbix server, with protocol (http or https).
|
||||||
|
C(url) is an alias for C(server_url).
|
||||||
required: true
|
required: true
|
||||||
default: null
|
default: null
|
||||||
aliases: [ "url" ]
|
aliases: [ "url" ]
|
||||||
user:
|
login_user:
|
||||||
description:
|
description:
|
||||||
- Zabbix user name.
|
- Zabbix user name.
|
||||||
required: true
|
required: true
|
||||||
default: null
|
default: null
|
||||||
passwd:
|
login_password:
|
||||||
description:
|
description:
|
||||||
- Zabbix user password.
|
- Zabbix user password.
|
||||||
required: true
|
required: true
|
||||||
default: null
|
default: null
|
||||||
hosts:
|
host_names:
|
||||||
description:
|
description:
|
||||||
- Hosts to manage maintenance window for. Separate multiple hosts with commas.
|
- Hosts to manage maintenance window for. Separate multiple hosts with commas.
|
||||||
C(host) is an alias for C(hosts).
|
C(host_name) is an alias for C(host_names).
|
||||||
B(Required) option when C(state) is I(present) and no C(groups) specified.
|
B(Required) option when C(state) is I(present) and no C(host_groups) specified.
|
||||||
required: false
|
required: false
|
||||||
default: null
|
default: null
|
||||||
aliases: [ "host" ]
|
aliases: [ "host_name" ]
|
||||||
groups:
|
host_groups:
|
||||||
description:
|
description:
|
||||||
- Host groups to manage maintenance window for. Separate multiple groups with commas.
|
- Host groups to manage maintenance window for. Separate multiple groups with commas.
|
||||||
C(group) is an alias for C(groups).
|
C(host_group) is an alias for C(host_groups).
|
||||||
B(Required) option when C(state) is I(present) and no C(hosts) specified.
|
B(Required) option when C(state) is I(present) and no C(host_names) specified.
|
||||||
required: false
|
required: false
|
||||||
default: null
|
default: null
|
||||||
aliases: [ "group" ]
|
aliases: [ "host_group" ]
|
||||||
minutes:
|
minutes:
|
||||||
description:
|
description:
|
||||||
- Length of maintenance window in minutes.
|
- Length of maintenance window in minutes.
|
||||||
|
@ -102,39 +103,39 @@ EXAMPLES = '''
|
||||||
# Create maintenance window named "Update of www1"
|
# Create maintenance window named "Update of www1"
|
||||||
# for host www1.example.com for 90 minutes
|
# for host www1.example.com for 90 minutes
|
||||||
- zabbix_maintenance: name="Update of www1"
|
- zabbix_maintenance: name="Update of www1"
|
||||||
host=www1.example.com
|
host_name=www1.example.com
|
||||||
state=present
|
state=present
|
||||||
minutes=90
|
minutes=90
|
||||||
server=https://monitoring.example.com
|
server_url=https://monitoring.example.com
|
||||||
user=ansible
|
login_user=ansible
|
||||||
passwd=pAsSwOrD
|
login_password=pAsSwOrD
|
||||||
|
|
||||||
# Create maintenance window named "Mass update"
|
# Create maintenance window named "Mass update"
|
||||||
# for host www1.example.com and host groups Office and Dev
|
# for host www1.example.com and host groups Office and Dev
|
||||||
- zabbix_maintenance: name="Update of www1"
|
- zabbix_maintenance: name="Update of www1"
|
||||||
host=www1.example.com
|
host_name=www1.example.com
|
||||||
group=Office,Dev
|
host_groups=Office,Dev
|
||||||
state=present
|
state=present
|
||||||
server=https://monitoring.example.com
|
server_url=https://monitoring.example.com
|
||||||
user=ansible
|
login_user=ansible
|
||||||
passwd=pAsSwOrD
|
login_password=pAsSwOrD
|
||||||
|
|
||||||
# Create maintenance window named "update"
|
# Create maintenance window named "update"
|
||||||
# for host www1.example.com and without data collection.
|
# for hosts www1.example.com and db1.example.com and without data collection.
|
||||||
- zabbix_maintenance: name=update
|
- zabbix_maintenance: name=update
|
||||||
host=www1.example.com
|
host_names=www1.example.com,db1.example.com
|
||||||
state=present
|
state=present
|
||||||
collect_data=false
|
collect_data=false
|
||||||
server=https://monitoring.example.com
|
server_url=https://monitoring.example.com
|
||||||
user=ansible
|
login_user=ansible
|
||||||
passwd=pAsSwOrD
|
login_password=pAsSwOrD
|
||||||
|
|
||||||
# Remove maintenance window named "Test1"
|
# Remove maintenance window named "Test1"
|
||||||
- zabbix_maintenance: name=Test1
|
- zabbix_maintenance: name=Test1
|
||||||
state=absent
|
state=absent
|
||||||
server=https://monitoring.example.com
|
server_url=https://monitoring.example.com
|
||||||
user=ansible
|
login_user=ansible
|
||||||
passwd=pAsSwOrD
|
login_password=pAsSwOrD
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import datetime, time
|
import datetime, time
|
||||||
|
@ -205,9 +206,9 @@ def check_maintenance(zbx, name):
|
||||||
return 1, None, str(e)
|
return 1, None, str(e)
|
||||||
return 0, result, None
|
return 0, result, None
|
||||||
|
|
||||||
def get_group_ids(zbx, group_names):
|
def get_group_ids(zbx, host_groups):
|
||||||
group_ids = []
|
group_ids = []
|
||||||
for group in group_names:
|
for group in host_groups:
|
||||||
try:
|
try:
|
||||||
result = zbx.hostgroup.get(
|
result = zbx.hostgroup.get(
|
||||||
{
|
{
|
||||||
|
@ -256,12 +257,12 @@ def main():
|
||||||
module = AnsibleModule(
|
module = AnsibleModule(
|
||||||
argument_spec=dict(
|
argument_spec=dict(
|
||||||
state=dict(required=True, default=None, choices=['present', 'absent']),
|
state=dict(required=True, default=None, choices=['present', 'absent']),
|
||||||
server=dict(required=True, default=None, aliases=['url']),
|
server_url=dict(required=True, default=None, aliases=['url']),
|
||||||
hosts=dict(type='list', required=False, default=None, aliases=['host']),
|
host_names=dict(type='list', required=False, default=None, aliases=['host_name']),
|
||||||
minutes=dict(type='int', required=False, default=10),
|
minutes=dict(type='int', required=False, default=10),
|
||||||
groups=dict(type='list', required=False, default=None, aliases=['group']),
|
host_groups=dict(type='list', required=False, default=None, aliases=['host_group']),
|
||||||
user=dict(required=True, default=None),
|
login_user=dict(required=True, default=None),
|
||||||
passwd=dict(required=True, default=None),
|
login_password=dict(required=True, default=None),
|
||||||
name=dict(required=True, default=None),
|
name=dict(required=True, default=None),
|
||||||
desc=dict(required=False, default="Created by Ansible"),
|
desc=dict(required=False, default="Created by Ansible"),
|
||||||
collect_data=dict(type='bool', required=False, default=True),
|
collect_data=dict(type='bool', required=False, default=True),
|
||||||
|
@ -272,15 +273,15 @@ def main():
|
||||||
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)")
|
||||||
|
|
||||||
hosts = module.params['hosts']
|
host_names = module.params['host_names']
|
||||||
groups = module.params['groups']
|
host_groups = module.params['host_groups']
|
||||||
state = module.params['state']
|
state = module.params['state']
|
||||||
user = module.params['user']
|
login_user = module.params['login_user']
|
||||||
passwd = module.params['passwd']
|
login_password = module.params['login_password']
|
||||||
minutes = module.params['minutes']
|
minutes = module.params['minutes']
|
||||||
name = module.params['name']
|
name = module.params['name']
|
||||||
desc = module.params['desc']
|
desc = module.params['desc']
|
||||||
server = module.params['server']
|
server_url = module.params['server_url']
|
||||||
collect_data = module.params['collect_data']
|
collect_data = module.params['collect_data']
|
||||||
if collect_data:
|
if collect_data:
|
||||||
maintenance_type = 0
|
maintenance_type = 0
|
||||||
|
@ -289,8 +290,8 @@ def main():
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
zbx = ZabbixAPI(server)
|
zbx = ZabbixAPI(server_url)
|
||||||
zbx.login(user, passwd)
|
zbx.login(login_user, login_password)
|
||||||
except BaseException as e:
|
except BaseException as 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)
|
||||||
|
|
||||||
|
@ -302,15 +303,15 @@ def main():
|
||||||
start_time = time.mktime(now.timetuple())
|
start_time = time.mktime(now.timetuple())
|
||||||
period = 60 * int(minutes) #N * 60 seconds
|
period = 60 * int(minutes) #N * 60 seconds
|
||||||
|
|
||||||
if groups:
|
if host_groups:
|
||||||
(rc, group_ids, error) = get_group_ids(zbx, groups)
|
(rc, group_ids, error) = get_group_ids(zbx, host_groups)
|
||||||
if rc != 0:
|
if rc != 0:
|
||||||
module.fail_json(msg="Failed to get group_ids: %s"%error)
|
module.fail_json(msg="Failed to get group_ids: %s"%error)
|
||||||
else:
|
else:
|
||||||
group_ids = []
|
group_ids = []
|
||||||
|
|
||||||
if hosts:
|
if host_names:
|
||||||
(rc, host_ids, error) = get_host_ids(zbx, hosts)
|
(rc, host_ids, error) = get_host_ids(zbx, host_names)
|
||||||
if rc != 0:
|
if rc != 0:
|
||||||
module.fail_json(msg="Failed to get host_ids: %s"%error)
|
module.fail_json(msg="Failed to get host_ids: %s"%error)
|
||||||
else:
|
else:
|
||||||
|
@ -321,8 +322,8 @@ def main():
|
||||||
module.fail_json(msg="Failed to check maintenance %s existance: %s"%(name, error))
|
module.fail_json(msg="Failed to check maintenance %s existance: %s"%(name, error))
|
||||||
|
|
||||||
if not exists:
|
if not exists:
|
||||||
if not hosts and not groups:
|
if not host_names and not host_groups:
|
||||||
module.fail_json(msg="At least one host or host group must be defined for each created maintenance.")
|
module.fail_json(msg="At least one host_name or host_group must be defined for each created maintenance.")
|
||||||
|
|
||||||
if module.check_mode:
|
if module.check_mode:
|
||||||
changed = True
|
changed = True
|
||||||
|
|
Loading…
Reference in a new issue