Repository: ansible/ansible

Issue:
ansible-playbook fails to run
##### Issue Type:

Bug Report
##### Ansible Version:

± ansible --version
ansible 1.6 (brew dddb48e67d) last updated 2014/04/21 13:24:10 (GMT -400)
##### Environment:

OS X 
##### Summary:

ansible-playbook returns error while gathering facts
##### Steps To Reproduce:
1. Create the following playbook:

``` yaml
# file: debug.yml

---
- hosts: localhost
  roles:
    - debug
```

And the following `roles/debug/defaults/main.yml` file:

``` yaml
# file: roles/debug/defaults/main.yml

---
test:
  one: "This is the first variable."
```

And the following `roles/debug/vars/main.yml` file:

``` yaml
# file: roles/debug/vars/main.yml

---
test:
  two: "This is the second variable."
  three: "This is the third variable."
```

And the following `roles/debug/tasks/main.yml`:

``` yaml
# file: roles/debug/tasks/main.yml

---
- name: main | debug
  debug: "msg='{{ test.one }} and {{ test.two }} and {{ test.three }}'"
```

And the following inventory file:

``` yaml
localhost
```
##### Expected Results:

The `debug` role completes without error. 
##### Actual Results:

```
± ansible-playbook -i ~/.ansible.d/inventories/test debug.yml

PLAY [localhost] **************************************************************

GATHERING FACTS ***************************************************************
fatal: [localhost] => failed to parse: Traceback (most recent call last):
  File "/Users/dan/.ansible/tmp/ansible-tmp-1398102984.19-34210843891217/setup", line 3687, in <module>
    main()
  File "/Users/dan/.ansible/tmp/ansible-tmp-1398102984.19-34210843891217/setup", line 134, in main
    data = run_setup(module)
  File "/Users/dan/.ansible/tmp/ansible-tmp-1398102984.19-34210843891217/setup", line 78, in run_setup
    facts = ansible_facts(module)
  File "/Users/dan/.ansible/tmp/ansible-tmp-1398102984.19-34210843891217/setup", line 3627, in ansible_facts
    facts.update(Network(module).populate())
  File "/Users/dan/.ansible/tmp/ansible-tmp-1398102984.19-34210843891217/setup", line 2996, in populate
    interfaces, ips = self.get_interfaces_info(ifconfig_path)
  File "/Users/dan/.ansible/tmp/ansible-tmp-1398102984.19-34210843891217/setup", line 3064, in get_interfaces_info
    current_if = self.parse_interface_line(words)
  File "/Users/dan/.ansible/tmp/ansible-tmp-1398102984.19-34210843891217/setup", line 3092, in parse_interface_line
    current_if['mtu'] = words[5]
IndexError: list index out of range
```



Discussion:
Hrm, looks like it may be related to this commit, which I just merged today: e5f297697abb3a9f5e13ce939542f900c1c2c257. Are you managing a *BSD box?

I'm running OS X, so I guess that's technically a kind of BSD.

@bcoca

One easy way to fix this would be to just double check the length of the `words` list.

yep, sorry, tested against latest stable FreeBSDs 8, 9 and 10, didn't think
older versions would break (DUH!), will send PR tonight.​


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
@@ -1751,9 +1751,14 @@ def parse_interface_line(self, words):
         device = words[0][0:-1]
         current_if = {'device': device, 'ipv4': [], 'ipv6': [], 'type': 'unknown'}
         current_if['flags']  = self.get_options(words[1])
-        current_if['metric'] = words[3]
-        current_if['mtu'] = words[5]
         current_if['macaddress'] = 'unknown'    # will be overwritten later
+
+        if len(words) >= 5 : # Newer FreeBSD versions
+            current_if['metric'] = words[3]
+            current_if['mtu'] = words[5]
+        else:
+            current_if['mtu'] = words[3]
+
         return current_if
 
     def parse_options_line(self, words, current_if, ips):


Test patch: