@ -64,6 +64,11 @@ options:
- Visible name of the host in Zabbix .
- Visible name of the host in Zabbix .
required : false
required : false
version_added : ' 2.3 '
version_added : ' 2.3 '
description :
description :
- Description of the host in Zabbix .
required : false
version_added : ' 2.5 '
host_groups :
host_groups :
description :
description :
- List of host groups the host is part of .
- List of host groups the host is part of .
@ -172,6 +177,7 @@ EXAMPLES = '''
login_password : password
login_password : password
host_name : ExampleHost
host_name : ExampleHost
visible_name : ExampleName
visible_name : ExampleName
description : My ExampleHost Description
host_groups :
host_groups :
- Example group1
- Example group1
- Example group2
- Example group2
@ -268,7 +274,7 @@ class Host(object):
template_ids . append ( template_id )
template_ids . append ( template_id )
return template_ids
return template_ids
def add_host ( self , host_name , group_ids , status , interfaces , proxy_id , visible_name , tls_connect,
def add_host ( self , host_name , group_ids , status , interfaces , proxy_id , visible_name , description, tls_connect,
tls_accept , tls_psk_identity , tls_psk , tls_issuer , tls_subject ) :
tls_accept , tls_psk_identity , tls_psk , tls_issuer , tls_subject ) :
try :
try :
if self . _module . check_mode :
if self . _module . check_mode :
@ -287,6 +293,9 @@ class Host(object):
parameters [ ' tls_issuer ' ] = tls_issuer
parameters [ ' tls_issuer ' ] = tls_issuer
if tls_subject :
if tls_subject :
parameters [ ' tls_subject ' ] = tls_subject
parameters [ ' tls_subject ' ] = tls_subject
if description :
parameters [ ' description ' ] = description
host_list = self . _zapi . host . create ( parameters )
host_list = self . _zapi . host . create ( parameters )
if len ( host_list ) > = 1 :
if len ( host_list ) > = 1 :
return host_list [ ' hostids ' ] [ 0 ]
return host_list [ ' hostids ' ] [ 0 ]
@ -294,7 +303,7 @@ class Host(object):
self . _module . fail_json ( msg = " Failed to create host %s : %s " % ( host_name , e ) )
self . _module . fail_json ( msg = " Failed to create host %s : %s " % ( host_name , e ) )
def update_host ( self , host_name , group_ids , status , host_id , interfaces , exist_interface_list , proxy_id ,
def update_host ( self , host_name , group_ids , status , host_id , interfaces , exist_interface_list , proxy_id ,
visible_name , tls_connect, tls_accept , tls_psk_identity , tls_psk , tls_issuer , tls_subject ) :
visible_name , description, tls_connect, tls_accept , tls_psk_identity , tls_psk , tls_issuer , tls_subject ) :
try :
try :
if self . _module . check_mode :
if self . _module . check_mode :
self . _module . exit_json ( changed = True )
self . _module . exit_json ( changed = True )
@ -312,6 +321,9 @@ class Host(object):
parameters [ ' tls_issuer ' ] = tls_issuer
parameters [ ' tls_issuer ' ] = tls_issuer
if tls_subject :
if tls_subject :
parameters [ ' tls_subject ' ] = tls_subject
parameters [ ' tls_subject ' ] = tls_subject
if description :
parameters [ ' description ' ] = description
self . _zapi . host . update ( parameters )
self . _zapi . host . update ( parameters )
interface_list_copy = exist_interface_list
interface_list_copy = exist_interface_list
if interfaces :
if interfaces :
@ -428,7 +440,7 @@ class Host(object):
# check all the properties before link or clear template
# check all the properties before link or clear template
def check_all_properties ( self , host_id , host_groups , status , interfaces , template_ids ,
def check_all_properties ( self , host_id , host_groups , status , interfaces , template_ids ,
exist_interfaces , host , proxy_id , visible_name , host_name) :
exist_interfaces , host , proxy_id , visible_name , description, host_name) :
# get the existing host's groups
# get the existing host's groups
exist_host_groups = self . get_host_groups_by_host_id ( host_id )
exist_host_groups = self . get_host_groups_by_host_id ( host_id )
if set ( host_groups ) != set ( exist_host_groups ) :
if set ( host_groups ) != set ( exist_host_groups ) :
@ -455,6 +467,12 @@ class Host(object):
if host [ ' name ' ] != visible_name and host [ ' name ' ] != host_name :
if host [ ' name ' ] != visible_name and host [ ' name ' ] != host_name :
return True
return True
# The Zabbbix API returns an empty description as an empty string
if description is None :
description = ' '
if host [ ' description ' ] != description :
return True
return False
return False
# link or clear template of the host
# link or clear template of the host
@ -535,8 +553,8 @@ def main():
interfaces = dict ( type = ' list ' , required = False ) ,
interfaces = dict ( type = ' list ' , required = False ) ,
force = dict ( type = ' bool ' , default = True ) ,
force = dict ( type = ' bool ' , default = True ) ,
proxy = dict ( type = ' str ' , required = False ) ,
proxy = dict ( type = ' str ' , required = False ) ,
visible_name = dict ( type = ' str ' , required = False )
visible_name = dict ( type = ' str ' , required = False ) ,
description = dict ( type = ' str ' , required = False )
) ,
) ,
supports_check_mode = True
supports_check_mode = True
)
)
@ -551,6 +569,7 @@ def main():
http_login_password = module . params [ ' http_login_password ' ]
http_login_password = module . params [ ' http_login_password ' ]
host_name = module . params [ ' host_name ' ]
host_name = module . params [ ' host_name ' ]
visible_name = module . params [ ' visible_name ' ]
visible_name = module . params [ ' visible_name ' ]
description = module . params [ ' description ' ]
host_groups = module . params [ ' host_groups ' ]
host_groups = module . params [ ' host_groups ' ]
link_templates = module . params [ ' link_templates ' ]
link_templates = module . params [ ' link_templates ' ]
inventory_mode = module . params [ ' inventory_mode ' ]
inventory_mode = module . params [ ' inventory_mode ' ]
@ -648,11 +667,11 @@ def main():
if len ( exist_interfaces ) > interfaces_len :
if len ( exist_interfaces ) > interfaces_len :
if host . check_all_properties ( host_id , host_groups , status , interfaces , template_ids ,
if host . check_all_properties ( host_id , host_groups , status , interfaces , template_ids ,
exist_interfaces , zabbix_host_obj , proxy_id , visible_name , host_name) :
exist_interfaces , zabbix_host_obj , proxy_id , visible_name , description, host_name) :
host . link_or_clear_template ( host_id , template_ids , tls_connect , tls_accept , tls_psk_identity ,
host . link_or_clear_template ( host_id , template_ids , tls_connect , tls_accept , tls_psk_identity ,
tls_psk , tls_issuer , tls_subject )
tls_psk , tls_issuer , tls_subject )
host . update_host ( host_name , group_ids , status , host_id ,
host . update_host ( host_name , group_ids , status , host_id ,
interfaces , exist_interfaces , proxy_id , visible_name , tls_connect, tls_accept ,
interfaces , exist_interfaces , proxy_id , visible_name , description, tls_connect, tls_accept ,
tls_psk_identity , tls_psk , tls_issuer , tls_subject )
tls_psk_identity , tls_psk , tls_issuer , tls_subject )
module . exit_json ( changed = True ,
module . exit_json ( changed = True ,
result = " Successfully update host %s ( %s ) and linked with template ' %s ' "
result = " Successfully update host %s ( %s ) and linked with template ' %s ' "
@ -663,7 +682,7 @@ def main():
if host . check_all_properties ( host_id , host_groups , status , interfaces , template_ids ,
if host . check_all_properties ( host_id , host_groups , status , interfaces , template_ids ,
exist_interfaces_copy , zabbix_host_obj , proxy_id , visible_name , host_name ) :
exist_interfaces_copy , zabbix_host_obj , proxy_id , visible_name , host_name ) :
host . update_host ( host_name , group_ids , status , host_id , interfaces , exist_interfaces , proxy_id ,
host . update_host ( host_name , group_ids , status , host_id , interfaces , exist_interfaces , proxy_id ,
visible_name , tls_connect, tls_accept , tls_psk_identity , tls_psk , tls_issuer ,
visible_name , description, tls_connect, tls_accept , tls_psk_identity , tls_psk , tls_issuer ,
tls_subject )
tls_subject )
host . link_or_clear_template ( host_id , template_ids , tls_connect , tls_accept , tls_psk_identity ,
host . link_or_clear_template ( host_id , template_ids , tls_connect , tls_accept , tls_psk_identity ,
tls_psk , tls_issuer , tls_subject )
tls_psk , tls_issuer , tls_subject )
@ -691,7 +710,7 @@ def main():
module . fail_json ( msg = " Specify at least one interface for creating host ' %s ' . " % host_name )
module . fail_json ( msg = " Specify at least one interface for creating host ' %s ' . " % host_name )
# create host
# create host
host_id = host . add_host ( host_name , group_ids , status , interfaces , proxy_id , visible_name , tls_connect,
host_id = host . add_host ( host_name , group_ids , status , interfaces , proxy_id , visible_name , description, tls_connect,
tls_accept , tls_psk_identity , tls_psk , tls_issuer , tls_subject )
tls_accept , tls_psk_identity , tls_psk , tls_issuer , tls_subject )
host . link_or_clear_template ( host_id , template_ids , tls_connect , tls_accept , tls_psk_identity ,
host . link_or_clear_template ( host_id , template_ids , tls_connect , tls_accept , tls_psk_identity ,
tls_psk , tls_issuer , tls_subject )
tls_psk , tls_issuer , tls_subject )