Repository: ansible/ansible

Issue:
nxos_linkagg fails to create linkagg when running on I2 image
<!---
Verify first that your issue/request is not already reported on GitHub.
THIS FORM WILL BE READ BY A MACHINE, COMPLETE ALL SECTIONS AS DESCRIBED.
Also test if the latest release, and devel branch are affected too.
ALWAYS add information AFTER (OUTSIDE) these html comments.
Otherwise it may end up being automatically closed by our bot. -->

##### SUMMARY
<!--- Explain the problem briefly -->

##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
<!--- Insert, BELOW THIS COMMENT, the name of the module, plugin, task or feature.
Do not include extra details here, e.g. "vyos_command" not "the network module vyos_command" or the full path-->
nxos_linkagg
##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
ansible 2.6.0.dev0 (stable-2.6 9ae2050e2f) last updated 2018/06/11 12:10:05 (GMT -400)
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /root/agents-ci/ansible/lib/ansible
  executable location = /root/agents-ci/ansible/bin/ansible
  python version = 2.7.6 (default, Oct 26 2016, 20:30:19) [GCC 4.8.4]
```

##### OS / ENVIRONMENT
<!--- Mention, BELOW THIS COMMENT, the OS you are running Ansible from, and the OS you are
managing, or say "N/A" for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.-->
Ansible Server : Ubuntu 14.04
Device: N9K running 7.0(3)I2(5)

##### STEPS TO REPRODUCE
<!--- For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used. -->

When we run the integration tests on N9k running I2 image, the create_linkagg test fails if there are no port-channels defined already on the device.


##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
TASK [nxos_linkagg : create linkagg] *****************************************************************************************************************************
task path: /root/agents-ci/ansible/test/integration/targets/nxos_linkagg/tests/common/sanity.yaml:39                                                              
<n9k.example.com> ESTABLISH LOCAL CONNECTION FOR USER: root                                                                                                 
<n9k.example.com> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-local-29626w5zfjt/ansible-tmp-1528990630.34-160148825984664 `" && echo ansible-tmp-1528990630.34-160148825984664="` echo /root/.ansible/tmp/ansible-local-29626w5zfjt/ansible-tmp-1528990630.34-160148825984664 `" ) && sleep 0' 
Using module file /root/agents-ci/ansible/lib/ansible/modules/network/nxos/nxos_linkagg.py                                                                        
<n9k.example.com> PUT /root/.ansible/tmp/ansible-local-29626w5zfjt/tmpKqKLUJ TO /root/.ansible/tmp/ansible-local-29626w5zfjt/ansible-tmp-1528990630.34-160148825984664/nxos_linkagg.py                                                                                                                                        
<n9k.example.com> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-local-29626w5zfjt/ansible-tmp-1528990630.34-160148825984664/ /root/.ansible/tmp/ansible-local-29626w5zfjt/ansible-tmp-1528990630.34-160148825984664/nxos_linkagg.py && sleep 0'                                                                       
<n9k.example.com> EXEC /bin/sh -c '/root/p2/bin/python /root/.ansible/tmp/ansible-local-29626w5zfjt/ansible-tmp-1528990630.34-160148825984664/nxos_linkagg.py && sleep 0'                                                                                                                                                     
<n9k.example.com> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-local-29626w5zfjt/ansible-tmp-1528990630.34-160148825984664/ > /dev/null 2>&1 && sleep 0'                                                                                                                                                              
The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_oiQISX/ansible_module_nxos_linkagg.py", line 398, in <module>
    main()
  File "/tmp/ansible_oiQISX/ansible_module_nxos_linkagg.py", line 385, in main
    have = map_config_to_obj(module)
  File "/tmp/ansible_oiQISX/ansible_module_nxos_linkagg.py", line 329, in map_config_to_obj
    channels = output['TABLE_channel']['ROW_channel']
TypeError: string indices must be integers

fatal: [n9k.example.com]: FAILED! => {
    "changed": false,
    "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_oiQISX/ansible_module_nxos_linkagg.py\", line 398, in <module>\n    main()\n  File \"/tmp/ansible_oiQISX/ansible_module_nxos_linkagg.py\", line 385, in main\n    have = map_config_to_obj(module)\n  File \"/tmp/ansible_oiQISX/ansible_module_nxos_linkagg.py\", line 329, in map_config_to_obj\n    channels = output['TABLE_channel']['ROW_channel']\nTypeError: string indices must be integers\n",
    "module_stdout": "",
    "msg": "MODULE FAILURE",
    "rc": 1
}

