# Buggy code
def get_mount_facts(self):
    uuids = dict()
    self.facts["mounts"] = []
    bind_mounts = []
    findmntPath = self.module.get_bin_path("findmnt")
    if findmntPath:
        rc, out, err = self.module.run_command(
            "%s -lnur" % (findmntPath), use_unsafe_shell=True
        )
        if rc == 0:
            # find bind mounts, in case /etc/mtab is a symlink to /proc/mounts
            for line in out.split("\n"):
                fields = line.rstrip("\n").split()
                if len(fields) < 2:
                    continue
                if re.match(".*\]", fields[1]):
                    bind_mounts.append(fields[0])

    mtab = get_file_content("/etc/mtab", "")
    for line in mtab.split("\n"):
        fields = line.rstrip("\n").split()
        if fields[0].startswith("/") or ":/" in fields[0]:
            if fields[2] != "none":
                size_total, size_available = self._get_mount_size_facts(fields[1])
                if fields[0] in uuids:
                    uuid = uuids[fields[0]]
                else:
                    uuid = "NA"
                    lsblkPath = self.module.get_bin_path("lsblk")
                    if lsblkPath:
                        rc, out, err = self.module.run_command(
                            "%s -ln --output UUID %s" % (lsblkPath, fields[0]),
                            use_unsafe_shell=True,
                        )

                        if rc == 0:
                            uuid = out.strip()
                            uuids[fields[0]] = uuid

                if fields[1] in bind_mounts:
                    # only add if not already there, we might have a plain /etc/mtab
                    if not re.match(".*bind.*", fields[3]):
                        fields[3] += ",bind"

                self.facts["mounts"].append(
                    {
                        "mount": fields[1],
                        "device": fields[0],
                        "fstype": fields[2],
                        "options": fields[3],
                        # statvfs data
                        "size_total": size_total,
                        "size_available": size_available,
                        "uuid": uuid,
                    }
                )


# Traceback (IndexError)
[root@box ansible]# sudo -E ansible-playbook -vvvv -i hosts_generator.py wisp-util.yml
Using /mnt/datadisk/jenkins/workspace/squashible-util/ansible.cfg as config file
Loaded callback default of type stdout, v2.0

PLAYBOOK: wisp-util.yml ********************************************************
1 plays in wisp-util.yml

PLAY [chroot] ******************************************************************

TASK [setup] *******************************************************************
<chroot> THIS IS A LOCAL CHROOT DIR
<chroot> EXEC ['/sbin/chroot', u'chroot', '/bin/sh', '-c', u'/bin/sh -c \'( umask 77 &amp;&amp; mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1465321346.92-86268247124152 `" &amp;&amp; echo ansible-tmp-1465321346.92-86268247124152="` echo $HOME/.ansible/tmp/ansible-tmp-1465321346.92-86268247124152 `" ) &amp;&amp; sleep 0\'']
<chroot> PUT /tmp/tmpftiGkl TO /root/.ansible/tmp/ansible-tmp-1465321346.92-86268247124152/setup
<chroot> EXEC ['/sbin/chroot', u'chroot', '/bin/sh', '-c', u'dd of=/root/.ansible/tmp/ansible-tmp-1465321346.92-86268247124152/setup bs=65536']
<chroot> EXEC ['/sbin/chroot', u'chroot', '/bin/sh', '-c', u'/bin/sh -c \'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1465321346.92-86268247124152/setup; rm -rf "/root/.ansible/tmp/ansible-tmp-1465321346.92-86268247124152/" > /dev/null 2>&amp;1 &amp;&amp; sleep 0\'']
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_Y_rKjr/ansible_module_setup.py", line 127, in <module>
main()
File "/tmp/ansible_Y_rKjr/ansible_module_setup.py", line 119, in main
data = get_all_facts(module)
File "/tmp/ansible_Y_rKjr/ansible_modlib.zip/ansible/module_utils/facts.py", line 3177, in get_all_facts
File "/tmp/ansible_Y_rKjr/ansible_modlib.zip/ansible/module_utils/facts.py", line 3123, in ansible_facts
File "/tmp/ansible_Y_rKjr/ansible_modlib.zip/ansible/module_utils/facts.py", line 919, in populate
File "/tmp/ansible_Y_rKjr/ansible_modlib.zip/ansible/module_utils/facts.py", line 107, in wrapper
File "/tmp/ansible_Y_rKjr/ansible_modlib.zip/ansible/module_utils/facts.py", line 1142, in get_mount_facts
IndexError: list index out of range

fatal: [chroot]: FAILED! => {"changed": false, "failed": true, "invocation": {"module_name": "setup"}, "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_Y_rKjr/ansible_module_setup.py\", line 127, in <module>\n    main()\n  File \"/tmp/ansible_Y_rKjr/ansible_module_setup.py\", line 119, in main\n    data = get_all_facts(module)\n  File \"/tmp/ansible_Y_rKjr/ansible_modlib.zip/ansible/module_utils/facts.py\", line 3177, in get_all_facts\n  File \"/tmp/ansible_Y_rKjr/ansible_modlib.zip/ansible/module_utils/facts.py\", line 3123, in ansible_facts\n  File \"/tmp/ansible_Y_rKjr/ansible_modlib.zip/ansible/module_utils/facts.py\", line 919, in populate\n  File \"/tmp/ansible_Y_rKjr/ansible_modlib.zip/ansible/module_utils/facts.py\", line 107, in wrapper\n  File \"/tmp/ansible_Y_rKjr/ansible_modlib.zip/ansible/module_utils/facts.py\", line 1142, in get_mount_facts\nIndexError: list index out of range\n", "module_stdout": "", "msg": "MODULE FAILURE", "parsed": false}

# Fixed code
def get_mount_facts(self):
    uuids = dict()
    self.facts["mounts"] = []
    bind_mounts = []
    findmntPath = self.module.get_bin_path("findmnt")
    if findmntPath:
        rc, out, err = self.module.run_command(
            "%s -lnur" % (findmntPath), use_unsafe_shell=True
        )
        if rc == 0:
            # find bind mounts, in case /etc/mtab is a symlink to /proc/mounts
            for line in out.split("\n"):
                fields = line.rstrip("\n").split()
                if len(fields) < 2:
                    continue
                if re.match(".*\]", fields[1]):
                    bind_mounts.append(fields[0])

    mtab = get_file_content("/etc/mtab", "")
    for line in mtab.split("\n"):
        fields = line.rstrip("\n").split()
        if len(fields) < 4:
            continue
        if fields[0].startswith("/") or ":/" in fields[0]:
            if fields[2] != "none":
                size_total, size_available = self._get_mount_size_facts(fields[1])
                if fields[0] in uuids:
                    uuid = uuids[fields[0]]
                else:
                    uuid = "NA"
                    lsblkPath = self.module.get_bin_path("lsblk")
                    if lsblkPath:
                        rc, out, err = self.module.run_command(
                            "%s -ln --output UUID %s" % (lsblkPath, fields[0]),
                            use_unsafe_shell=True,
                        )

                        if rc == 0:
                            uuid = out.strip()
                            uuids[fields[0]] = uuid

                if fields[1] in bind_mounts:
                    # only add if not already there, we might have a plain /etc/mtab
                    if not re.match(".*bind.*", fields[3]):
                        fields[3] += ",bind"

                self.facts["mounts"].append(
                    {
                        "mount": fields[1],
                        "device": fields[0],
                        "fstype": fields[2],
                        "options": fields[3],
                        # statvfs data
                        "size_total": size_total,
                        "size_available": size_available,
                        "uuid": uuid,
                    }
                )