forked from telekom-mms/ansible-collection-icinga-director
-
Notifications
You must be signed in to change notification settings - Fork 0
/
icinga_hostgroup.py
166 lines (145 loc) · 4.77 KB
/
icinga_hostgroup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2020 T-Systems Multimedia Solutions GmbH
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
#
# This module is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this software. If not, see <http://www.gnu.org/licenses/>.
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = """
---
module: icinga_hostgroup
short_description: Manage hostgroups in Icinga2
description:
- Add or remove a hostgroup to Icinga2 through the director API.
author: Sebastian Gumprich (@rndmh3ro)
extends_documentation_fragment:
- ansible.builtin.url
- telekom_mms.icinga_director.common_options
version_added: '1.0.0'
notes:
- This module supports check mode.
options:
state:
description:
- Apply feature state.
choices: [ "present", "absent" ]
default: present
type: str
object_name:
description:
- Icinga object name for this hostgroup.
aliases: ['name']
required: true
type: str
display_name:
description:
- An alternative display name for this group.
- If you wonder how this could be helpful just leave it blank.
type: str
assign_filter:
description:
- This allows you to configure an assignment filter.
- Please feel free to combine as many nested operators as you want.
type: str
append:
description:
- Do not overwrite the whole object but instead append the defined properties.
- Note - Appending to existing vars, imports or any other list/dict is not possible. You have to overwrite the complete list/dict.
- Note - Variables that are set by default will also be applied, even if not set.
type: bool
choices: [true, false]
version_added: '1.25.0'
"""
EXAMPLES = """
- name: Create hostgroup
telekom_mms.icinga_director.icinga_hostgroup:
state: present
url: "{{ icinga_url }}"
url_username: "{{ icinga_user }}"
url_password: "{{ icinga_pass }}"
object_name: foohostgroup
display_name: foohostgroup
- name: Update hostgroup
telekom_mms.icinga_director.icinga_hostgroup:
state: present
url: "{{ icinga_url }}"
url_username: "{{ icinga_user }}"
url_password: "{{ icinga_pass }}"
object_name: foohostgroup
assign_filter: 'host.name="foohost"'
append: true
- name: Update hostgroup using multiple contiditions in assign_filter
telekom_mms.icinga_director.icinga_hostgroup:
state: present
url: "{{ icinga_url }}"
url_username: "{{ icinga_user }}"
url_password: "{{ icinga_pass }}"
object_name: foohostgroup
assign_filter: 'host.vars.something="{{ your_var_here }}"|host.vars.something_else="anything"'
append: true
vars:
your_var_here: foo
"""
RETURN = r""" # """
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.urls import url_argument_spec
from ansible_collections.telekom_mms.icinga_director.plugins.module_utils.icinga import (
Icinga2APIObject,
)
# ===========================================
# Module execution.
#
def main():
# use the predefined argument spec for url
argument_spec = url_argument_spec()
# add our own arguments
argument_spec.update(
state=dict(default="present", choices=["absent", "present"]),
url=dict(required=True),
append=dict(type="bool", choices=[True, False]),
object_name=dict(required=True, aliases=["name"]),
display_name=dict(required=False),
assign_filter=dict(required=False),
)
# Define the main module
module = AnsibleModule(
argument_spec=argument_spec, supports_check_mode=True
)
data_keys = [
"object_name",
"display_name",
"assign_filter",
]
data = {}
if module.params["append"]:
for k in data_keys:
if module.params[k]:
data[k] = module.params[k]
else:
for k in data_keys:
data[k] = module.params[k]
data["object_type"] = "object"
icinga_object = Icinga2APIObject(
module=module, path="/hostgroup", data=data
)
changed, diff = icinga_object.update(module.params["state"])
module.exit_json(
changed=changed,
diff=diff,
)
# import module snippets
if __name__ == "__main__":
main()