```

Note: I will open a PR to address this issue shortly.
nxos_linkagg fails to create linkagg when running on I2 image
<!---
Verify first that your issue/request is not already reported on GitHub.
THIS FORM WILL BE READ BY A MACHINE, COMPLETE ALL SECTIONS AS DESCRIBED.
Also test if the latest release, and devel branch are affected too.
ALWAYS add information AFTER (OUTSIDE) these html comments.
Otherwise it may end up being automatically closed by our bot. -->

##### SUMMARY
<!--- Explain the problem briefly -->

##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
<!--- Insert, BELOW THIS COMMENT, the name of the module, plugin, task or feature.
Do not include extra details here, e.g. "vyos_command" not "the network module vyos_command" or the full path-->
nxos_linkagg
##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
ansible 2.6.0.dev0 (stable-2.6 9ae2050e2f) last updated 2018/06/11 12:10:05 (GMT -400)
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /root/agents-ci/ansible/lib/ansible
  executable location = /root/agents-ci/ansible/bin/ansible
  python version = 2.7.6 (default, Oct 26 2016, 20:30:19) [GCC 4.8.4]
```

##### OS / ENVIRONMENT
<!--- Mention, BELOW THIS COMMENT, the OS you are running Ansible from, and the OS you are
managing, or say "N/A" for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.-->
Ansible Server : Ubuntu 14.04
Device: N9K running 7.0(3)I2(5)

##### STEPS TO REPRODUCE
<!--- For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used. -->

When we run the integration tests on N9k running I2 image, the create_linkagg test fails if there are no port-channels defined already on the device.


##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
TASK [nxos_linkagg : create linkagg] *****************************************************************************************************************************
task path: /root/agents-ci/ansible/test/integration/targets/nxos_linkagg/tests/common/sanity.yaml:39                                                              
<n9k.example.com> ESTABLISH LOCAL CONNECTION FOR USER: root                                                                                                 
<n9k.example.com> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-local-29626w5zfjt/ansible-tmp-1528990630.34-160148825984664 `" && echo ansible-tmp-1528990630.34-160148825984664="` echo /root/.ansible/tmp/ansible-local-29626w5zfjt/ansible-tmp-1528990630.34-160148825984664 `" ) && sleep 0' 
Using module file /root/agents-ci/ansible/lib/ansible/modules/network/nxos/nxos_linkagg.py                                                                        
<n9k.example.com> PUT /root/.ansible/tmp/ansible-local-29626w5zfjt/tmpKqKLUJ TO /root/.ansible/tmp/ansible-local-29626w5zfjt/ansible-tmp-1528990630.34-160148825984664/nxos_linkagg.py                                                                                                                                        
<n9k.example.com> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-local-29626w5zfjt/ansible-tmp-1528990630.34-160148825984664/ /root/.ansible/tmp/ansible-local-29626w5zfjt/ansible-tmp-1528990630.34-160148825984664/nxos_linkagg.py && sleep 0'                                                                       
<n9k.example.com> EXEC /bin/sh -c '/root/p2/bin/python /root/.ansible/tmp/ansible-local-29626w5zfjt/ansible-tmp-1528990630.34-160148825984664/nxos_linkagg.py && sleep 0'                                                                                                                                                     
<n9k.example.com> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-local-29626w5zfjt/ansible-tmp-1528990630.34-160148825984664/ > /dev/null 2>&1 && sleep 0'                                                                                                                                                              
The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_oiQISX/ansible_module_nxos_linkagg.py", line 398, in <module>
    main()
  File "/tmp/ansible_oiQISX/ansible_module_nxos_linkagg.py", line 385, in main
    have = map_config_to_obj(module)
  File "/tmp/ansible_oiQISX/ansible_module_nxos_linkagg.py", line 329, in map_config_to_obj
    channels = output['TABLE_channel']['ROW_channel']
TypeError: string indices must be integers

fatal: [n9k.example.com]: FAILED! => {
    "changed": false,
    "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_oiQISX/ansible_module_nxos_linkagg.py\", line 398, in <module>\n    main()\n  File \"/tmp/ansible_oiQISX/ansible_module_nxos_linkagg.py\", line 385, in main\n    have = map_config_to_obj(module)\n  File \"/tmp/ansible_oiQISX/ansible_module_nxos_linkagg.py\", line 329, in map_config_to_obj\n    channels = output['TABLE_channel']['ROW_channel']\nTypeError: string indices must be integers\n",
    "module_stdout": "",
    "msg": "MODULE FAILURE",
    "rc": 1
}

```

Note: I will open a PR to address this issue shortly.


Discussion:
Files identified in the description:
* [lib/ansible/modules/network/nxos/nxos_linkagg.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/network/nxos/nxos_linkagg.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @mikewiebe @privateip @rahushen @rcarrillocruz @trishnaguha
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
Files identified in the description:
* [lib/ansible/modules/network/nxos/nxos_linkagg.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/network/nxos/nxos_linkagg.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @mikewiebe @privateip @rahushen @rcarrillocruz @trishnaguha
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/lib/ansible/modules/network/nxos/nxos_linkagg.py b/lib/ansible/modules/network/nxos/nxos_linkagg.py
--- a/lib/ansible/modules/network/nxos/nxos_linkagg.py
+++ b/lib/ansible/modules/network/nxos/nxos_linkagg.py
@@ -327,7 +327,7 @@ def map_config_to_obj(module):
 
     try:
         channels = output['TABLE_channel']['ROW_channel']
-    except KeyError:
+    except (TypeError, KeyError):
         return objs
 
     if channels:


Test patch: