forked from Nosmoht/ansible-module-foreman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
foreman_operatingsystem.py
242 lines (210 loc) · 7.8 KB
/
foreman_operatingsystem.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Ansible module to manage Foreman operating system resources.
#
# 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/>.
DOCUMENTATION = '''
---
module: foreman_operatingsystem
short_description:
- Manage Foreman Operatingsystems using Foreman API v2.
description:
- Create, update and and delete Foreman Operatingsystems using Foreman API v2
options:
description:
description: OS description
required: false
default: None
name:
description: OS name
required: true
major:
description: OS major version
required: true
minor:
description: OS minor version
required: false
default: None
release_name:
description: Release name
required: false
default: None
state:
description: OS state
required: false
default: 'present'
choices: ['present', 'absent']
foreman_host:
description: Hostname or IP address of Foreman system
required: false
default: 127.0.0.1
foreman_port:
description: Port of Foreman API
required: false
default: 443
foreman_user:
description: Username to be used to authenticate on Foreman
required: true
foreman_pass:
description: Password to be used to authenticate user on Foreman
required: true
notes:
- Requires the python-foreman package to be installed. See https://github.com/Nosmoht/python-foreman.
version_added: "2.0"
author: "Thomas Krahn (@nosmoht)"
'''
EXAMPLES = '''
- name: Ensure CoreOS 607.0.0
foreman_operatingsystem:
name: CoreOS 607.0.0
architectures:
- x86_64
description: CoreOS Current stable
media:
- CoreOS mirror
major: 607
minor: 0.0
ptables:
- CoreOS default fake
state: present
foreman_host: 127.0.0.1
foreman_port: 443
foreman_user: admin
foreman_pass: secret
'''
try:
from foreman.foreman import *
foremanclient_found = True
except ImportError:
foremanclient_found = False
def list_to_dict_list(alist, key):
result = []
if alist:
for item in alist:
result.append({key: item})
return result
def dict_list_to_list(alist, key):
result = list()
if alist:
for item in alist:
result.append(item.get(key, None))
return result
def equal_dict_lists(l1, l2, compare_key='name'):
s1 = set(dict_list_to_list(alist=l1, key=compare_key))
s2 = set(dict_list_to_list(alist=l2, key=compare_key))
return s1.issubset(s2) and s2.issubset(s1)
def get_resources(resource_type, resource_specs):
result = []
for item in resource_specs:
search_data = dict()
if isinstance(item, dict):
for key in item:
search_data[key] = item[key]
else:
search_data['name'] = item
try:
resource = theforeman.search_resource(resource_type=resource_type, data=search_data)
if not resource:
module.fail_json(
msg='Could not find resource type {resource_type} defined as {spec}'.format(
resource_type=resource_type,
spec=item))
result.append(resource)
except ForemanError as e:
module.fail_json(msg='Could not search resource type {resource_type} defined as {spec}: {error}'.format(
resource_type=resource_type, spec=item, error=e.message))
return result
def ensure():
comparable_keys = ['description', 'family', 'major', 'minor', 'release_name']
name = module.params['name']
state = module.params['state']
data = dict(name=name)
data['major'] = module.params['major']
try:
os = theforeman.search_operatingsystem(data=data)
if os:
os = theforeman.get_operatingsystem(id=os.get('id'))
except ForemanError as e:
module.fail_json(msg='Could not get operatingsystem: {0}'.format(e.message))
if state == 'absent':
if os:
try:
os = theforeman.delete_operatingsystem(id=os.get('id'))
return True, os
except ForemanError as e:
module.fail_json(msg='Could not delete operatingsystem: {0}'.format(e.message))
return False, os
data['architectures'] = get_resources(resource_type='architectures', resource_specs=module.params['architectures'])
data['description'] = module.params['description']
data['family'] = module.params['family']
data['minor'] = module.params['minor']
if module.params['media']:
data['media'] = get_resources(resource_type='media', resource_specs=module.params['media'])
if module.params['ptables']:
data['ptables'] = get_resources(resource_type='ptables', resource_specs=module.params['ptables'])
data['release_name'] = module.params['release_name']
if not os:
try:
os = theforeman.create_operatingsystem(data=data)
return True, os
except ForemanError as e:
module.fail_json(msg='Could not create operatingsystem: {0}'.format(e.message))
if (not all(data[key] == os.get(key, data[key]) for key in comparable_keys)) or (
not equal_dict_lists(l1=data.get('architectures', None), l2=os.get('architectures', None))) or (
not equal_dict_lists(l1=data.get('media', None), l2=os.get('media', None))) or (
not equal_dict_lists(l1=data.get('ptables', None), l2=os.get('ptables', None))):
try:
os = theforeman.update_operatingsystem(id=os.get('id'), data=data)
return True, os
except ForemanError as e:
module.fail_json(msg='Could not update operatingsystem: {0}'.format(e.message))
return False, os
def main():
global module
global theforeman
module = AnsibleModule(
argument_spec=dict(
architectures=dict(type='list', required=False),
description=dict(type='str', required=False),
family=dict(type='str', required=False),
major=dict(type='str', required=False),
media=dict(type='list', required=False),
minor=dict(type='str', required=False),
name=dict(type='str', required=True),
ptables=dict(type='list', required=False),
release_name=dict(type='str', required=False),
state=dict(type='str', default='present', choices=['present', 'absent']),
foreman_host=dict(type='str', default='127.0.0.1'),
foreman_port=dict(type='str', default='443'),
foreman_user=dict(type='str', required=True),
foreman_pass=dict(type='str', required=True)
),
)
if not foremanclient_found:
module.fail_json(msg='python-foreman module is required. See https://github.com/Nosmoht/python-foreman.')
foreman_host = module.params['foreman_host']
foreman_port = module.params['foreman_port']
foreman_user = module.params['foreman_user']
foreman_pass = module.params['foreman_pass']
theforeman = Foreman(hostname=foreman_host,
port=foreman_port,
username=foreman_user,
password=foreman_pass)
changed, os = ensure()
module.exit_json(changed=changed, operatingsystem=os)
# import module snippets
from ansible.module_utils.basic import *
if __name__ == '__main__':
main()