Hi,
Could we possibly add a rebuild function to the rax module (usually at /usr/share/ansible/cloud/rax)?
In Rackspace if a cloud server is deleted and a new one is created the IP is “lost” (i.e. re-assigned to another random cloud server that will need it), so a rebuild function would be useful.
I believe that pyrax already does this: https://gist.github.com/rgbkrk/6994544 (found online).
I began drafting the function (untested), but have never developed anything for ansible so I’d like your help/opinion.
—snip—
def rebuild(module, instance_ids=, image=None, wait=True, wait_timeout=300, kept=, rebuild=True):
cs = pyrax.cloudservers
changed = False
instances = {}
servers =
for instance_id in instance_ids:
servers.append(cs.servers.get(instance_id))
for server in servers:
try:
server.rebuild(image=image)
except Exception, e:
module.fail_json(msg=e.message)
else:
changed = True
instance = rax_to_dict(server, ‘server’)
instances[instance[‘id’]] = instance
If requested, wait for server rebuild
if wait:
end_time = time.time() + wait_timeout
infinite = wait_timeout == 0
while infinite or time.time() < end_time:
for server in servers:
instance_id = server.id
try:
server.get()
except:
server.status == ‘ERROR’
if not filter(lambda s: s.status not in (‘’, ‘ERROR’), servers):
break
time.sleep(5)
timeout = filter(lambda s: s[‘status’] not in (‘’, ‘ERROR’),
instances.values())
error = filter(lambda s: s[‘status’] in (‘ERROR’),
instances.values())
success = filter(lambda s: s[‘status’] in (‘’, ‘ACTIVE’),
instances.values())
instances = [rax_to_dict(s, ‘server’) for s in kept]
results = {
‘changed’: changed,
‘action’: ‘rebuild’,
‘instances’: instances,
‘success’: success,
‘error’: error,
‘timeout’: timeout,
‘instance_ids’: {
‘instances’: [i[‘id’] for i in instances],
‘success’: [i[‘id’] for i in success],
‘error’: [i[‘id’] for i in error],
‘timeout’: [i[‘id’] for i in timeout]
}
}
if timeout:
results[‘msg’] = ‘Timeout waiting for all servers to rebuild’
elif error:
results[‘msg’] = ‘Failed to rebuild all servers’
if ‘msg’ in results:
module.fail_json(**results)
else:
module.exit_json(**results)
—snip—
Thanks
Miguel