# Buggy code
def map_obj_to_ele(module, want):
    element = Element("system")
    login = SubElement(element, "login")

    for item in want:
        if item["state"] != "present":
            if item["name"] == "root":
                module.fail_json(msg="cannot delete the 'root' account.")
            operation = "delete"
        else:
            operation = "merge"

        user = SubElement(login, "user", {"operation": operation})

        SubElement(user, "name").text = item["name"]

        if operation == "merge":
            if item["active"]:
                user.set("active", "active")
            else:
                user.set("inactive", "inactive")

            if item["role"]:
                SubElement(user, "class").text = item["role"]

            if item.get("full_name"):
                SubElement(user, "full-name").text = item["full_name"]

            if item.get("sshkey"):
                auth = SubElement(user, "authentication")
                if "ssh-rsa" in item["sshkey"]:
                    ssh_rsa = SubElement(auth, "ssh-rsa")
                elif "ssh-dss" in item["sshkey"]:
                    ssh_rsa = SubElement(auth, "ssh-dsa")
                elif "ecdsa-sha2" in item["sshkey"]:
                    ssh_rsa = SubElement(auth, "ssh-ecdsa")
                elif "ssh-ed25519" in item["sshkey"]:
                    ssh_rsa = SubElement(auth, "ssh-ed25519")
                key = SubElement(ssh_rsa, "name").text = item["sshkey"]

            if item.get("encrypted_password"):
                if "auth" not in locals():
                    auth = SubElement(user, "authentication")
                SubElement(auth, "encrypted-password").text = item["encrypted_password"]

    return element


# Traceback (ansible.module_utils.connection.ConnectionError)
The full traceback is:
Traceback (most recent call last):
File "/home/user/.ansible/tmp/ansible-local-267743wr75f/ansible-tmp-1548947971.62-20189547841248/AnsiballZ_junos_user.py", line 113, in <module>
_ansiballz_main()
File "/home/user/.ansible/tmp/ansible-local-267743wr75f/ansible-tmp-1548947971.62-20189547841248/AnsiballZ_junos_user.py", line 105, in _ansiballz_main
invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
File "/home/user/.ansible/tmp/ansible-local-267743wr75f/ansible-tmp-1548947971.62-20189547841248/AnsiballZ_junos_user.py", line 48, in invoke_module
imp.load_module('__main__', mod, module, MOD_DESC)
File "/tmp/ansible_junos_user_payload_yz6HY3/__main__.py", line 367, in <module>
File "/tmp/ansible_junos_user_payload_yz6HY3/__main__.py", line 350, in main
File "/tmp/ansible_junos_user_payload_yz6HY3/ansible_junos_user_payload.zip/ansible/module_utils/network/junos/junos.py", line 251, in load_config
File "/tmp/ansible_junos_user_payload_yz6HY3/ansible_junos_user_payload.zip/ansible/module_utils/network/common/netconf.py", line 76, in __rpc__
File "/tmp/ansible_junos_user_payload_yz6HY3/ansible_junos_user_payload.zip/ansible/module_utils/network/common/netconf.py", line 108, in parse_rpc_error
ansible.module_utils.connection.ConnectionError: error: mgd: Missing mandatory statement: 'class'
error: configuration check-out failed: daemon file propagation failed

fatal: [switch]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n  File \"/home/user/.ansible/tmp/ansible-local-267743wr75f/ansible-tmp-1548947971.62-20189547841248/AnsiballZ_
"module_stdout": "",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 1
}

# Fixed code
def map_obj_to_ele(module, want):
    element = Element("system")
    login = SubElement(element, "login")

    for item in want:
        if item["state"] != "present":
            if item["name"] == "root":
                module.fail_json(msg="cannot delete the 'root' account.")
            operation = "delete"
        else:
            operation = "merge"

        if item["name"] != "root":
            user = SubElement(login, "user", {"operation": operation})
            SubElement(user, "name").text = item["name"]
        else:
            user = auth = SubElement(
                element, "root-authentication", {"operation": operation}
            )

        if operation == "merge":
            if item["name"] == "root" and (
                not item["active"] or item["role"] or item["full_name"]
            ):
                module.fail_json(
                    msg="'root' account cannot be deactivated or be assigned a role and a full name"
                )

            if item["active"]:
                user.set("active", "active")
            else:
                user.set("inactive", "inactive")

            if item["role"]:
                SubElement(user, "class").text = item["role"]

            if item.get("full_name"):
                SubElement(user, "full-name").text = item["full_name"]

            if item.get("sshkey"):
                if "auth" not in locals():
                    auth = SubElement(user, "authentication")
                if "ssh-rsa" in item["sshkey"]:
                    ssh_rsa = SubElement(auth, "ssh-rsa")
                elif "ssh-dss" in item["sshkey"]:
                    ssh_rsa = SubElement(auth, "ssh-dsa")
                elif "ecdsa-sha2" in item["sshkey"]:
                    ssh_rsa = SubElement(auth, "ssh-ecdsa")
                elif "ssh-ed25519" in item["sshkey"]:
                    ssh_rsa = SubElement(auth, "ssh-ed25519")
                key = SubElement(ssh_rsa, "name").text = item["sshkey"]

            if item.get("encrypted_password"):
                if "auth" not in locals():
                    auth = SubElement(user, "authentication")
                SubElement(auth, "encrypted-password").text = item["encrypted_password"]

    return element