Initial commit

This commit is contained in:
Ansible Core Team 2020-03-09 09:11:07 +00:00
commit aebc1b03fd
4861 changed files with 812621 additions and 0 deletions

View file

@ -0,0 +1,258 @@
#!/usr/bin/python
# Copyright: Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'}
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 consummer
key as described into U(https://docs.ovh.com/gb/en/customer/first-steps-with-ovh-api/)
requirements:
- ovh >= 0.4.8
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 )
service:
required: true
description:
- The name of the OVH service this IP address should be routed
endpoint:
required: true
description:
- The endpoint to use ( for instance ovh-eu)
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)
application_key:
required: true
description:
- The applicationKey to use
application_secret:
required: true
description:
- The application secret to use
consumer_key:
required: true
description:
- The consumer key to use
timeout:
required: false
default: 120
description:
- The timeout in seconds used to wait for a task to be
completed. Default is 120 seconds.
'''
EXAMPLES = '''
# Route an IP address 1.1.1.1 to the service ns666.ovh.net
- ovh_ip_failover:
name: 1.1.1.1
service: ns666.ovh.net
endpoint: ovh-eu
application_key: yourkey
application_secret: yoursecret
consumer_key: yourconsumerkey
- 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
- 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
return True
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()

View file

@ -0,0 +1,307 @@
#!/usr/bin/python
# Copyright: Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'}
DOCUMENTATION = '''
---
module: ovh_ip_loadbalancing_backend
short_description: Manage OVH IP LoadBalancing backends
description:
- Manage OVH (French European hosting provider) LoadBalancing IP backends
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.3.5
options:
name:
required: true
description:
- Name of the LoadBalancing internal name (ip-X.X.X.X)
backend:
required: true
description:
- The IP address of the backend to update / modify / delete
state:
default: present
choices: ['present', 'absent']
description:
- Determines whether the backend is to be created/modified
or deleted
probe:
default: 'none'
choices: ['none', 'http', 'icmp' , 'oco']
description:
- Determines the type of probe to use for this backend
weight:
default: 8
description:
- Determines the weight for this backend
endpoint:
required: true
description:
- The endpoint to use ( for instance ovh-eu)
application_key:
required: true
description:
- The applicationKey to use
application_secret:
required: true
description:
- The application secret to use
consumer_key:
required: true
description:
- The consumer key to use
timeout:
default: 120
description:
- The timeout in seconds used to wait for a task to be
completed.
'''
EXAMPLES = '''
# Adds or modify the backend '212.1.1.1' to a
# loadbalancing 'ip-1.1.1.1'
- ovh_ip_loadbalancing:
name: ip-1.1.1.1
backend: 212.1.1.1
state: present
probe: none
weight: 8
endpoint: ovh-eu
application_key: yourkey
application_secret: yoursecret
consumer_key: yourconsumerkey
# Removes a backend '212.1.1.1' from a loadbalancing 'ip-1.1.1.1'
- ovh_ip_loadbalancing:
name: ip-1.1.1.1
backend: 212.1.1.1
state: absent
endpoint: ovh-eu
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
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 len(client.get('/ip/loadBalancing/{0}/task'.format(name))) > 0:
time.sleep(1) # Delay for 1 sec
currentTimeout -= 1
if currentTimeout < 0:
return False
return True
def main():
module = AnsibleModule(
argument_spec=dict(
name=dict(required=True),
backend=dict(required=True),
weight=dict(default=8, type='int'),
probe=dict(default='none',
choices=['none', 'http', 'icmp', 'oco']),
state=dict(default='present', choices=['present', 'absent']),
endpoint=dict(required=True),
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')
)
)
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')
state = module.params.get('state')
backend = module.params.get('backend')
weight = module.params.get('weight')
probe = module.params.get('probe')
timeout = module.params.get('timeout')
# Connect to OVH API
client = getOvhClient(module)
# Check that the load balancing exists
try:
loadBalancings = client.get('/ip/loadBalancing')
except APIError as apiError:
module.fail_json(
msg='Unable to call OVH api for getting the list of loadBalancing, '
'check application key, secret, consumerkey and parameters. '
'Error returned by OVH api was : {0}'.format(apiError))
if name not in loadBalancings:
module.fail_json(msg='IP LoadBalancing {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 loadBalancing, check application key, secret, consumerkey '
'and parameters. Error returned by OVH api was : {0}'
.format(apiError))
try:
backends = client.get('/ip/loadBalancing/{0}/backend'.format(name))
except APIError as apiError:
module.fail_json(
msg='Unable to call OVH api for getting the list of backends '
'of the loadBalancing, check application key, secret, consumerkey '
'and parameters. Error returned by OVH api was : {0}'
.format(apiError))
backendExists = backend in backends
moduleChanged = False
if state == "absent":
if backendExists:
# Remove backend
try:
client.delete(
'/ip/loadBalancing/{0}/backend/{1}'.format(name, backend))
if not waitForNoTask(client, name, timeout):
module.fail_json(
msg='Timeout of {0} seconds while waiting for completion '
'of removing backend task'.format(timeout))
except APIError as apiError:
module.fail_json(
msg='Unable to call OVH api for deleting the backend, '
'check application key, secret, consumerkey and '
'parameters. Error returned by OVH api was : {0}'
.format(apiError))
moduleChanged = True
else:
if backendExists:
# Get properties
try:
backendProperties = client.get(
'/ip/loadBalancing/{0}/backend/{1}'.format(name, backend))
except APIError as apiError:
module.fail_json(
msg='Unable to call OVH api for getting the backend properties, '
'check application key, secret, consumerkey and '
'parameters. Error returned by OVH api was : {0}'
.format(apiError))
if (backendProperties['weight'] != weight):
# Change weight
try:
client.post(
'/ip/loadBalancing/{0}/backend/{1}/setWeight'
.format(name, backend), weight=weight)
if not waitForNoTask(client, name, timeout):
module.fail_json(
msg='Timeout of {0} seconds while waiting for completion '
'of setWeight to backend task'
.format(timeout))
except APIError as apiError:
module.fail_json(
msg='Unable to call OVH api for updating the weight of the '
'backend, check application key, secret, consumerkey '
'and parameters. Error returned by OVH api was : {0}'
.format(apiError))
moduleChanged = True
if (backendProperties['probe'] != probe):
# Change probe
backendProperties['probe'] = probe
try:
client.put(
'/ip/loadBalancing/{0}/backend/{1}'
.format(name, backend), probe=probe)
if not waitForNoTask(client, name, timeout):
module.fail_json(
msg='Timeout of {0} seconds while waiting for completion of '
'setProbe to backend task'
.format(timeout))
except APIError as apiError:
module.fail_json(
msg='Unable to call OVH api for updating the probe of '
'the backend, check application key, secret, '
'consumerkey and parameters. Error returned by OVH api '
'was : {0}'
.format(apiError))
moduleChanged = True
else:
# Creates backend
try:
try:
client.post('/ip/loadBalancing/{0}/backend'.format(name),
ipBackend=backend, probe=probe, weight=weight)
except APIError as apiError:
module.fail_json(
msg='Unable to call OVH api for creating the backend, check '
'application key, secret, consumerkey and parameters. '
'Error returned by OVH api was : {0}'
.format(apiError))
if not waitForNoTask(client, name, timeout):
module.fail_json(
msg='Timeout of {0} seconds while waiting for completion of '
'backend creation task'.format(timeout))
except APIError as apiError:
module.fail_json(
msg='Unable to call OVH api for creating the backend, check '
'application key, secret, consumerkey and parameters. '
'Error returned by OVH api was : {0}'.format(apiError))
moduleChanged = True
module.exit_json(changed=moduleChanged)
if __name__ == '__main__':
main()

View file

@ -0,0 +1,163 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: (c) 2019, Francois Lallart (@fraff)
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
ANSIBLE_METADATA = {
"metadata_version": "1.1",
"status": ["preview"],
"supported_by": "community",
}
DOCUMENTATION = '''
---
module: ovh_monthly_billing
author: Francois Lallart (@fraff)
short_description: Manage OVH monthly billing
description:
- Enable monthly billing on OVH cloud intances (be aware OVH does not allow to disable it).
requirements: [ "ovh" ]
options:
project_id:
required: true
type: str
description:
- ID of the project, get it with U(https://api.ovh.com/console/#/cloud/project#GET)
instance_id:
required: true
type: str
description:
- ID of the instance, get it with U(https://api.ovh.com/console/#/cloud/project/%7BserviceName%7D/instance#GET)
endpoint:
type: str
description:
- The endpoint to use (for instance ovh-eu)
application_key:
type: str
description:
- The applicationKey to use
application_secret:
type: str
description:
- The application secret to use
consumer_key:
type: str
description:
- The consumer key to use
'''
EXAMPLES = '''
# basic usage, using auth from /etc/ovh.conf
- ovh_monthly_billing:
project_id: 0c727a20aa144485b70c44dee9123b46
instance_id: 8fa89ad2-8f08-4220-9fa4-9695ea23e948
# a bit more more complex
# get openstack cloud ID and instance ID, OVH use them in its API
- os_server_info:
cloud: myProjectName
region_name: myRegionName
server: myServerName
# force run even in check_mode
check_mode: no
# use theses IDs
- ovh_monthly_billing:
project_id: "{{ openstack_servers.0.tenant_id }}"
instance_id: "{{ openstack_servers.0.id }}"
application_key: yourkey
application_secret: yoursecret
consumer_key: yourconsumerkey
'''
RETURN = '''
'''
import os
import sys
import traceback
try:
import ovh
import ovh.exceptions
from ovh.exceptions import APIError
HAS_OVH = True
except ImportError:
HAS_OVH = False
OVH_IMPORT_ERROR = traceback.format_exc()
from ansible.module_utils.basic import AnsibleModule
def main():
module = AnsibleModule(
argument_spec=dict(
project_id=dict(required=True),
instance_id=dict(required=True),
endpoint=dict(required=False),
application_key=dict(required=False, no_log=True),
application_secret=dict(required=False, no_log=True),
consumer_key=dict(required=False, no_log=True),
),
supports_check_mode=True
)
# Get parameters
project_id = module.params.get('project_id')
instance_id = module.params.get('instance_id')
endpoint = module.params.get('endpoint')
application_key = module.params.get('application_key')
application_secret = module.params.get('application_secret')
consumer_key = module.params.get('consumer_key')
project = ""
instance = ""
ovh_billing_status = ""
if not HAS_OVH:
module.fail_json(msg='python-ovh is required to run this module, see https://github.com/ovh/python-ovh')
# Connect to OVH API
client = ovh.Client(
endpoint=endpoint,
application_key=application_key,
application_secret=application_secret,
consumer_key=consumer_key
)
# Check that the instance exists
try:
project = client.get('/cloud/project/{0}'.format(project_id))
except ovh.exceptions.ResourceNotFoundError:
module.fail_json(msg='project {0} does not exist'.format(project_id))
# Check that the instance exists
try:
instance = client.get('/cloud/project/{0}/instance/{1}'.format(project_id, instance_id))
except ovh.exceptions.ResourceNotFoundError:
module.fail_json(msg='instance {0} does not exist in project {1}'.format(instance_id, project_id))
# Is monthlyBilling already enabled or pending ?
if instance['monthlyBilling'] is not None:
if instance['monthlyBilling']['status'] in ['ok', 'activationPending']:
module.exit_json(changed=False, ovh_billing_status=instance['monthlyBilling'])
if module.check_mode:
module.exit_json(changed=True, msg="Dry Run!")
try:
ovh_billing_status = client.post('/cloud/project/{0}/instance/{1}/activeMonthlyBilling'.format(project_id, instance_id))
module.exit_json(changed=True, ovh_billing_status=ovh_billing_status['monthlyBilling'])
except APIError as apiError:
module.fail_json(changed=False, msg="Failed to call OVH API: {0}".format(apiError))
# We should never reach here
module.fail_json(msg='Internal ovh_monthly_billing module error')
if __name__ == "__main__":
main()