forked from Nosmoht/ansible-module-foreman
-
Notifications
You must be signed in to change notification settings - Fork 1
/
foreman_config_template.py
242 lines (213 loc) · 8.17 KB
/
foreman_config_template.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/env python
# -*- coding: utf-8 -*-
DOCUMENTATION = '''
---
module: foreman_config_template
short_description:
- Manage Foreman Provision Templates using Foreman API v2.
description:
- Create, update and and delete Foreman provision templates using Foreman API v2
options:
audit_comment:
description:
- Audit comment
required: false
name:
description: Provision template name
required: true
default: null
aliases: None
locked:
description: Whether or not the template is locked for editing
required: false
default: false
operatingsystems:
description: List of Operatingsystem names the template is assigned to
required: false
default: None
template:
description: RAW template content
required: false
template_file:
description: Path and filename to load the template from
required: false
template_kind_name:
description: Template Kind name
required: false
snippet:
description: Define if template is a snippet or not
required: false
default: false
state:
description: Provision template 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
default: null
foreman_pass:
description: Password to be used to authenticate user on Foreman
required: true
default: null
notes:
- Requires the python-foreman package to be installed. See https://github.com/Nosmoht/python-foreman.
author: Thomas Krahn
'''
EXAMPLES = '''
- name: Ensure Config Template
foreman_config_template:
name: CoreOS Cloud-config
locked: false
operatingsystems:
- CoreOS
template_file: /tmp/coreos-cloud-config
snippet: true
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 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_func, resource_specs):
result = list()
if not resource_specs:
return 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 = resource_func(data=search_data)
if not resource:
module.fail_json(
msg='Could not find resource type {resource_type} specified as {name}'.format(
resource_type=resource_type,
name=item))
result.append(resource)
except ForemanError as e:
module.fail_json(msg='Could not search resource type {resource_type} specified as {name}: {error}'.format(
resource_type=resource_type, name=item, error=e.message))
return result
def ensure():
audit_comment = module.params['audit_comment']
compareable_keys = ['locked', 'snippet', 'template']
locked = module.params['locked']
name = module.params['name']
operatingsystems = module.params['operatingsystems']
state = module.params['state']
snippet = module.params['snippet']
template = module.params['template']
template_file = module.params['template_file']
template_kind_name = module.params['template_kind_name']
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)
data = dict(name=name)
try:
config_template = theforeman.search_config_template(data=data)
if config_template:
config_template = theforeman.get_config_template(id=config_template.get('id'))
except ForemanError as e:
module.fail_json(msg='Could not get config template: {0}'.format(e.message))
if state == 'absent':
if config_template:
try:
config_template = theforeman.delete_config_template(id=config_template.get('id'))
return True, config_template
except ForemanError as e:
module.fail_json(msg='Could not delete config template: {0}'.format(e.message))
if state == 'present':
if not template and not template_file:
module.fail_json(msg='Either template or template_file must be defined')
elif template and template_file:
module.fail_json(msg='Only one of either template or template_file must be defined')
elif template:
data['template'] = template
else:
try:
with open(template_file) as f:
data['template'] = f.read()
except IOError as e:
module.fail_json(msg='Could not open file {0}: {1}'.format(template_file, e.message))
data['audit_comment'] = audit_comment
data['locked'] = locked
data['snippet'] = snippet
data['template_kind_name'] = template_kind_name
if not snippet:
data['operatingsystems'] = get_resources(resource_type='operatingsystem',
resource_func=theforeman.search_operatingsystem,
resource_specs=operatingsystems)
if not config_template:
try:
config_template = theforeman.create_config_template(data=data)
return True, config_template
except ForemanError as e:
module.fail_json(msg='Could not create config template: {0}'.format(e.message))
if (not all(data.get(key, None) == config_template.get(key, None) for key in compareable_keys)) or (
not equal_dict_lists(l1=data.get('operatingsystems', None),
l2=config_template.get('operatingsystems', None))):
try:
config_template = theforeman.update_config_template(id=config_template.get('id'), data=data)
return True, config_template
except ForemanError as e:
module.fail_json(msg='Could not update config template: {0}'.format(e.message))
return False, config_template
def main():
global module
module = AnsibleModule(
argument_spec=dict(
audit_comment=dict(type='str', required=False),
name=dict(type='str', required=True),
locked=dict(type='bool', default=False),
operatingsystems=dict(type='list', required=False),
template=dict(type='str', required=False),
template_file=dict(type='str', required=False),
template_kind_name=dict(type='str', required=False),
snippet=dict(type='bool', default=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.')
changed, config_template = ensure()
module.exit_json(changed=changed, config_template=config_template)
# import module snippets
from ansible.module_utils.basic import *
main()