Repository: ansible/ansible

Issue:
list index out of range during fact gather
<!--- Verify first that your issue/request is not already reported in GitHub -->
##### ISSUE TYPE

<!--- Pick one below and delete the rest: -->
- Bug Report
##### ANSIBLE VERSION

<!--- Paste verbatim output from “ansible --version” between quotes below -->

```
ansible 2.1.0.0
  config file =
  configured module search path = Default w/o overrides
```
##### CONFIGURATION

<!---
[defaults]
log_path=ansible.log
display_skipped_hosts=False
callback_plugins = ./callback_plugins
-->
##### OS / ENVIRONMENT

<!---
Fedora release 23 (Twenty Three)
-->
##### SUMMARY

<!--- Explain the problem briefly -->

On Ansible 2.1, when gathering facts, for a chroot host the facts gather fails with a list index out of range exception.

Issue does not occur on previous 2.0.x versions.
##### STEPS TO REPRODUCE

<!---
Occurs when using ansible_connection: chroot
-->

<!--- Paste example playbooks or commands between quotes below -->

```

```

<!--- You can also paste gist.github.com links for larger files -->
##### EXPECTED RESULTS

<!--- What did you expect to happen when running the steps above? -->

Should gather facts correctly.
##### ACTUAL RESULTS

<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

exception

<!--- Paste verbatim command output between quotes below -->

```
[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 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1465321346.92-86268247124152 `" && echo ansible-tmp-1465321346.92-86268247124152="` echo $HOME/.ansible/tmp/ansible-tmp-1465321346.92-86268247124152 `" ) && 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>&1 && 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}
```



Discussion:

Resolving patch:
diff --git a/lib/ansible/module_utils/facts.py b/lib/ansible/module_utils/facts.py
--- a/lib/ansible/module_utils/facts.py
+++ b/lib/ansible/module_utils/facts.py
@@ -1211,6 +1211,8 @@ def get_mount_facts(self):
         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])


Test patch: