Improvements in the packet_device module (#23127)

* Improvements and fixes in the packet_device module

* add version_added to new args

* remove default value from facility

* changed 'lock' from deprecated arg to alias of 'locked'
This commit is contained in:
Tomáš Karásek 2017-08-22 16:43:37 +03:00 committed by Toshio Kuratomi
parent 0954fadff7
commit b3a90253fc
2 changed files with 160 additions and 79 deletions

View file

@ -13,19 +13,19 @@ ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'], 'status': ['preview'],
'supported_by': 'community'} 'supported_by': 'community'}
DOCUMENTATION = ''' DOCUMENTATION = '''
--- ---
module: packet_device module: packet_device
short_description: create, destroy, start, stop, and reboot a Packet Host machine. short_description: Manage a bare metal server in the Packet Host.
description: description:
- create, destroy, update, start, stop, and reboot a Packet Host machine. When the machine is created it can optionally wait for it to have an - Manage a bare metal server in the Packet Host (a "device" in the API terms).
IP address before returning. This module has a dependency on packet >= 1.0. - When the machine is created it can optionally wait for public IP address, or for active state.
- API is documented at U(https://www.packet.net/help/api/#page:devices,header:devices-devices-post). - This module has a dependency on packet >= 1.0.
- API is documented at U(https://www.packet.net/developers/api/devices).
version_added: 2.3 version_added: "2.3"
author: author:
- Tomas Karasek (@t0mk) <tom.to.the.k@gmail.com> - Tomas Karasek (@t0mk) <tom.to.the.k@gmail.com>
@ -40,10 +40,12 @@ options:
count: count:
description: description:
- The number of devices to create. Count number can be included in hostname via the %d string formatter. - The number of devices to create. Count number can be included in hostname via the %d string formatter.
default: 1
count_offset: count_offset:
description: description:
- From which number to start the count. - From which number to start the count.
default: 1
device_ids: device_ids:
description: description:
@ -51,7 +53,7 @@ options:
facility: facility:
description: description:
- Facility slug for device creation. As of 2016, it should be one of [ewr1, sjc1, ams1, nrt1]. - Facility slug for device creation. See Packet API for current list - U(https://www.packet.net/developers/api/facilities/).
features: features:
description: description:
@ -60,22 +62,24 @@ options:
hostnames: hostnames:
description: description:
- A hostname of a device, or a list of hostnames. - A hostname of a device, or a list of hostnames.
- If given string or one-item list, you can use the C("%d") Python string format to expand numbers from count. - If given string or one-item list, you can use the C("%d") Python string format to expand numbers from I(count).
- If only one hostname, it might be expanded to list if count>1. - If only one hostname, it might be expanded to list if I(count)>1.
aliases: [name] aliases: [name]
lock: locked:
description: description:
- Whether to lock a created device. - Whether to lock a created device.
default: false default: false
version_added: "2.4"
aliases: [lock]
operating_system: operating_system:
description: description:
- OS slug for device creation. See Packet docs or API for current list. - OS slug for device creation. See Packet API for current list - U(https://www.packet.net/developers/api/operatingsystems/).
plan: plan:
description: description:
- Plan slug for device creation. See Packet docs or API for current list. - Plan slug for device creation. See Packet API for current list - U(https://www.packet.net/developers/api/plans/).
project_id: project_id:
description: description:
@ -85,30 +89,53 @@ options:
state: state:
description: description:
- Desired state of the device. - Desired state of the device.
- If set to C(present) (the default), the module call will return immediately after the device-creating HTTP request successfully returns.
- If set to C(active), the module call will block until all the specified devices are in state active due to the Packet API, or until I(wait_timeout).
choices: [present, absent, active, inactive, rebooted] choices: [present, absent, active, inactive, rebooted]
default: 'present' default: present
user_data: user_data:
description: description:
- Userdata blob made available to the machine - Userdata blob made available to the machine
required: false
default: None
wait: wait:
description: description:
- Whether to wait for the instance to be assigned IP address before returning. - Whether to wait for the instance to be assigned IP address before returning.
required: false - This option has been deprecated in favor of C(wait_for_public_IPv).
default: False default: false
type: bool
wait_for_public_IPv:
description:
- Whether to wait for the instance to be assigned a public IPv4/IPv6 address.
- If set to 4, it will wait until IPv4 is assigned to the instance.
- If set to 6, wait until public IPv6 is assigned to the instance.
choices: [4,6]
version_added: "2.4"
wait_timeout: wait_timeout:
description: description:
- How long to wait for IP address of new devices before quitting. In seconds. - How long (seconds) to wait either for automatic IP address assignment, or for the device to reach the C(active) I(state).
default: 60 - If I(wait_for_public_IPv) is set and I(state) is C(active), the module will wait for both events consequently, applying the timeout twice.
default: 900
ipxe_script_url:
description:
- URL of custom iPXE script for provisioning.
- More about custome iPXE for Packet devices at U(https://help.packet.net/technical/infrastructure/custom-ipxe).
version_added: "2.4"
always_pxe:
description:
- Persist PXE as the first boot option.
- Normally, the PXE process happens only on the first boot. Set this arg to have your device continuously boot to iPXE.
default: false
version_added: "2.4"
requirements: requirements:
- packet-python - "packet-python >= 1.35"
- "python >= 2.6"
notes:
- Doesn't support check mode.
''' '''
EXAMPLES = ''' EXAMPLES = '''
@ -127,6 +154,22 @@ EXAMPLES = '''
plan: baremetal_0 plan: baremetal_0
facility: sjc1 facility: sjc1
# Create the same device and wait until it is in state "active", (when it's
# ready for other API operations). Fail if the devices in not "active" in
# 10 minutes.
- name: create device and wait up to 10 minutes for active state
hosts: localhost
tasks:
- packet_device:
project_id: 89b497ee-5afc-420a-8fb5-56984898f4df
hostnames: myserver
operating_system: ubuntu_16_04
plan: baremetal_0
facility: sjc1
state: active
wait_timeout: 600
- name: create 3 ubuntu devices called server-01, server-02 and server-03 - name: create 3 ubuntu devices called server-01, server-02 and server-03
hosts: localhost hosts: localhost
tasks: tasks:
@ -149,6 +192,7 @@ EXAMPLES = '''
facility: ewr1 facility: ewr1
locked: true locked: true
project_id: 89b497ee-5afc-420a-8fb5-56984898f4df project_id: 89b497ee-5afc-420a-8fb5-56984898f4df
wait_for_public_IPv: 4
user_data: | user_data: |
#cloud-config #cloud-config
ssh_authorized_keys: ssh_authorized_keys:
@ -196,12 +240,16 @@ changed:
description: True if a device was altered in any way (created, modified or removed) description: True if a device was altered in any way (created, modified or removed)
type: bool type: bool
sample: True sample: True
returned: always returned: success
devices: devices:
description: Information about each device that was processed description: Information about each device that was processed
type: list type: list
sample: '[{"hostname": "my-server.com", "id": "server-id", "public-ipv4": "147.229.15.12", "private-ipv4": "10.0.15.12", "public-ipv6": ""2604:1380:2:5200::3"}]' sample: '[{"hostname": "my-server.com", "id": "2a5122b9-c323-4d5c-b53c-9ad3f54273e7",
returned: always "public_ipv4": "147.229.15.12", "private-ipv4": "10.0.15.12",
"tags": [], "locked": false, "state": "provisioning",
"public_ipv6": ""2604:1380:2:5200::3"}]'
returned: success
''' # NOQA ''' # NOQA
@ -209,6 +257,10 @@ import os
import re import re
import time import time
import uuid import uuid
import traceback
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_native
HAS_PACKET_SDK = True HAS_PACKET_SDK = True
try: try:
@ -249,6 +301,7 @@ def serialize_device(device):
'hostname': 'device_hostname', 'hostname': 'device_hostname',
'tags': [], 'tags': [],
'locked': false, 'locked': false,
'state': 'provisioning',
'ip_addresses': [ 'ip_addresses': [
{ {
"address": "147.75.194.227", "address": "147.75.194.227",
@ -277,6 +330,7 @@ def serialize_device(device):
device_data['hostname'] = device.hostname device_data['hostname'] = device.hostname
device_data['tags'] = device.tags device_data['tags'] = device.tags
device_data['locked'] = device.locked device_data['locked'] = device.locked
device_data['state'] = device.state
device_data['ip_addresses'] = [ device_data['ip_addresses'] = [
{ {
'address': addr_data['address'], 'address': addr_data['address'],
@ -321,7 +375,7 @@ def is_valid_uuid(myuuid):
def listify_string_name_or_id(s): def listify_string_name_or_id(s):
if ',' in s: if ',' in s:
return [i for i in s.split(',')] return s.split(',')
else: else:
return [s] return [s]
@ -360,7 +414,7 @@ def get_hostname_list(module):
raise Exception("Hostname '%s' does not seem to be valid" % hn) raise Exception("Hostname '%s' does not seem to be valid" % hn)
if len(hostnames) > MAX_DEVICES: if len(hostnames) > MAX_DEVICES:
raise Exception("You specified too many devices, max is %d" % raise Exception("You specified too many hostnames, max is %d" %
MAX_DEVICES) MAX_DEVICES)
return hostnames return hostnames
@ -393,10 +447,10 @@ def create_single_device(module, packet_conn, hostname):
plan = module.params.get('plan') plan = module.params.get('plan')
user_data = module.params.get('user_data') user_data = module.params.get('user_data')
facility = module.params.get('facility') facility = module.params.get('facility')
locked = module.params.get('lock')
operating_system = module.params.get('operating_system') operating_system = module.params.get('operating_system')
locked = module.params.get('locked') locked = module.params.get('locked')
ipxe_script_url = module.params.get('ipxe_script_url')
always_pxe = module.params.get('always_pxe')
device = packet_conn.create_device( device = packet_conn.create_device(
project_id=project_id, project_id=project_id,
hostname=hostname, hostname=hostname,
@ -408,35 +462,53 @@ def create_single_device(module, packet_conn, hostname):
return device return device
def wait_for_ips(module, packet_conn, created_devices): def refresh_device_list(module, packet_conn, devices):
device_ids = [d.id for d in devices]
def has_public_ip(addr_list):
return any([a['public'] and (len(a['address']) > 0) for a in addr_list])
def all_have_public_ip(ds):
return all([has_public_ip(d.ip_addresses) for d in ds])
def refresh_created_devices(ids_of_created_devices, module, packet_conn):
new_device_list = get_existing_devices(module, packet_conn) new_device_list = get_existing_devices(module, packet_conn)
return [d for d in new_device_list if d.id in ids_of_created_devices] return [d for d in new_device_list if d.id in device_ids]
def wait_for_devices_active(module, packet_conn, watched_devices):
wait_timeout = module.params.get('wait_timeout')
wait_timeout = time.time() + wait_timeout
refreshed = watched_devices
while wait_timeout > time.time():
refreshed = refresh_device_list(module, packet_conn, watched_devices)
if all(d.state == 'active' for d in refreshed):
return refreshed
time.sleep(5)
raise Exception("Waiting for state \"active\" timed out for devices: %s"
% [d.hostname for d in refreshed if d.state != "active"])
def wait_for_public_IPv(module, packet_conn, created_devices):
def has_public_ip(addr_list, ip_v):
return any([a['public'] and a['address_family'] == ip_v and
a['address'] for a in addr_list])
def all_have_public_ip(ds, ip_v):
return all([has_public_ip(d.ip_addresses, ip_v) for d in ds])
address_family = module.params.get('wait_for_public_IPv')
created_ids = [d.id for d in created_devices]
wait_timeout = module.params.get('wait_timeout') wait_timeout = module.params.get('wait_timeout')
wait_timeout = time.time() + wait_timeout wait_timeout = time.time() + wait_timeout
while wait_timeout > time.time(): while wait_timeout > time.time():
refreshed = refresh_created_devices(created_ids, module, refreshed = refresh_device_list(module, packet_conn, created_devices)
packet_conn) if all_have_public_ip(refreshed, address_family):
if all_have_public_ip(refreshed):
return refreshed return refreshed
time.sleep(5) time.sleep(5)
raise Exception("Waiting for IP assignment timed out. Hostnames: %s" raise Exception("Waiting for IPv%d address timed out. Hostnames: %s"
% [d.hostname for d in created_devices]) % (address_family, [d.hostname for d in created_devices]))
def get_existing_devices(module, packet_conn): def get_existing_devices(module, packet_conn):
project_id = module.params.get('project_id') project_id = module.params.get('project_id')
return packet_conn.list_devices(project_id, params={'per_page': MAX_DEVICES}) return packet_conn.list_devices(
project_id, params={
'per_page': MAX_DEVICES})
def get_specified_device_identifiers(module): def get_specified_device_identifiers(module):
@ -448,7 +520,7 @@ def get_specified_device_identifiers(module):
return {'hostnames': hostname_list, 'ids': []} return {'hostnames': hostname_list, 'ids': []}
def act_on_devices(target_state, module, packet_conn): def act_on_devices(module, packet_conn, target_state):
specified_identifiers = get_specified_device_identifiers(module) specified_identifiers = get_specified_device_identifiers(module)
existing_devices = get_existing_devices(module, packet_conn) existing_devices = get_existing_devices(module, packet_conn)
changed = False changed = False
@ -470,27 +542,30 @@ def act_on_devices(target_state, module, packet_conn):
state_map = { state_map = {
'absent': _absent_state_map, 'absent': _absent_state_map,
'active': {'inactive': packet.Device.power_on}, 'active': {'inactive': packet.Device.power_on,
'provisioning': None, 'rebooting': None
},
'inactive': {'active': packet.Device.power_off}, 'inactive': {'active': packet.Device.power_off},
'rebooted': {'active': packet.Device.reboot, 'rebooted': {'active': packet.Device.reboot,
'inactive': packet.Device.power_on}, 'inactive': packet.Device.power_on,
'provisioning': None, 'rebooting': None
},
} }
# First do non-creation actions, it might be faster # First do non-creation actions, it might be faster
for d in process_devices: for d in process_devices:
if d.state == target_state:
continue
if d.state in state_map[target_state]: if d.state in state_map[target_state]:
api_operation = state_map[target_state].get(d.state) api_operation = state_map[target_state].get(d.state)
try: if api_operation is not None:
api_operation(d) api_operation(d)
changed = True changed = True
except Exception as e:
_msg = ("while trying to make device %s, id %s %s, from state %s, "
"with api call by %s got error: %s" %
(d.hostname, d.id, target_state, d.state, api_operation, e))
raise Exception(_msg)
else: else:
_msg = ("I don't know how to process existing device %s from state %s " _msg = (
"to state %s" % (d.hostname, d.state, target_state)) "I don't know how to process existing device %s from state %s "
"to state %s" %
(d.hostname, d.state, target_state))
raise Exception(_msg) raise Exception(_msg)
# At last create missing devices # At last create missing devices
@ -498,11 +573,15 @@ def act_on_devices(target_state, module, packet_conn):
if create_hostnames: if create_hostnames:
created_devices = [create_single_device(module, packet_conn, n) created_devices = [create_single_device(module, packet_conn, n)
for n in create_hostnames] for n in create_hostnames]
if module.params.get('wait'): if module.params.get('wait_for_public_IPv'):
created_devices = wait_for_ips(module, packet_conn, created_devices) created_devices = wait_for_public_IPv(
module, packet_conn, created_devices)
changed = True changed = True
processed_devices = created_devices + process_devices processed_devices = created_devices + process_devices
if target_state == 'active':
processed_devices = wait_for_devices_active(
module, packet_conn, processed_devices)
return { return {
'changed': changed, 'changed': changed,
@ -518,21 +597,24 @@ def main():
count=dict(type='int', default=1), count=dict(type='int', default=1),
count_offset=dict(type='int', default=1), count_offset=dict(type='int', default=1),
device_ids=dict(type='list'), device_ids=dict(type='list'),
facility=dict(default='ewr1'), facility=dict(),
features=dict(type='dict'), features=dict(type='dict'),
hostnames=dict(type='list', aliases=['name']), hostnames=dict(type='list', aliases=['name']),
locked=dict(type='bool', default=False), locked=dict(type='bool', default=False, aliases=['lock']),
operating_system=dict(), operating_system=dict(),
plan=dict(), plan=dict(),
project_id=dict(required=True), project_id=dict(required=True),
state=dict(choices=ALLOWED_STATES, default='present'), state=dict(choices=ALLOWED_STATES, default='present'),
user_data=dict(default=None), user_data=dict(default=None),
wait=dict(type='bool', default=False), wait_for_public_IPv=dict(type='int', choices=[4, 6]),
wait_timeout=dict(type='int', default=60), wait_timeout=dict(type='int', default=900),
ipxe_script_url=dict(default=''),
always_pxe=dict(type='bool', default=False),
), ),
required_one_of=[('device_ids', 'hostnames',)], required_one_of=[('device_ids', 'hostnames',)],
mutually_exclusive=[ mutually_exclusive=[
('always_pxe', 'operating_system'),
('ipxe_script_url', 'operating_system'),
('hostnames', 'device_ids'), ('hostnames', 'device_ids'),
('count', 'device_ids'), ('count', 'device_ids'),
('count_offset', 'device_ids'), ('count_offset', 'device_ids'),
@ -555,10 +637,10 @@ def main():
state = module.params.get('state') state = module.params.get('state')
try: try:
module.exit_json(**act_on_devices(state, module, packet_conn)) module.exit_json(**act_on_devices(module, packet_conn, state))
except Exception as e: except Exception as e:
module.fail_json(msg='failed to set machine state %s, error: %s' % (state,str(e))) module.fail_json(msg='failed to set device state %s, error: %s' %
(state, to_native(e)), exception=traceback.format_exc())
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View file

@ -146,7 +146,6 @@ lib/ansible/modules/cloud/ovirt/ovirt_affinity_group.py
lib/ansible/modules/cloud/ovirt/ovirt_nics.py lib/ansible/modules/cloud/ovirt/ovirt_nics.py
lib/ansible/modules/cloud/ovirt/ovirt_permissions.py lib/ansible/modules/cloud/ovirt/ovirt_permissions.py
lib/ansible/modules/cloud/ovirt/ovirt_vms.py lib/ansible/modules/cloud/ovirt/ovirt_vms.py
lib/ansible/modules/cloud/packet/packet_device.py
lib/ansible/modules/cloud/packet/packet_sshkey.py lib/ansible/modules/cloud/packet/packet_sshkey.py
lib/ansible/modules/cloud/profitbricks/profitbricks.py lib/ansible/modules/cloud/profitbricks/profitbricks.py
lib/ansible/modules/cloud/profitbricks/profitbricks_datacenter.py lib/ansible/modules/cloud/profitbricks/profitbricks_datacenter.py