Utilize ansible.module_utils.rax

This commit is contained in:
Matt Martz 2013-12-02 14:03:10 -06:00
parent 4a0efeceab
commit 77377be11c

View file

@ -41,7 +41,7 @@ options:
default: null
region:
description:
- Region to create the load balancer in
- Region to create the key pair in
default: DFW
username:
description:
@ -89,7 +89,6 @@ EXAMPLES = '''
'''
import sys
import os
from types import NoneType
@ -148,49 +147,32 @@ def rax_keypair(module, name, public_key, state):
def main():
module = AnsibleModule(
argument_spec=dict(
api_key=dict(),
credentials=dict(aliases=['creds_file']),
argument_spec = rax_argument_spec()
argument_spec.update(
dict(
name=dict(),
public_key=dict(),
region=dict(),
state=dict(default='present', choices=['absent', 'present']),
username=dict(),
),
)
)
module = AnsibleModule(
argument_spec=argument_spec,
required_together=rax_required_together(),
)
api_key = module.params.get('api_key')
credentials = module.params.get('credentials')
name = module.params.get('name')
public_key = module.params.get('public_key')
region = module.params.get('region')
state = module.params.get('state')
username = module.params.get('username')
try:
username = username or os.environ.get('RAX_USERNAME')
api_key = api_key or os.environ.get('RAX_API_KEY')
credentials = (credentials or os.environ.get('RAX_CREDENTIALS') or
os.environ.get('RAX_CREDS_FILE'))
region = region or os.environ.get('RAX_REGION')
except KeyError, e:
module.fail_json(msg='Unable to load %s' % e.message)
try:
pyrax.set_setting('identity_type', 'rackspace')
if api_key and username:
pyrax.set_credentials(username, api_key=api_key, region=region)
elif credentials:
credentials = os.path.expanduser(credentials)
pyrax.set_credential_file(credentials, region=region)
else:
raise Exception('No credentials supplied!')
except Exception, e:
module.fail_json(msg='%s' % e.message)
setup_rax_module(module, pyrax)
rax_keypair(module, name, public_key, state)
from ansible.module_utils.basic import *
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.rax import *
### invoke the module
main()