# Buggy code
def find_uri_for_artifact(self, artifact):
    if artifact.version == "latest":
        artifact.version = self.find_latest_version_available(artifact)

    if artifact.is_snapshot():
        if self.local:
            return self._uri_for_artifact(artifact, artifact.version)
        path = "/%s/%s" % (artifact.path(), self.metadata_file_name)
        content = self._getContent(
            self.base + path, "Failed to retrieve the maven metadata file: " + path
        )
        xml = etree.fromstring(content)
        timestamp = xml.xpath("/metadata/versioning/snapshot/timestamp/text()")[0]
        buildNumber = xml.xpath("/metadata/versioning/snapshot/buildNumber/text()")[0]
        for snapshotArtifact in xml.xpath(
            "/metadata/versioning/snapshotVersions/snapshotVersion"
        ):
            classifier = snapshotArtifact.xpath("classifier/text()")
            artifact_classifier = classifier[0] if classifier else ""
            extension = snapshotArtifact.xpath("extension/text()")
            artifact_extension = extension[0] if extension else ""
            if (
                artifact_classifier == artifact.classifier
                and artifact_extension == artifact.extension
            ):
                return self._uri_for_artifact(
                    artifact, snapshotArtifact.xpath("value/text()")[0]
                )
        return self._uri_for_artifact(
            artifact,
            artifact.version.replace("SNAPSHOT", timestamp + "-" + buildNumber),
        )

    return self._uri_for_artifact(artifact, artifact.version)


# Traceback (nIndexError)
TASK [install-modules : get module installation files from artifactory] ***
--
task path: /opt/rundeck-data/d0614942-24f2-4453-aee9-b66a9e7f9914/18233/roles/install-modules/tasks/install-module.yml:12
Using module file /opt/ansible/lib/ansible/modules/packaging/language/maven_artifact.py
<my-server.my-serverfarm.net> ESTABLISH SSH CONNECTION FOR USER: ec2-user
<my-server.my-serverfarm.net> SSH: EXEC ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o 'IdentityFile="/var/lib/rundeck/.ssh/ec2_user_id_rsa"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ec2-user -o ConnectTimeout=10 -o ControlPath=/var/lib/rundeck/.ansible/cp/5342204d28 my-server.my-serverfarm.net '/bin/sh -c '"'"'sudo -H -S -n -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-sypixboiktxznzxvgvjfmmezrqkzjsxy; /usr/bin/python'"'"'"'"'"'"'"'"' &amp;&amp; sleep 0'"'"''
Escalation succeeded
<my-server.my-serverfarm.net> (1, '', 'OpenSSH_7.4p1, OpenSSL 1.0.2k-fips  26 Jan 2017\r\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\ndebug1: /etc/ssh/ssh_config line 58: Applying options for *\r\ndebug1: auto-mux: Trying existing master\r\ndebug2: fd 3 setting O_NONBLOCK\r\ndebug2: mux_client_hello_exchange: master version 4\r\ndebug3: mux_client_forwards: request forwardings: 0 local, 0 remote\r\ndebug3: mux_client_request_session: entering\r\ndebug3: mux_client_request_alive: entering\r\ndebug3: mux_client_request_alive: done pid = 6689\r\ndebug3: mux_client_request_session: session request sent\r\ndebug1: mux_client_request_session: master session id: 2\r\nTraceback (most recent call last):\n  File "/tmp/ansible_5JSeNG/ansible_module_maven_artifact.py", line 507, in <module>\n    main()\n  File "/tmp/ansible_5JSeNG/ansible_module_maven_artifact.py", line 489, in main\n    if downloader.download(artifact, b_dest):\n  File "/tmp/ansible_5JSeNG/ansible_module_maven_artifact.py", line 347, in download\n    url = self.find_uri_for_artifact(artifact)\n  File "/tmp/ansible_5JSeNG/ansible_module_maven_artifact.py", line 295, in find_uri_for_artifact\n    timestamp = xml.xpath("/metadata/versioning/snapshot/timestamp/text()")[0]\nIndexError: list index out of range\ndebug3: mux_client_read_packet: read header failed: Broken pipe\r\ndebug2: Received exit status from master 1\r\n')
fatal: [my-server.my-serverfarm.net]: FAILED! => {
"changed": false,
"module_stderr": "OpenSSH_7.4p1, OpenSSL 1.0.2k-fips  26 Jan 2017\r\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\ndebug1: /etc/ssh/ssh_config line 58: Applying options for *\r\ndebug1: auto-mux: Trying existing master\r\ndebug2: fd 3 setting O_NONBLOCK\r\ndebug2: mux_client_hello_exchange: master version 4\r\ndebug3: mux_client_forwards: request forwardings: 0 local, 0 remote\r\ndebug3: mux_client_request_session: entering\r\ndebug3: mux_client_request_alive: entering\r\ndebug3: mux_client_request_alive: done pid = 6689\r\ndebug3: mux_client_request_session: session request sent\r\ndebug1: mux_client_request_session: master session id: 2\r\nTraceback (most recent call last):\n  File \"/tmp/ansible_5JSeNG/ansible_module_maven_artifact.py\", line 507, in <module>\n    main()\n  File \"/tmp/ansible_5JSeNG/ansible_module_maven_artifact.py\", line 489, in main\n    if downloader.download(artifact, b_dest):\n  File \"/tmp/ansible_5JSeNG/ansible_module_maven_artifact.py\", line 347, in download\n    url = self.find_uri_for_artifact(artifact)\n  File \"/tmp/ansible_5JSeNG/ansible_module_maven_artifact.py\", line 295, in find_uri_for_artifact\n    timestamp = xml.xpath(\"/metadata/versioning/snapshot/timestamp/text()\")[0]\nIndexError: list index out of range\ndebug3: mux_client_read_packet: read header failed: Broken pipe\r\ndebug2: Received exit status from master 1\r\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}

# Fixed code
def find_uri_for_artifact(self, artifact):
    if artifact.version == "latest":
        artifact.version = self.find_latest_version_available(artifact)

    if artifact.is_snapshot():
        if self.local:
            return self._uri_for_artifact(artifact, artifact.version)
        path = "/%s/%s" % (artifact.path(), self.metadata_file_name)
        content = self._getContent(
            self.base + path, "Failed to retrieve the maven metadata file: " + path
        )
        xml = etree.fromstring(content)

        for snapshotArtifact in xml.xpath(
            "/metadata/versioning/snapshotVersions/snapshotVersion"
        ):
            classifier = snapshotArtifact.xpath("classifier/text()")
            artifact_classifier = classifier[0] if classifier else ""
            extension = snapshotArtifact.xpath("extension/text()")
            artifact_extension = extension[0] if extension else ""
            if (
                artifact_classifier == artifact.classifier
                and artifact_extension == artifact.extension
            ):
                return self._uri_for_artifact(
                    artifact, snapshotArtifact.xpath("value/text()")[0]
                )
        timestamp_xmlpath = xml.xpath("/metadata/versioning/snapshot/timestamp/text()")
        if timestamp_xmlpath:
            timestamp = timestamp_xmlpath[0]
            build_number = xml.xpath(
                "/metadata/versioning/snapshot/buildNumber/text()"
            )[0]
            return self._uri_for_artifact(
                artifact,
                artifact.version.replace("SNAPSHOT", timestamp + "-" + build_number),
            )

    return self._uri_for_artifact(artifact, artifact.version)