forked from Nosmoht/ansible-module-foreman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
foreman_compute_attribute.py
151 lines (130 loc) · 5.68 KB
/
foreman_compute_attribute.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Ansible module to manage Foreman compute attribute 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_compute_attribute
short_description: Manage Foreman Compute Attributes using Foreman API v2
description:
- Create and update Foreman Compute Attributes using Foreman API v2
options:
compute_resource:
description: Name of compute resource
required: true
compute_profile:
description: Name of compute profile
required: true
vm_attributes:
description: Hash containing the data of vm_attrs
required: true
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
foreman_ssl:
description: Enable SSL when connecting to Foreman API
required: false
default: 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)"
'''
try:
from foreman.foreman import *
foremanclient_found = True
except ImportError:
foremanclient_found = False
def ensure(module):
compute_profile_name = module.params['compute_profile']
compute_resource_name = module.params['compute_resource']
vm_attributes = module.params['vm_attributes']
foreman_host = module.params['foreman_host']
foreman_port = module.params['foreman_port']
foreman_user = module.params['foreman_user']
foreman_pass = module.params['foreman_pass']
foreman_ssl = module.params['foreman_ssl']
theforeman = Foreman(hostname=foreman_host,
port=foreman_port,
username=foreman_user,
password=foreman_pass,
ssl=foreman_ssl)
try:
compute_resource = theforeman.search_compute_resource(data={'name': compute_resource_name})
if not compute_resource:
module.fail_json(msg='Compute resource not found: {0}'.format(compute_resource_name))
except ForemanError as e:
module.fail_json(msg='Could not get compute resource: {0}'.format(compute_resource_name))
try:
compute_profile = theforeman.search_compute_profile(data={'name': compute_profile_name})
if not compute_profile:
module.fail_json(msg='Compute profile not found: {0}'.format(compute_resource_name))
except ForemanError as e:
module.fail_json(msg='Could not get compute profile: {0}'.format(compute_resource_name))
compute_attributes = theforeman.get_compute_attribute(compute_resource_id=compute_resource.get('id'),
compute_profile_id=compute_profile.get('id'))
if compute_attributes:
compute_attribute = compute_attributes[0]
else:
compute_attribute = None
if not compute_attribute:
try:
compute_attribute = theforeman.create_compute_attribute(compute_resource_id=compute_resource.get('id'),
compute_profile_id=compute_profile.get('id'),
data={'vm_attrs': vm_attributes})
return True, compute_attribute
except ForemanError as e:
module.fail_json(msg='Could not create compute attribute: {0}'.format(e.message))
if not all(compute_attribute['vm_attrs'].get(key, vm_attributes.get(key)) == vm_attributes.get(key) for key in
vm_attributes) != 0:
try:
compute_attribute = theforeman.update_compute_attribute(id=compute_attribute.get('id'),
data=vm_attributes)
return True, compute_attribute
except ForemanError as e:
module.fail_json(msg='Could not update compute attribute: {0}'.format(e.message))
return False, compute_attribute
def main():
module = AnsibleModule(
argument_spec=dict(
compute_profile=dict(type='str', required=True),
compute_resource=dict(type='str', required=True),
vm_attributes=dict(type='dict', required=False),
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, no_log=True),
foreman_ssl=dict(type='bool', required=False, default=True)
),
)
if not foremanclient_found:
module.fail_json(msg='python-foreman is required. See https://github.com/Nosmoht/python-foreman.')
changed, compute_attribute = ensure(module)
module.exit_json(changed=changed, compute_attribute=compute_attribute)
from ansible.module_utils.basic import *
if __name__ == '__main__':
main()