forked from Nosmoht/ansible-module-foreman
-
Notifications
You must be signed in to change notification settings - Fork 1
/
foreman_compute_resource.py
206 lines (182 loc) · 6.56 KB
/
foreman_compute_resource.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
DOCUMENTATION = '''
---
module: foreman_compute_resource
short_description: Manage Foreman Compute resources using Foreman API v2
description:
- Create and delete Foreman Compute Resources using Foreman API v2
options:
name:
description: Compute Resource name
required: true
default: null
aliases: []
datacenter: Name of Datacenter (only for Vmware)
required: false
default: null
password:
description: Password for Ovirt, EC2, Vmware, Openstack. Secret key for EC2
required: false
default: null
provider:
description: Providers name (e.g. Ovirt, EC2, Vmware, Openstack, EC2, Google)
required: false
default: null
server:
description: Hostname of Vmware vSphere system
required: false
default: null
state:
description: Compute Resource state
required: false
default: present
choices: ["present", "absent"]
tenant:
description: Tenant name for Openstack
url:
description: URL for Libvirt, Ovirt, and Openstack
required: false
default: null
user:
description: Username for Ovirt, EC2, Vmware, Openstack. Access Key for EC2.
required: false
default: null
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 Vmware compute resource
foreman_compute_resource:
name: VMware
datacenter: dc01
provider: VMware
server: vsphere.example.com
url: vsphere.example.com
user: domain\admin
password: secret
state: present
foreman_user: admin
foreman_pass: secret
- name: Ensure Openstack compute resource
foreman_compute_resource:
name: Openstack
provider: OpenStack
tenant: ExampleTenant
url: https://compute01.example.com:5000/v2.0/tokens
user: admin
'''
try:
from foreman.foreman import *
except ImportError:
foremanclient_found = False
else:
foremanclient_found = True
def get_provider_params(provider):
provider_name = provider.lower()
if provider_name == 'docker':
return ['password', 'url', 'user']
elif provider_name == 'ec2':
return ['access_key', 'password', 'region', 'url', 'user']
elif provider_name == 'google':
return ['email', 'key_path', 'project', 'url', 'zone']
elif provider_name == 'libvirt':
return ['display_type', 'url']
elif provider_name == 'ovirt':
return ['url', 'user', 'password']
elif provider_name == 'openstack':
return ['url', 'user', 'password', 'tenant']
elif provider_name == 'vmware':
return ['datacenter', 'user', 'password', 'server']
else:
return []
def ensure(module):
name = module.params['name']
state = module.params['state']
provider = module.params['provider']
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:
compute_resource = theforeman.search_compute_resource(data=data)
except ForemanError as e:
module.fail_json(msg='Could not get compute resource: {0}'.format(e.message))
if state == 'absent':
if compute_resource:
try:
compute_resource = theforeman.delete_compute_resource(id=compute_resource.get('id'))
except ForemanError as e:
module.fail_json(msg='Could not delete compute resource: {0}'.format(e.message))
return True, compute_resource
data['provider'] = provider
provider_params = get_provider_params(provider=provider)
for key in provider_params:
data[key] = module.params[key]
if state == 'present':
if not compute_resource:
try:
compute_resource = theforeman.create_compute_resource(data=data)
except ForemanError as e:
module.fail_json(msg='Could not create compute resource: {0}'.format(e.message))
return True, compute_resource
return False, compute_resource
if not all(data.get(key, None) == compute_resource.get(key, None) for key in provider_params):
try:
compute_resource = theforeman.update_compute_resource(id=compute_resource.get('id'), data=data)
except ForemanError as e:
module.fail_json(msg='Could not update compute resource: {0}'.format(e.message))
return True, compute_resource
return False, compute_resource
def main():
module = AnsibleModule(
argument_spec=dict(
name=dict(type='str', required=True),
access_key=dict(type='str', requireD=False),
datacenter=dict(type='str', required=False),
display_type=dict(type='str', required=False),
email=dict(type='str', required=False),
key_path=dict(type='str', required=False),
password=dict(type='str', required=False),
provider=dict(type='str', required=False),
region=dict(type='str', required=False),
server=dict(type='str', required=False),
url=dict(type='str', required=False),
user=dict(type='str', required=False),
state=dict(type='str', default='present', choices=['present', 'absent']),
tenat=dict(type='str', 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)
),
)
if not foremanclient_found:
module.fail_json(msg='python-foreman module is required. See https://github.com/Nosmoht/python-foreman.')
changed, compute_resource = ensure(module)
module.exit_json(changed=changed, compute_resource=compute_resource)
# import module snippets
from ansible.module_utils.basic import *
main()