|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# Copyright (c) 2019 Ansible Project |
| 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 | + |
| 20 | +from __future__ import absolute_import, division, print_function |
| 21 | + |
| 22 | +__metaclass__ = type |
| 23 | + |
| 24 | +ANSIBLE_METADATA = {'metadata_version': '1.1', |
| 25 | + 'status': ['preview'], |
| 26 | + 'supported_by': 'community'} |
| 27 | + |
| 28 | +DOCUMENTATION = ''' |
| 29 | +--- |
| 30 | +module: icinga_command_template |
| 31 | +short_description: Manage command templates in Icinga2 |
| 32 | +description: |
| 33 | + - "Add or remove a command template to Icinga2 through the director API." |
| 34 | +author: "Lars Krahl (@mmslkr)" |
| 35 | +options: |
| 36 | + url: |
| 37 | + description: |
| 38 | + - HTTP, HTTPS, or FTP URL in the form (http|https|ftp)://[user[:pass]]@host.domain[:port]/path |
| 39 | + required: true |
| 40 | + type: str |
| 41 | + use_proxy: |
| 42 | + description: |
| 43 | + - If C(no), it will not use a proxy, even if one is defined in |
| 44 | + an environment variable on the target hosts. |
| 45 | + type: bool |
| 46 | + default: 'yes' |
| 47 | + validate_certs: |
| 48 | + description: |
| 49 | + - If C(no), SSL certificates will not be validated. This should only be used |
| 50 | + on personally controlled sites using self-signed certificates. |
| 51 | + type: bool |
| 52 | + default: 'yes' |
| 53 | + url_username: |
| 54 | + description: |
| 55 | + - The username for use in HTTP basic authentication. |
| 56 | + - This parameter can be used without C(url_password) for sites that allow empty passwords. |
| 57 | + type: str |
| 58 | + url_password: |
| 59 | + description: |
| 60 | + - The password for use in HTTP basic authentication. |
| 61 | + - If the C(url_username) parameter is not specified, the C(url_password) parameter will not be used. |
| 62 | + type: str |
| 63 | + force_basic_auth: |
| 64 | + description: |
| 65 | + - httplib2, the library used by the uri module only sends authentication information when a webservice |
| 66 | + responds to an initial request with a 401 status. Since some basic auth services do not properly |
| 67 | + send a 401, logins will fail. This option forces the sending of the Basic authentication header |
| 68 | + upon initial request. |
| 69 | + type: bool |
| 70 | + default: 'no' |
| 71 | + client_cert: |
| 72 | + description: |
| 73 | + - PEM formatted certificate chain file to be used for SSL client |
| 74 | + authentication. This file can also include the key as well, and if |
| 75 | + the key is included, C(client_key) is not required. |
| 76 | + type: path |
| 77 | + client_key: |
| 78 | + description: |
| 79 | + - PEM formatted file that contains your private key to be used for SSL |
| 80 | + client authentication. If C(client_cert) contains both the certificate |
| 81 | + and key, this option is not required. |
| 82 | + type: path |
| 83 | + state: |
| 84 | + description: |
| 85 | + - Apply feature state. |
| 86 | + choices: [ "present", "absent" ] |
| 87 | + default: present |
| 88 | + type: str |
| 89 | + command: |
| 90 | + description: |
| 91 | + - The command Icinga should run. |
| 92 | + - Absolute paths are accepted as provided, relative paths are prefixed with "PluginDir + ", similar Constant prefixes are allowed. |
| 93 | + - Spaces will lead to separation of command path and standalone arguments. |
| 94 | + - Please note that this means that we do not support spaces in plugin names and paths right now. |
| 95 | + type: str |
| 96 | + required: False |
| 97 | + command_type: |
| 98 | + description: |
| 99 | + - Plugin Check commands are what you need when running checks agains your infrastructure. |
| 100 | + - Notification commands will be used when it comes to notify your users. |
| 101 | + - Event commands allow you to trigger specific actions when problems occur. |
| 102 | + - Some people use them for auto-healing mechanisms, like restarting services or rebooting systems at specific thresholds |
| 103 | + choices: ["PluginCheck", "PluginNotification", "PluginEvent"] |
| 104 | + default: "PluginCheck" |
| 105 | + type: str |
| 106 | + disabled: |
| 107 | + description: |
| 108 | + - Disabled objects will not be deployed |
| 109 | + type: bool |
| 110 | + default: False |
| 111 | + choices: [True, False] |
| 112 | + object_name: |
| 113 | + description: |
| 114 | + - Name of the service apply rule |
| 115 | + required: true |
| 116 | + type: str |
| 117 | + imports: |
| 118 | + description: |
| 119 | + - Importable templates, add as many as you want. Please note that order matters when importing properties from multiple templates - last one wins |
| 120 | + required: false |
| 121 | + type: list |
| 122 | + timeout: |
| 123 | + description: |
| 124 | + - Optional command timeout. Allowed values are seconds or durations postfixed with a specific unit (e.g. 1m or also 3m 30s). |
| 125 | + required: false |
| 126 | + type: str |
| 127 | + zone: |
| 128 | + description: |
| 129 | + - Icinga cluster zone. Allows to manually override Directors decisions of where to deploy your config to. |
| 130 | + - You should consider not doing so unless you gained deep understanding of how an Icinga Cluster stack works |
| 131 | + required: false |
| 132 | + type: str |
| 133 | + vars: |
| 134 | + description: |
| 135 | + - Custom properties of the command template |
| 136 | + required: false |
| 137 | + type: "dict" |
| 138 | + arguments: |
| 139 | + description: |
| 140 | + - arguments of the command template |
| 141 | + required: false |
| 142 | + type: "dict" |
| 143 | +''' |
| 144 | + |
| 145 | +EXAMPLES = ''' |
| 146 | + - name: create command template |
| 147 | + icinga_command_template: |
| 148 | + state: present |
| 149 | + url: "https://example.com" |
| 150 | + url_username: "{{ icinga_user }}" |
| 151 | + url_password: "{{ icinga_pass }}" |
| 152 | + arguments: |
| 153 | + '--authpassphrase': |
| 154 | + value: $snmpv3_priv_key$ |
| 155 | + '--authprotocol': |
| 156 | + value: $snmpv3_auth_protocol$ |
| 157 | + '--critical': |
| 158 | + value: $centreon_critical$ |
| 159 | + '--filter': |
| 160 | + value: $centreon_filter$ |
| 161 | + '--hostname': |
| 162 | + value: $snmp_address$ |
| 163 | + '--maxrepetitions': |
| 164 | + value: $centreon_maxrepetitions$ |
| 165 | + '--mode': |
| 166 | + value: $centreon_mode$ |
| 167 | + '--plugin': |
| 168 | + value: $centreon_plugin$ |
| 169 | + '--privpassphrase': |
| 170 | + value: $snmpv3_auth_key$ |
| 171 | + '--privprotocol': |
| 172 | + value: $snmpv3_priv_protocol$ |
| 173 | + '--snmp-community': |
| 174 | + value: $snmp_community$ |
| 175 | + '--snmp-timeout': |
| 176 | + value: $snmp_timeout$ |
| 177 | + '--snmp-username': |
| 178 | + value: $snmpv3_user$ |
| 179 | + '--snmp-version': |
| 180 | + value: $snmp_version$ |
| 181 | + '--subsetleef': |
| 182 | + value: $centreon_subsetleef$ |
| 183 | + '--verbose': |
| 184 | + set_if: $centreon_verbose$ |
| 185 | + '--warning': |
| 186 | + value: $centreon_warning$ |
| 187 | + command: "/opt/centreon-plugins/centreon_plugins.pl" |
| 188 | + object_name: centreon-plugins-neu |
| 189 | + disabled: False |
| 190 | + vars: |
| 191 | + centreon_maxrepetitions: 20 |
| 192 | + centreon_subsetleef: 20 |
| 193 | + centreon_verbose: false |
| 194 | + snmp_address: $address$ |
| 195 | + snmp_timeout: 60 |
| 196 | + snmp_version: '2' |
| 197 | + snmpv3_auth_key: authkey |
| 198 | + snmpv3_priv_key: privkey |
| 199 | + snmpv3_user: user |
| 200 | +''' |
| 201 | + |
| 202 | +from ansible.module_utils.basic import AnsibleModule |
| 203 | +from ansible.module_utils.urls import url_argument_spec |
| 204 | +from ansible_collections.t_systems_mms.icinga_director.plugins.module_utils.icinga import Icinga2APIObject |
| 205 | + |
| 206 | + |
| 207 | +# =========================================== |
| 208 | +# Module execution. |
| 209 | +# |
| 210 | +def main(): |
| 211 | + # use the predefined argument spec for url |
| 212 | + argument_spec = url_argument_spec() |
| 213 | + # remove unnecessary argument 'force' |
| 214 | + del argument_spec['force'] |
| 215 | + del argument_spec['http_agent'] |
| 216 | + # add our own arguments |
| 217 | + argument_spec.update( |
| 218 | + state=dict(default="present", choices=["absent", "present"]), |
| 219 | + object_name=dict(required=True), |
| 220 | + imports=dict(type="list", required=False, default=[]), |
| 221 | + disabled=dict(type="bool", required=False, default=False, choices=[True, False]), |
| 222 | + vars=dict(type="dict", default={}), |
| 223 | + command=dict(required=False), |
| 224 | + command_type=dict(default="PluginCheck", choices=["PluginCheck", "PluginNotification", "PluginEvent"]), |
| 225 | + timeout=dict(required=False, default=None), |
| 226 | + zone=dict(required=False, default=None), |
| 227 | + arguments=dict(type="dict", default=None), |
| 228 | + ) |
| 229 | + |
| 230 | + # Define the main module |
| 231 | + module = AnsibleModule( |
| 232 | + argument_spec=argument_spec, |
| 233 | + supports_check_mode=True |
| 234 | + ) |
| 235 | + |
| 236 | + state = module.params["state"] |
| 237 | + object_name = module.params["object_name"] |
| 238 | + imports = module.params["imports"] |
| 239 | + disabled = module.params["disabled"] |
| 240 | + vars = module.params["vars"] |
| 241 | + command = module.params["command"] |
| 242 | + command_type = module.params["command_type"] |
| 243 | + timeout = module.params["timeout"] |
| 244 | + zone = module.params["zone"] |
| 245 | + # `arguments` is of type dict, default should be {} |
| 246 | + # however, the director api returns [] when no `arguments` are set, making the diff seem changed |
| 247 | + # therefore set the default to [] as well to get a clean diff output |
| 248 | + if not module.params["arguments"]: |
| 249 | + module.params["arguments"] = [] |
| 250 | + arguments = module.params["arguments"] |
| 251 | + |
| 252 | + data = { |
| 253 | + 'object_name': object_name, |
| 254 | + 'object_type': "template", |
| 255 | + 'imports': imports, |
| 256 | + 'disabled': disabled, |
| 257 | + 'vars': vars, |
| 258 | + 'command': command, |
| 259 | + 'methods_execute': command_type, |
| 260 | + 'timeout': timeout, |
| 261 | + 'zone': zone, |
| 262 | + 'arguments': arguments, |
| 263 | + } |
| 264 | + |
| 265 | + try: |
| 266 | + icinga_object = Icinga2APIObject(module=module, path="/command", data=data) |
| 267 | + except Exception as e: |
| 268 | + module.fail_json(msg="unable to connect to Icinga. Exception message: %s" % e) |
| 269 | + |
| 270 | + changed, diff = icinga_object.update(state) |
| 271 | + module.exit_json(changed=changed, object_name=object_name, data=icinga_object.data, diff=diff) |
| 272 | + |
| 273 | + |
| 274 | +# import module snippets |
| 275 | +if __name__ == '__main__': |
| 276 | + main() |
0 commit comments