-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinventory.py
252 lines (221 loc) · 7.65 KB
/
inventory.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# -*- coding: utf-8 -*-
"""
Ansible inventory module for Uyuni
2022 Christian Stankowic
This program 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 program 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 program. If not, see <http://www.gnu.org/licenses/>.
"""
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
DOCUMENTATION = '''
name: inventory
short_description: Uyuni inventory source
author:
- Christian Stankowic (@stdevel)
description:
- Get inventory hosts from the Uyuni API.
- "Uses a configuration file as an inventory source, it must end in
C(.uyuni.yml) or C(.uyuni.yaml)."
options:
plugin:
description: Name of the plugin.
required: true
type: string
choices: ['stdevel.uyuni.inventory']
host:
description:
- Hostname/IP address of the Uyuni server.
- If the value is not specified in the inventory configuration, the value of environment variable C(UYUNI_HOST) will be used instead.
type: string
required: true
env:
- name: UYUNI_HOST
version_added: 0.2.0
user:
description:
- Username to query the API with.
- If the value is not specified in the inventory configuration, the value of environment variable C(UYUNI_USER) will be used instead.
type: string
required: true
env:
- name: UYUNI_USER
version_added: 0.2.0
port:
description: API port
type: int
default: 443
password:
description:
- Password to query the API with.
- If the value is not specified in the inventory configuration, the value of environment variable C(UYUNI_PASSWORD) will be used instead.
type: string
required: true
env:
- name: UYUNI_PASSWORD
version_added: 0.2.0
verify_ssl:
description: Enables or disables SSL certificate verification.
type: boolean
default: true
only_powered_on:
description: Only shows powered-on hosts.
type: boolean
default: true
ipv6_only:
description: Use IPv6 addresses only
type: boolean
default: false
show_custom_values:
description: Lists defined custom parameters and values
type: boolean
default: false
groups:
description: Limits to specific names groups
type: list
elements: str
required: false
pending_reboot_only:
description: Limits to systems requiring a reboot only
type: boolean
default: false
'''
EXAMPLES = r'''
---
# my.uyuni.yml
plugin: stdevel.uyuni.inventory
host: 192.168.180.1
user: admin
password: admin
verify_ssl: false
show_custom_values: true
ipv6_only: true
groups:
- dev
- demo
...
---
# for use in AWX / AAP (Inventory Source "Sourced from a Project"),
# together with a custom credential that injects environment variables UYUNI_HOST, UYUNI_USER, UYUNI_PASSWORD
plugin: stdevel.uyuni.inventory
show_custom_values: true
...
'''
from ansible.plugins.inventory import (
BaseInventoryPlugin, Constructable, Cacheable
)
from ..module_utils.helper_functions import _configure_connection
class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
"""
Host inventory parser for ansible using Uyuni
"""
NAME = 'stdevel.uyuni.inventory'
def __init__(self):
"""
Initializes the inventory plugin
"""
super(InventoryModule, self).__init__()
# clear config
self.api_instance = None
self.host = None
self.user = None
self.password = None
self.port = None
self.verify_ssl = None
def verify_file(self, path):
"""
Verifies the configuration file
"""
valid = False
if super(InventoryModule, self).verify_file(path):
if path.endswith(('uyuni.yaml', 'uyuni.yml')):
valid = True
else:
self.display.vvv(
'Skipping due to inventory source not ending in "uyuni.yml" nor "uyuni.yaml"' # noqa: E501
)
return valid
def _api_connect(self):
"""
Connects to the Uyuni API
"""
self.api_instance = _configure_connection(
dict(
host=str(self.get_option('host')),
username=str(self.get_option('user')),
password=str(self.get_option('password')),
port=str(self.get_option('port')),
verify_ssl=self.get_option('verify_ssl')
)
)
def _populate(self):
# get groups and hosts
all_groups = self.api_instance.get_all_hostgroups()
hosts = self.api_instance.get_all_hosts()
if self.get_option('groups'):
# limit to group selection
groups = [x for x in all_groups if x in self.get_option('groups')]
else:
# all groups
groups = all_groups
for group in groups:
# add selected/all groups
self.inventory.add_group(group)
# get systems requiring reboot
_reboot = self.api_instance.get_hosts_by_required_reboot()
# add _all_ the hosts
for host in hosts:
# get host groups
_groups = self.api_instance.get_hostgroups_by_host(int(host['id']))
if self.get_option('groups'):
# only add if host is filtered groups
if not any(x in _groups for x in self.get_option('groups')):
continue
# check if reboot required
if self.get_option('pending_reboot_only'):
try:
if host['name'] not in _reboot:
continue
except TypeError:
continue
# add host
self.inventory.add_host(host['name'])
# get IP address
_network = self.api_instance.get_host_network(int(host['id']))
if self.get_option('ipv6_only'):
self.inventory.set_variable(
host['name'], 'ansible_host', _network['ip6']
)
else:
self.inventory.set_variable(
host['name'], 'ansible_host', _network['ip']
)
# add parameters
if self.get_option('show_custom_values'):
_params = self.api_instance.get_host_params(int(host['id']))
for param in _params:
self.inventory.set_variable(
host['name'], param, _params[param]
)
# add hostgroups
for _group in _groups:
if _group in groups:
self.inventory.add_child(_group, host['name'])
def parse(self, inventory, loader, path, cache=True):
"""
Parses the inventory
"""
super(InventoryModule, self).parse(inventory, loader, path)
# read config from file, this sets 'options'
self._read_config_data(path)
# create API instance
self._api_connect()
# create inventory
self._populate()