1#!/usr/local/bin/python3.8 2# -*- coding: utf-8 -*- 3# 4# Copyright (c) 2020 T-Systems Multimedia Solutions GmbH 5# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) 6# 7# This module is free software: you can redistribute it and/or modify 8# it under the terms of the GNU General Public License as published by 9# the Free Software Foundation, either version 3 of the License, or 10# (at your option) any later version. 11# 12# This software is distributed in the hope that it will be useful, 13# but WITHOUT ANY WARRANTY; without even the implied warranty of 14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15# GNU General Public License for more details. 16# 17# You should have received a copy of the GNU General Public License 18# along with this software. If not, see <http://www.gnu.org/licenses/>. 19 20from __future__ import absolute_import, division, print_function 21 22__metaclass__ = type 23 24DOCUMENTATION = """ 25--- 26module: icinga_service_template 27short_description: Manage service templates in Icinga2 28description: 29 - Add or remove a service template to Icinga2 through the director API. 30author: Sebastian Gumprich (@rndmh3ro) 31extends_documentation_fragment: 32 - ansible.builtin.url 33 - t_systems_mms.icinga_director.common_options 34version_added: '1.0.0' 35notes: 36 - This module supports check mode. 37options: 38 state: 39 description: 40 - Apply feature state. 41 choices: [ "present", "absent" ] 42 default: present 43 type: str 44 object_name: 45 description: 46 - Name of the service template. 47 aliases: ['name'] 48 required: true 49 type: str 50 check_command: 51 description: 52 - Check command definition. 53 type: str 54 check_interval: 55 description: 56 - Your regular check interval. 57 type: str 58 check_period: 59 description: 60 - The name of a time period which determines when this object should be monitored. Not limited by default. 61 type: str 62 check_timeout: 63 description: 64 - Check command timeout in seconds. Overrides the CheckCommand's timeout attribute. 65 type: str 66 enable_active_checks: 67 description: 68 - Whether to actively check this object. 69 type: "bool" 70 enable_event_handler: 71 description: 72 - Whether to enable event handlers this object. 73 type: "bool" 74 enable_notifications: 75 description: 76 - Whether to send notifications for this object. 77 type: "bool" 78 enable_passive_checks: 79 description: 80 - Whether to accept passive check results for this object. 81 type: "bool" 82 enable_perfdata: 83 description: 84 - Whether to process performance data provided by this object. 85 type: "bool" 86 event_command: 87 description: 88 - Event command for service which gets called on every check execution if one of these conditions matches 89 - The service is in a soft state 90 - The service state changes into a hard state 91 - The service state recovers from a soft or hard state to OK/Up 92 type: "str" 93 groups: 94 description: 95 - Service groups that should be directly assigned to this service. 96 - Servicegroups can be useful for various reasons. 97 - They are helpful to provided service-type specific view in Icinga Web 2, either for custom dashboards or as an instrument to enforce restrictions. 98 - Service groups can be directly assigned to single services or to service templates. 99 type: "list" 100 elements: "str" 101 default: [] 102 imports: 103 description: 104 - Importable templates, add as many as you want. 105 - Please note that order matters when importing properties from multiple templates - last one wins. 106 type: "list" 107 elements: "str" 108 default: [] 109 max_check_attempts: 110 description: 111 - Defines after how many check attempts a new hard state is reached. 112 type: str 113 notes: 114 description: 115 - Additional notes for this object. 116 type: str 117 version_added: '1.8.0' 118 notes_url: 119 description: 120 - An URL pointing to additional notes for this object. 121 - Separate multiple urls like this "'http://url1' 'http://url2'". 122 - Maximum length is 255 characters. 123 type: str 124 version_added: '1.8.0' 125 retry_interval: 126 description: 127 - Retry interval, will be applied after a state change unless the next hard state is reached. 128 type: str 129 use_agent: 130 description: 131 - Whether the check commmand for this service should be executed on the Icinga agent. 132 type: "bool" 133 vars: 134 description: 135 - Custom properties of the service template. 136 type: "dict" 137 default: {} 138 volatile: 139 description: 140 - Whether this check is volatile. 141 type: "bool" 142 disabled: 143 description: 144 - Disabled objects will not be deployed. 145 type: bool 146 default: False 147 choices: [True, False] 148""" 149 150EXAMPLES = """ 151- name: Create servicetemplate 152 t_systems_mms.icinga_director.icinga_service_template: 153 state: present 154 url: "{{ icinga_url }}" 155 url_username: "{{ icinga_user }}" 156 url_password: "{{ icinga_pass }}" 157 object_name: fooservicetemplate 158 use_agent: false 159 vars: 160 procs_argument: consul 161 procs_critical: '1:' 162 procs_warning: '1:' 163 notes: "example note" 164 notes_url: "'http://url1' 'http://url2'" 165 166- name: Create servicetemplate with event command 167 t_systems_mms.icinga_director.icinga_service_template: 168 state: present 169 url: "{{ icinga_url }}" 170 url_username: "{{ icinga_user }}" 171 url_password: "{{ icinga_pass }}" 172 object_name: apache_check_servicetemplate 173 use_agent: false 174 event_command: restart_httpd 175 notes: "example note" 176 notes_url: "'http://url1' 'http://url2'" 177""" 178 179RETURN = r""" # """ 180 181from ansible.module_utils.basic import AnsibleModule 182from ansible.module_utils.urls import url_argument_spec 183from ansible_collections.t_systems_mms.icinga_director.plugins.module_utils.icinga import ( 184 Icinga2APIObject, 185) 186 187 188# =========================================== 189# Module execution. 190# 191def main(): 192 # use the predefined argument spec for url 193 argument_spec = url_argument_spec() 194 # add our own arguments 195 argument_spec.update( 196 state=dict(default="present", choices=["absent", "present"]), 197 url=dict(required=True), 198 object_name=dict(required=True, aliases=["name"]), 199 disabled=dict(type="bool", default=False, choices=[True, False]), 200 check_command=dict(required=False), 201 check_interval=dict(required=False), 202 check_period=dict(required=False), 203 check_timeout=dict(required=False), 204 enable_active_checks=dict(type="bool", required=False), 205 enable_event_handler=dict(type="bool", required=False), 206 enable_notifications=dict(type="bool", required=False), 207 enable_passive_checks=dict(type="bool", required=False), 208 enable_perfdata=dict(type="bool", required=False), 209 event_command=dict(type="str", required=False), 210 groups=dict(type="list", elements="str", default=[], required=False), 211 imports=dict(type="list", elements="str", default=[], required=False), 212 max_check_attempts=dict(required=False), 213 notes=dict(type="str", required=False), 214 notes_url=dict(type="str", required=False), 215 retry_interval=dict(required=False), 216 use_agent=dict(type="bool", required=False), 217 vars=dict(type="dict", default={}, required=False), 218 volatile=dict(type="bool", required=False), 219 ) 220 221 # Define the main module 222 module = AnsibleModule( 223 argument_spec=argument_spec, supports_check_mode=True 224 ) 225 226 data = { 227 "object_name": module.params["object_name"], 228 "disabled": module.params["disabled"], 229 "object_type": "template", 230 "check_command": module.params["check_command"], 231 "check_interval": module.params["check_interval"], 232 "check_period": module.params["check_period"], 233 "check_timeout": module.params["check_timeout"], 234 "enable_active_checks": module.params["enable_active_checks"], 235 "enable_event_handler": module.params["enable_event_handler"], 236 "enable_notifications": module.params["enable_notifications"], 237 "enable_passive_checks": module.params["enable_passive_checks"], 238 "enable_perfdata": module.params["enable_perfdata"], 239 "event_command": module.params["event_command"], 240 "groups": module.params["groups"], 241 "imports": module.params["imports"], 242 "max_check_attempts": module.params["max_check_attempts"], 243 "notes": module.params["notes"], 244 "notes_url": module.params["notes_url"], 245 "retry_interval": module.params["retry_interval"], 246 "use_agent": module.params["use_agent"], 247 "vars": module.params["vars"], 248 "volatile": module.params["volatile"], 249 } 250 251 icinga_object = Icinga2APIObject(module=module, path="/service", data=data) 252 253 changed, diff = icinga_object.update(module.params["state"]) 254 module.exit_json( 255 changed=changed, 256 diff=diff, 257 ) 258 259 260# import module snippets 261if __name__ == "__main__": 262 main() 263