| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright Ansible Project |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: ovh_ip_failover |
| 11 |
short_description: Manage OVH IP failover address |
| 12 |
description: |
| 13 |
- Manage OVH (French European hosting provider) IP Failover Address. For now, this module can only be used to move an IP |
| 14 |
failover (or failover block) between services. |
| 15 |
author: "Pascal HERAUD (@pascalheraud)" |
| 16 |
notes: |
| 17 |
- Uses the Python OVH API U(https://github.com/ovh/python-ovh). You have to create an application (a key and secret) with |
| 18 |
a consumer key as described into U(https://docs.ovh.com/gb/en/customer/first-steps-with-ovh-api/). |
| 19 |
requirements: |
| 20 |
- ovh >= 0.4.8 |
| 21 |
extends_documentation_fragment: |
| 22 |
- community.general._attributes |
| 23 |
attributes: |
| 24 |
check_mode: |
| 25 |
support: full |
| 26 |
diff_mode: |
| 27 |
support: none |
| 28 |
options: |
| 29 |
name: |
| 30 |
required: true |
| 31 |
description: |
| 32 |
- The IP address to manage (can be a single IP like V(1.1.1.1) or a block like V(1.1.1.1/28)). |
| 33 |
type: str |
| 34 |
service: |
| 35 |
required: true |
| 36 |
description: |
| 37 |
- The name of the OVH service this IP address should be routed. |
| 38 |
type: str |
| 39 |
endpoint: |
| 40 |
required: true |
| 41 |
description: |
| 42 |
- The endpoint to use (for instance V(ovh-eu)). |
| 43 |
type: str |
| 44 |
wait_completion: |
| 45 |
default: true |
| 46 |
type: bool |
| 47 |
description: |
| 48 |
- If V(true), the module waits for the IP address to be moved. If false, exit without waiting. The C(taskId) is returned |
| 49 |
in module output. |
| 50 |
wait_task_completion: |
| 51 |
default: 0 |
| 52 |
description: |
| 53 |
- If not V(0), the module waits for this task ID to be completed. Use O(wait_task_completion) if you want to wait for |
| 54 |
completion of a previously executed task with O(wait_completion=false). You can execute this module repeatedly on |
| 55 |
a list of failover IPs using O(wait_completion=false) (see examples). |
| 56 |
type: int |
| 57 |
application_key: |
| 58 |
required: true |
| 59 |
description: |
| 60 |
- The applicationKey to use. |
| 61 |
type: str |
| 62 |
application_secret: |
| 63 |
required: true |
| 64 |
description: |
| 65 |
- The application secret to use. |
| 66 |
type: str |
| 67 |
consumer_key: |
| 68 |
required: true |
| 69 |
description: |
| 70 |
- The consumer key to use. |
| 71 |
type: str |
| 72 |
timeout: |
| 73 |
default: 120 |
| 74 |
description: |
| 75 |
- The timeout in seconds used to wait for a task to be completed. Default is 120 seconds. |
| 76 |
type: int |
| 77 |
""" |
| 78 |
|
| 79 |
EXAMPLES = r""" |
| 80 |
# Route an IP address 1.1.1.1 to the service ns666.ovh.net |
| 81 |
- community.general.ovh_ip_failover: |
| 82 |
name: 1.1.1.1 |
| 83 |
service: ns666.ovh.net |
| 84 |
endpoint: ovh-eu |
| 85 |
application_key: yourkey |
| 86 |
application_secret: yoursecret |
| 87 |
consumer_key: yourconsumerkey |
| 88 |
- community.general.ovh_ip_failover: |
| 89 |
name: 1.1.1.1 |
| 90 |
service: ns666.ovh.net |
| 91 |
endpoint: ovh-eu |
| 92 |
wait_completion: false |
| 93 |
application_key: yourkey |
| 94 |
application_secret: yoursecret |
| 95 |
consumer_key: yourconsumerkey |
| 96 |
register: moved |
| 97 |
- community.general.ovh_ip_failover: |
| 98 |
name: 1.1.1.1 |
| 99 |
service: ns666.ovh.net |
| 100 |
endpoint: ovh-eu |
| 101 |
wait_task_completion: "{{moved.taskId}}" |
| 102 |
application_key: yourkey |
| 103 |
application_secret: yoursecret |
| 104 |
consumer_key: yourconsumerkey |
| 105 |
""" |
| 106 |
|
| 107 |
|
| 108 |
import time |
| 109 |
from urllib.parse import quote_plus |
| 110 |
|
| 111 |
try: |
| 112 |
import ovh |
| 113 |
import ovh.exceptions |
| 114 |
from ovh.exceptions import APIError |
| 115 |
|
| 116 |
HAS_OVH = True |
| 117 |
except ImportError: |
| 118 |
HAS_OVH = False |
| 119 |
|
| 120 |
from ansible.module_utils.basic import AnsibleModule |
| 121 |
|
| 122 |
|
| 123 |
def getOvhClient(ansibleModule): |
| 124 |
endpoint = ansibleModule.params.get("endpoint") |
| 125 |
application_key = ansibleModule.params.get("application_key") |
| 126 |
application_secret = ansibleModule.params.get("application_secret") |
| 127 |
consumer_key = ansibleModule.params.get("consumer_key") |
| 128 |
|
| 129 |
return ovh.Client( |
| 130 |
endpoint=endpoint, |
| 131 |
application_key=application_key, |
| 132 |
application_secret=application_secret, |
| 133 |
consumer_key=consumer_key, |
| 134 |
) |
| 135 |
|
| 136 |
|
| 137 |
def waitForNoTask(client, name, timeout): |
| 138 |
currentTimeout = timeout |
| 139 |
while client.get(f"/ip/{quote_plus(name)}/task", function="genericMoveFloatingIp", status="todo"): |
| 140 |
time.sleep(1) # Delay for 1 sec |
| 141 |
currentTimeout -= 1 |
| 142 |
if currentTimeout < 0: |
| 143 |
return False |
| 144 |
return True |
| 145 |
|
| 146 |
|
| 147 |
def waitForTaskDone(client, name, taskId, timeout): |
| 148 |
currentTimeout = timeout |
| 149 |
while True: |
| 150 |
task = client.get(f"/ip/{quote_plus(name)}/task/{taskId}") |
| 151 |
if task["status"] == "done": |
| 152 |
return True |
| 153 |
time.sleep(5) # Delay for 5 sec to not harass the API |
| 154 |
currentTimeout -= 5 |
| 155 |
if currentTimeout < 0: |
| 156 |
return False |
| 157 |
|
| 158 |
|
| 159 |
def main(): |
| 160 |
module = AnsibleModule( |
| 161 |
argument_spec=dict( |
| 162 |
name=dict(required=True), |
| 163 |
service=dict(required=True), |
| 164 |
endpoint=dict(required=True), |
| 165 |
wait_completion=dict(default=True, type="bool"), |
| 166 |
wait_task_completion=dict(default=0, type="int"), |
| 167 |
application_key=dict(required=True, no_log=True), |
| 168 |
application_secret=dict(required=True, no_log=True), |
| 169 |
consumer_key=dict(required=True, no_log=True), |
| 170 |
timeout=dict(default=120, type="int"), |
| 171 |
), |
| 172 |
supports_check_mode=True, |
| 173 |
) |
| 174 |
|
| 175 |
result = dict(changed=False) |
| 176 |
|
| 177 |
if not HAS_OVH: |
| 178 |
module.fail_json(msg="ovh-api python module is required to run this module ") |
| 179 |
|
| 180 |
# Get parameters |
| 181 |
name = module.params.get("name") |
| 182 |
service = module.params.get("service") |
| 183 |
timeout = module.params.get("timeout") |
| 184 |
wait_completion = module.params.get("wait_completion") |
| 185 |
wait_task_completion = module.params.get("wait_task_completion") |
| 186 |
|
| 187 |
# Connect to OVH API |
| 188 |
client = getOvhClient(module) |
| 189 |
|
| 190 |
# Check that the load balancing exists |
| 191 |
try: |
| 192 |
ips = client.get("/ip", ip=name, type="failover") |
| 193 |
except APIError as apiError: |
| 194 |
module.fail_json( |
| 195 |
msg=f"Unable to call OVH API for getting the list of ips, check application key, secret, consumerkey and parameters. " |
| 196 |
f"Error returned by OVH API was : {apiError}" |
| 197 |
) |
| 198 |
|
| 199 |
if name not in ips and f"{name}/32" not in ips: |
| 200 |
module.fail_json(msg=f"IP {name} does not exist") |
| 201 |
|
| 202 |
# Check that no task is pending before going on |
| 203 |
try: |
| 204 |
if not waitForNoTask(client, name, timeout): |
| 205 |
module.fail_json( |
| 206 |
msg=f"Timeout of {timeout} seconds while waiting for no pending tasks before executing the module " |
| 207 |
) |
| 208 |
except APIError as apiError: |
| 209 |
module.fail_json( |
| 210 |
msg=f"Unable to call OVH API for getting the list of pending tasks of the ip, check application key, secret, consumerkey and parameters. " |
| 211 |
f"Error returned by OVH API was : {apiError}" |
| 212 |
) |
| 213 |
|
| 214 |
try: |
| 215 |
ipproperties = client.get(f"/ip/{quote_plus(name)}") |
| 216 |
except APIError as apiError: |
| 217 |
module.fail_json( |
| 218 |
msg=f"Unable to call OVH API for getting the properties of the ip, check application key, secret, consumerkey and parameters. " |
| 219 |
f"Error returned by OVH API was : {apiError}" |
| 220 |
) |
| 221 |
|
| 222 |
if ipproperties["routedTo"]["serviceName"] != service: |
| 223 |
if not module.check_mode: |
| 224 |
if wait_task_completion == 0: |
| 225 |
# Move the IP and get the created taskId |
| 226 |
task = client.post(f"/ip/{quote_plus(name)}/move", to=service) |
| 227 |
taskId = task["taskId"] |
| 228 |
result["moved"] = True |
| 229 |
else: |
| 230 |
# Just wait for the given taskId to be completed |
| 231 |
taskId = wait_task_completion |
| 232 |
result["moved"] = False |
| 233 |
result["taskId"] = taskId |
| 234 |
if wait_completion or wait_task_completion != 0: |
| 235 |
if not waitForTaskDone(client, name, taskId, timeout): |
| 236 |
module.fail_json( |
| 237 |
msg=f"Timeout of {timeout} seconds while waiting for completion of move ip to service" |
| 238 |
) |
| 239 |
result["waited"] = True |
| 240 |
else: |
| 241 |
result["waited"] = False |
| 242 |
result["changed"] = True |
| 243 |
|
| 244 |
module.exit_json(**result) |
| 245 |
|
| 246 |
|
| 247 |
if __name__ == "__main__": |
| 248 |
main() |