# Buggy code
def create_single_device(module, packet_conn, hostname):
    for param in ("hostnames", "operating_system", "plan"):
        if not module.params.get(param):
            raise Exception("%s parameter is required for new device." % param)
    project_id = module.params.get("project_id")
    plan = module.params.get("plan")
    user_data = module.params.get("user_data")
    facility = module.params.get("facility")
    operating_system = module.params.get("operating_system")
    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(
        project_id=project_id,
        hostname=hostname,
        plan=plan,
        facility=facility,
        operating_system=operating_system,
        userdata=user_data,
        locked=locked,
    )
    return device


# Traceback (Exception)
task path: /home/krist/Work/LAB/pakket/create_rhvh_lab.yaml:9
Using module file /usr/lib/python2.7/site-packages/ansible/modules/cloud/packet/packet_device.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: krist
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_qyWyXe/ansible_module_packet_device.py", line 640, in main
module.exit_json(**act_on_devices(module, packet_conn, state))
File "/tmp/ansible_qyWyXe/ansible_module_packet_device.py", line 575, in act_on_devices
for n in create_hostnames]
File "/tmp/ansible_qyWyXe/ansible_module_packet_device.py", line 445, in create_single_device
% param)
Exception: operating_system parameter is required for new device.

failed: [localhost] (item=rhvh1) => {
"changed": false,
"failed": true,
"invocation": {
"module_args": {
"always_pxe": false,
"auth_token": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"count": 1,
"count_offset": 1,
"device_ids": null,
"facility": "ams1",
"features": null,
"hostnames": [
"rhvh1"
],
"ipxe_script_url": "http://home.kri.st/rhvh/boot.ipxe",
"locked": false,
"operating_system": null,
"plan": "baremetal_0",
"project_id": "be6b7156-3c89-447c-b46e-ee376809a3d2",
"state": "present",
"user_data": null,
"wait_for_public_IPv": null,
"wait_timeout": 900
}
},
"item": "rhvh1",
"msg": "failed to set device state present, error: operating_system parameter is required for new device."
}

# Fixed code
def create_single_device(module, packet_conn, hostname):
    for param in ("hostnames", "operating_system", "plan"):
        if not module.params.get(param):
            raise Exception("%s parameter is required for new device." % param)
    project_id = module.params.get("project_id")
    plan = module.params.get("plan")
    user_data = module.params.get("user_data")
    facility = module.params.get("facility")
    operating_system = module.params.get("operating_system")
    locked = module.params.get("locked")
    ipxe_script_url = module.params.get("ipxe_script_url")
    always_pxe = module.params.get("always_pxe")
    if operating_system != "custom_ipxe":
        for param in ("ipxe_script_url", "always_pxe"):
            if module.params.get(param):
                raise Exception(
                    "%s paramater is not valid for non custom_ipxe operating_system."
                    % param
                )

    device = packet_conn.create_device(
        project_id=project_id,
        hostname=hostname,
        plan=plan,
        facility=facility,
        operating_system=operating_system,
        userdata=user_data,
        locked=locked,
    )
    return device