Add user_data and config_drive support

This commit is contained in:
Matt Martz 2014-05-21 07:16:10 -05:00
parent 6bc056e012
commit 4ca7840861

View file

@ -31,7 +31,18 @@ options:
created servers. Only applicable when used with the I(group) attribute created servers. Only applicable when used with the I(group) attribute
or meta key. or meta key.
default: yes default: yes
choices:
- "yes"
- "no"
version_added: 1.5 version_added: 1.5
config_drive:
description:
- Attach read-only configuration drive to server as label config-2
default: no
choices:
- "yes"
- "no"
version_added: 1.7
count: count:
description: description:
- number of instances to launch - number of instances to launch
@ -55,6 +66,9 @@ options:
- Explicitly ensure an exact count of instances, used with - Explicitly ensure an exact count of instances, used with
state=active/present state=active/present
default: no default: no
choices:
- "yes"
- "no"
version_added: 1.4 version_added: 1.4
extra_client_args: extra_client_args:
description: description:
@ -119,6 +133,11 @@ options:
- present - present
- absent - absent
default: present default: present
user_data:
description:
- Data to be uploaded to the servers config drive. This option implies
I(config_drive)
version_added: 1.7
wait: wait:
description: description:
- wait for the instance to be in state 'running' before returning - wait for the instance to be in state 'running' before returning
@ -216,17 +235,29 @@ def server_to_dict(obj):
def create(module, names, flavor, image, meta, key_name, files, def create(module, names, flavor, image, meta, key_name, files,
wait, wait_timeout, disk_config, group, nics, wait, wait_timeout, disk_config, group, nics,
extra_create_args, existing=[]): extra_create_args, user_data, config_drive, existing=[]):
cs = pyrax.cloudservers cs = pyrax.cloudservers
changed = False changed = False
if user_data:
config_drive = True
if user_data and os.path.isfile(user_data):
try:
f = open(user_data)
user_data = f.read()
f.close()
except Exception, e:
module.fail_json(msg='Failed to load %s' % user_data)
# Handle the file contents # Handle the file contents
for rpath in files.keys(): for rpath in files.keys():
lpath = os.path.expanduser(files[rpath]) lpath = os.path.expanduser(files[rpath])
try: try:
fileobj = open(lpath, 'r') fileobj = open(lpath, 'r')
files[rpath] = fileobj.read() files[rpath] = fileobj.read()
fileobj.close()
except Exception, e: except Exception, e:
module.fail_json(msg='Failed to load %s' % lpath) module.fail_json(msg='Failed to load %s' % lpath)
try: try:
@ -237,6 +268,8 @@ def create(module, names, flavor, image, meta, key_name, files,
key_name=key_name, key_name=key_name,
files=files, nics=nics, files=files, nics=nics,
disk_config=disk_config, disk_config=disk_config,
config_drive=config_drive,
userdata=user_data,
**extra_create_args)) **extra_create_args))
except Exception, e: except Exception, e:
module.fail_json(msg='%s' % e.message) module.fail_json(msg='%s' % e.message)
@ -382,7 +415,7 @@ def delete(module, instance_ids, wait, wait_timeout, kept=[]):
def cloudservers(module, state, name, flavor, image, meta, key_name, files, def cloudservers(module, state, name, flavor, image, meta, key_name, files,
wait, wait_timeout, disk_config, count, group, wait, wait_timeout, disk_config, count, group,
instance_ids, exact_count, networks, count_offset, instance_ids, exact_count, networks, count_offset,
auto_increment, extra_create_args): auto_increment, extra_create_args, user_data, config_drive):
cs = pyrax.cloudservers cs = pyrax.cloudservers
cnw = pyrax.cloud_networks cnw = pyrax.cloud_networks
if not cnw: if not cnw:
@ -596,7 +629,7 @@ def cloudservers(module, state, name, flavor, image, meta, key_name, files,
create(module, names, flavor, image, meta, key_name, files, create(module, names, flavor, image, meta, key_name, files,
wait, wait_timeout, disk_config, group, nics, extra_create_args, wait, wait_timeout, disk_config, group, nics, extra_create_args,
existing=servers) user_data, config_drive, existing=servers)
elif state == 'absent': elif state == 'absent':
if instance_ids is None: if instance_ids is None:
@ -637,6 +670,7 @@ def main():
argument_spec.update( argument_spec.update(
dict( dict(
auto_increment=dict(default=True, type='bool'), auto_increment=dict(default=True, type='bool'),
config_drive=dict(default=False, type='bool'),
count=dict(default=1, type='int'), count=dict(default=1, type='int'),
count_offset=dict(default=1, type='int'), count_offset=dict(default=1, type='int'),
disk_config=dict(choices=['auto', 'manual']), disk_config=dict(choices=['auto', 'manual']),
@ -654,6 +688,7 @@ def main():
networks=dict(type='list', default=['public', 'private']), networks=dict(type='list', default=['public', 'private']),
service=dict(), service=dict(),
state=dict(default='present', choices=['present', 'absent']), state=dict(default='present', choices=['present', 'absent']),
user_data=dict(no_log=True),
wait=dict(default=False, type='bool'), wait=dict(default=False, type='bool'),
wait_timeout=dict(default=300), wait_timeout=dict(default=300),
) )
@ -675,6 +710,7 @@ def main():
'playbook pertaining to the "rax" module') 'playbook pertaining to the "rax" module')
auto_increment = module.params.get('auto_increment') auto_increment = module.params.get('auto_increment')
config_drive = module.params.get('config_drive')
count = module.params.get('count') count = module.params.get('count')
count_offset = module.params.get('count_offset') count_offset = module.params.get('count_offset')
disk_config = module.params.get('disk_config') disk_config = module.params.get('disk_config')
@ -693,6 +729,7 @@ def main():
name = module.params.get('name') name = module.params.get('name')
networks = module.params.get('networks') networks = module.params.get('networks')
state = module.params.get('state') state = module.params.get('state')
user_data = module.params.get('user_data')
wait = module.params.get('wait') wait = module.params.get('wait')
wait_timeout = int(module.params.get('wait_timeout')) wait_timeout = int(module.params.get('wait_timeout'))
@ -714,7 +751,7 @@ def main():
cloudservers(module, state, name, flavor, image, meta, key_name, files, cloudservers(module, state, name, flavor, image, meta, key_name, files,
wait, wait_timeout, disk_config, count, group, wait, wait_timeout, disk_config, count, group,
instance_ids, exact_count, networks, count_offset, instance_ids, exact_count, networks, count_offset,
auto_increment, extra_create_args) auto_increment, extra_create_args, user_data, config_drive)
# import module snippets # import module snippets