Skip to content

Commit a008d50

Browse files
authored
Merge pull request #8 from T-Systems-MMS/add_icinga_command_template
Add icinga command template
2 parents 1c64c73 + e467678 commit a008d50

3 files changed

Lines changed: 331 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This collection contains Ansible modules to change objects in Icinga 2 using the
99
Currently supported modules:
1010

1111
* `icinga_command`
12+
* `icinga_command_template`
1213
* `icinga_host`
1314
* `icinga_hostgroup`
1415
* `icinga_notification`
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
- name: create command template
2+
icinga_command_template:
3+
state: present
4+
url: "https://example.com"
5+
url_username: "{{ icinga_user }}"
6+
url_password: "{{ icinga_pass }}"
7+
arguments:
8+
'--authpassphrase':
9+
value: $snmpv3_priv_key$
10+
'--authprotocol':
11+
value: $snmpv3_auth_protocol$
12+
'--critical':
13+
value: $centreon_critical$
14+
'--filter':
15+
value: $centreon_filter$
16+
'--hostname':
17+
value: $snmp_address$
18+
'--maxrepetitions':
19+
value: $centreon_maxrepetitions$
20+
'--mode':
21+
value: $centreon_mode$
22+
'--plugin':
23+
value: $centreon_plugin$
24+
'--privpassphrase':
25+
value: $snmpv3_auth_key$
26+
'--privprotocol':
27+
value: $snmpv3_priv_protocol$
28+
'--snmp-community':
29+
value: $snmp_community$
30+
'--snmp-timeout':
31+
value: $snmp_timeout$
32+
'--snmp-username':
33+
value: $snmpv3_user$
34+
'--snmp-version':
35+
value: $snmp_version$
36+
'--subsetleef':
37+
value: $centreon_subsetleef$
38+
'--verbose':
39+
set_if: $centreon_verbose$
40+
'--warning':
41+
value: $centreon_warning$
42+
command: "/opt/centreon-plugins/centreon_plugins.pl"
43+
object_name: centreon-plugins-neu
44+
disabled: False
45+
vars:
46+
centreon_maxrepetitions: 20
47+
centreon_subsetleef: 20
48+
centreon_verbose: false
49+
snmp_address: $address$
50+
snmp_timeout: 60
51+
snmp_version: '2'
52+
snmpv3_auth_key: authkey
53+
snmpv3_priv_key: privkey
54+
snmpv3_user: user
Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
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

Comments
 (0)