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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = '''
---
module: ovh_ip_failover
short_description: Manage OVH IP failover address
description:
- Manage OVH (French European hosting provider) IP Failover Address. For now, this module can only be used to move
an ip failover (or failover block) between services
author: "Pascal HERAUD (@pascalheraud)"
notes:
- Uses the python OVH Api U(https://github.com/ovh/python-ovh).
You have to create an application (a key and secret) with a consumer
key as described into U(https://docs.ovh.com/gb/en/customer/first-steps-with-ovh-api/)
requirements:
- ovh >= 0.4.8
extends_documentation_fragment:
- community.general.attributes
attributes:
check_mode:
support: full
diff_mode:
support: none
options:
name:
required: true
description:
- The IP address to manage (can be a single IP like 1.1.1.1
or a block like 1.1.1.1/28 )
type: str
service:
required: true
description:
- The name of the OVH service this IP address should be routed
type: str
endpoint:
required: true
description:
- The endpoint to use ( for instance ovh-eu)
type: str
wait_completion:
required: false
default: true
type: bool
description:
- If true, the module will wait for the IP address to be moved.
If false, exit without waiting. The taskId will be returned
in module output
wait_task_completion:
required: false
default: 0
description:
- If not 0, the module will wait for this task id to be
completed. Use wait_task_completion if you want to wait for
completion of a previously executed task with
wait_completion=false. You can execute this module repeatedly on
a list of failover IPs using wait_completion=false (see examples)
type: int
application_key:
required: true
description:
- The applicationKey to use
type: str
application_secret:
required: true
description:
- The application secret to use
type: str
consumer_key:
required: true
description:
- The consumer key to use
type: str
timeout:
required: false
default: 120
description:
- The timeout in seconds used to wait for a task to be
completed. Default is 120 seconds.
type: int
'''
EXAMPLES = '''
# Route an IP address 1.1.1.1 to the service ns666.ovh.net
- community.general.ovh_ip_failover:
name: 1.1.1.1
service: ns666.ovh.net
endpoint: ovh-eu
application_key: yourkey
application_secret: yoursecret
consumer_key: yourconsumerkey
- community.general.ovh_ip_failover:
name: 1.1.1.1
service: ns666.ovh.net
endpoint: ovh-eu
wait_completion: false
application_key: yourkey
application_secret: yoursecret
consumer_key: yourconsumerkey
register: moved
- community.general.ovh_ip_failover:
name: 1.1.1.1
service: ns666.ovh.net
endpoint: ovh-eu
wait_task_completion: "{{moved.taskId}}"
application_key: yourkey
application_secret: yoursecret
consumer_key: yourconsumerkey
'''
RETURN = '''
'''
import time
try:
import ovh
import ovh.exceptions
from ovh.exceptions import APIError
HAS_OVH = True
except ImportError:
HAS_OVH = False
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.six.moves.urllib.parse import quote_plus
def getOvhClient(ansibleModule):
endpoint = ansibleModule.params.get('endpoint')
application_key = ansibleModule.params.get('application_key')
application_secret = ansibleModule.params.get('application_secret')
consumer_key = ansibleModule.params.get('consumer_key')
return ovh.Client(
endpoint=endpoint,
application_key=application_key,
application_secret=application_secret,
consumer_key=consumer_key
)
def waitForNoTask(client, name, timeout):
currentTimeout = timeout
while client.get('/ip/{0}/task'.format(quote_plus(name)),
function='genericMoveFloatingIp',
status='todo'):
time.sleep(1) # Delay for 1 sec
currentTimeout -= 1
if currentTimeout < 0:
return False
return True
def waitForTaskDone(client, name, taskId, timeout):
currentTimeout = timeout
while True:
task = client.get('/ip/{0}/task/{1}'.format(quote_plus(name), taskId))
if task['status'] == 'done':
return True
time.sleep(5) # Delay for 5 sec because it's long to wait completion, do not harass the API
currentTimeout -= 5
if currentTimeout < 0:
return False
def main():
module = AnsibleModule(
argument_spec=dict(
name=dict(required=True),
service=dict(required=True),
endpoint=dict(required=True),
wait_completion=dict(default=True, type='bool'),
wait_task_completion=dict(default=0, type='int'),
application_key=dict(required=True, no_log=True),
application_secret=dict(required=True, no_log=True),
consumer_key=dict(required=True, no_log=True),
timeout=dict(default=120, type='int')
),
supports_check_mode=True
)
result = dict(
changed=False
)
if not HAS_OVH:
module.fail_json(msg='ovh-api python module is required to run this module ')
# Get parameters
name = module.params.get('name')
service = module.params.get('service')
timeout = module.params.get('timeout')
wait_completion = module.params.get('wait_completion')
wait_task_completion = module.params.get('wait_task_completion')
# Connect to OVH API
client = getOvhClient(module)
# Check that the load balancing exists
try:
ips = client.get('/ip', ip=name, type='failover')
except APIError as apiError:
module.fail_json(
msg='Unable to call OVH api for getting the list of ips, '
'check application key, secret, consumerkey and parameters. '
'Error returned by OVH api was : {0}'.format(apiError))
if name not in ips and '{0}/32'.format(name) not in ips:
module.fail_json(msg='IP {0} does not exist'.format(name))
# Check that no task is pending before going on
try:
if not waitForNoTask(client, name, timeout):
module.fail_json(
msg='Timeout of {0} seconds while waiting for no pending '
'tasks before executing the module '.format(timeout))
except APIError as apiError:
module.fail_json(
msg='Unable to call OVH api for getting the list of pending tasks '
'of the ip, check application key, secret, consumerkey '
'and parameters. Error returned by OVH api was : {0}'
.format(apiError))
try:
ipproperties = client.get('/ip/{0}'.format(quote_plus(name)))
except APIError as apiError:
module.fail_json(
msg='Unable to call OVH api for getting the properties '
'of the ip, check application key, secret, consumerkey '
'and parameters. Error returned by OVH api was : {0}'
.format(apiError))
if ipproperties['routedTo']['serviceName'] != service:
if not module.check_mode:
if wait_task_completion == 0:
# Move the IP and get the created taskId
task = client.post('/ip/{0}/move'.format(quote_plus(name)), to=service)
taskId = task['taskId']
result['moved'] = True
else:
# Just wait for the given taskId to be completed
taskId = wait_task_completion
result['moved'] = False
result['taskId'] = taskId
if wait_completion or wait_task_completion != 0:
if not waitForTaskDone(client, name, taskId, timeout):
module.fail_json(
msg='Timeout of {0} seconds while waiting for completion '
'of move ip to service'.format(timeout))
result['waited'] = True
else:
result['waited'] = False
result['changed'] = True
module.exit_json(**result)
if __name__ == '__main__':
main()
|