# Buggy code
def main():
    module = AnsibleModule(
        argument_spec=dict(
            auth_token=dict(
                default=os.environ.get(PACKET_API_TOKEN_ENV_VAR), no_log=True
            ),
            count=dict(type="int", default=1),
            count_offset=dict(type="int", default=1),
            device_ids=dict(type="list"),
            facility=dict(),
            features=dict(type="dict"),
            hostnames=dict(type="list", aliases=["name"]),
            locked=dict(type="bool", default=False, aliases=["lock"]),
            operating_system=dict(),
            plan=dict(),
            project_id=dict(required=True),
            state=dict(choices=ALLOWED_STATES, default="present"),
            user_data=dict(default=None),
            wait_for_public_IPv=dict(type="int", choices=[4, 6]),
            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",
            )
        ],
        mutually_exclusive=[
            ("always_pxe", "operating_system"),
            ("ipxe_script_url", "operating_system"),
            ("hostnames", "device_ids"),
            ("count", "device_ids"),
            ("count_offset", "device_ids"),
        ],
    )

    if not HAS_PACKET_SDK:
        module.fail_json(msg="packet required for this module")

    if not module.params.get("auth_token"):
        _fail_msg = (
            "if Packet API token is not in environment variable %s, "
            "the auth_token parameter is required" % PACKET_API_TOKEN_ENV_VAR
        )
        module.fail_json(msg=_fail_msg)

    auth_token = module.params.get("auth_token")

    packet_conn = packet.Manager(auth_token=auth_token)

    state = module.params.get("state")

    try:
        module.exit_json(**act_on_devices(module, packet_conn, state))
    except Exception as e:
        module.fail_json(
            msg="failed to set device state %s, error: %s" % (state, to_native(e)),
            exception=traceback.format_exc(),
        )


# 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 main():
    module = AnsibleModule(
        argument_spec=dict(
            auth_token=dict(
                default=os.environ.get(PACKET_API_TOKEN_ENV_VAR), no_log=True
            ),
            count=dict(type="int", default=1),
            count_offset=dict(type="int", default=1),
            device_ids=dict(type="list"),
            facility=dict(),
            features=dict(type="dict"),
            hostnames=dict(type="list", aliases=["name"]),
            locked=dict(type="bool", default=False, aliases=["lock"]),
            operating_system=dict(),
            plan=dict(),
            project_id=dict(required=True),
            state=dict(choices=ALLOWED_STATES, default="present"),
            user_data=dict(default=None),
            wait_for_public_IPv=dict(type="int", choices=[4, 6]),
            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",
            )
        ],
        mutually_exclusive=[
            ("hostnames", "device_ids"),
            ("count", "device_ids"),
            ("count_offset", "device_ids"),
        ],
    )

    if not HAS_PACKET_SDK:
        module.fail_json(msg="packet required for this module")

    if not module.params.get("auth_token"):
        _fail_msg = (
            "if Packet API token is not in environment variable %s, "
            "the auth_token parameter is required" % PACKET_API_TOKEN_ENV_VAR
        )
        module.fail_json(msg=_fail_msg)

    auth_token = module.params.get("auth_token")

    packet_conn = packet.Manager(auth_token=auth_token)

    state = module.params.get("state")

    try:
        module.exit_json(**act_on_devices(module, packet_conn, state))
    except Exception as e:
        module.fail_json(
            msg="failed to set device state %s, error: %s" % (state, to_native(e)),
            exception=traceback.format_exc(),
        )