Repository: ansible/ansible

Issue:
nxos_system errors out for both transports (nxapi and cli)
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
 - nxos_system

##### NXOS VERSION
7.0(3)I3(1)

##### ANSIBLE VERSION
```
ansible-playbook 2.4.0 (devel 781bb44b02) last updated 2017/06/08 15:20:16 (GMT -400)
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/usr/share/ansible']
  ansible python module location = /root/agents-ci/ansible/lib/ansible
  executable location = /root/agents-ci/ansible/bin/ansible-playbook
  python version = 2.7.6 (default, Oct 26 2016, 20:30:19) [GCC 4.8.4]
```

##### OS / ENVIRONMENT
running on Ubuntu 14.04

##### SUMMARY
The module errors out in latest `devel` when using transport `nxapi` or `cli`.

##### STEPS TO REPRODUCE
```yaml
---
- name: configure hostname and domain-name
  nxos_system:
    hostname: n9k-109
    domain_name: test.example.com
    provider: "{{ cli }}"

- name: remove configuration
  nxos_system:
    state: absent
    provider: "{{ cli }}"

- name: configure DNS lookup sources
  nxos_system:
    lookup_source: Management1
    provider: "{{ cli }}"

- name: configure name servers
  nxos_system:
    name_servers:
      - 8.8.8.8
      - 8.8.4.4
    provider: "{{ cli }}"

- name: configure name servers with VRF support
  nxos_system:
    name_servers:
      - { server: 8.8.8.8, vrf: mgmt }
      - { server: 8.8.4.4, vrf: mgmt }
    provider: "{{ cli }}"
```

##### EXPECTED RESULTS
Expect nxapi_system module to run properly.

##### ACTUAL RESULTS FOR TRANSPORT CLI or NXAPI

```shell
TASK [nxos_system : debug] ********************************************************************************************************************************************************************************************************************
task path: /root/agents-ci/ansible/test/integration/targets/nxos_system/tests/cli/sanity.yaml:2
ok: [n9k] => {
    "failed": false, 
    "msg": "START TRANSPORT:CLI nxos_system sanity test"
}

TASK [nxos_system : configure hostname and domain-name] ***************************************************************************************************************************************************************************************
task path: /root/agents-ci/ansible/test/integration/targets/nxos_system/tests/cli/sanity.yaml:4
<n9k> connection transport is cli
<n9k> using connection plugin network_cli
<n9k> socket_path: /root/.ansible/pc/4cc10f215f
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/basic.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/network_common.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/netcfg.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/six/__init__.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/nxos.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/_text.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/pycompat24.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/six/_six.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/connection.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/urls.py
Using module file /root/agents-ci/ansible/lib/ansible/modules/network/nxos/nxos_system.py
<n9k> ESTABLISH LOCAL CONNECTION FOR USER: root
<n9k> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1496953782.17-279344047105018 `" && echo ansible-tmp-1496953782.17-279344047105018="` echo $HOME/.ansible/tmp/ansible-tmp-1496953782.17-279344047105018 `" ) && sleep 0'
<n9k> PUT /tmp/tmp25i1mK TO /root/.ansible/tmp/ansible-tmp-1496953782.17-279344047105018/nxos_system.py
<n9k> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1496953782.17-279344047105018/ /root/.ansible/tmp/ansible-tmp-1496953782.17-279344047105018/nxos_system.py && sleep 0'
<n9k> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-tmp-1496953782.17-279344047105018/nxos_system.py; rm -rf "/root/.ansible/tmp/ansible-tmp-1496953782.17-279344047105018/" > /dev/null 2>&1 && sleep 0'
The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_Qc0DMh/ansible_module_nxos_system.py", line 367, in <module>
    main()
  File "/tmp/ansible_Qc0DMh/ansible_module_nxos_system.py", line 354, in main
    have = map_config_to_obj(module)
  File "/tmp/ansible_Qc0DMh/ansible_module_nxos_system.py", line 281, in map_config_to_obj
    'name_servers': parse_name_servers(config, vrf_config),
  File "/tmp/ansible_Qc0DMh/ansible_module_nxos_system.py", line 255, in parse_name_servers
    for addr in match.group(1).split(' '):
AttributeError: 'NoneType' object has no attribute 'group'

fatal: [n9k]: FAILED! => {
    "changed": false, 
    "failed": true, 
    "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_Qc0DMh/ansible_module_nxos_system.py\", line 367, in <module>\n    main()\n  File \"/tmp/ansible_Qc0DMh/ansible_module_nxos_system.py\", line 354, in main\n    have = map_config_to_obj(module)\n  File \"/tmp/ansible_Qc0DMh/ansible_module_nxos_system.py\", line 281, in map_config_to_obj\n    'name_servers': parse_name_servers(config, vrf_config),\n  File \"/tmp/ansible_Qc0DMh/ansible_module_nxos_system.py\", line 255, in parse_name_servers\n    for addr in match.group(1).split(' '):\nAttributeError: 'NoneType' object has no attribute 'group'\n", 
    "module_stdout": "", 
    "msg": "MODULE FAILURE", 
    "rc": 0
}
	to retry, use: --limit @/root/agents-ci/ansible/test/integration/nxos.retry

PLAY RECAP ************************************************************************************************************************************************************************************************************************************
n9k          : ok=5    changed=0    unreachable=0    failed=1   


TASK [nxos_system : debug] ********************************************************************************************************************************************************************************************************************
task path: /root/agents-ci/ansible/test/integration/targets/nxos_system/tests/nxapi/sanity.yaml:2
ok: [n9k] => {
    "failed": false, 
    "msg": "START TRANSPORT:NXAPI nxos_system sanity test"
}

TASK [nxos_system : configure hostname and domain-name] ***************************************************************************************************************************************************************************************
task path: /root/agents-ci/ansible/test/integration/targets/nxos_system/tests/nxapi/sanity.yaml:4
<n9k> connection transport is nxapi
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/basic.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/network_common.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/netcfg.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/six/__init__.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/nxos.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/_text.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/pycompat24.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/six/_six.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/connection.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/urls.py
Using module file /root/agents-ci/ansible/lib/ansible/modules/network/nxos/nxos_system.py
<n9k> ESTABLISH LOCAL CONNECTION FOR USER: root
<n9k> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1496953812.69-88540854688789 `" && echo ansible-tmp-1496953812.69-88540854688789="` echo $HOME/.ansible/tmp/ansible-tmp-1496953812.69-88540854688789 `" ) && sleep 0'
<n9k> PUT /tmp/tmpP2LVu7 TO /root/.ansible/tmp/ansible-tmp-1496953812.69-88540854688789/nxos_system.py
<n9k> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1496953812.69-88540854688789/ /root/.ansible/tmp/ansible-tmp-1496953812.69-88540854688789/nxos_system.py && sleep 0'
<n9k> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-tmp-1496953812.69-88540854688789/nxos_system.py; rm -rf "/root/.ansible/tmp/ansible-tmp-1496953812.69-88540854688789/" > /dev/null 2>&1 && sleep 0'
The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_nfbvjP/ansible_module_nxos_system.py", line 367, in <module>
    main()
  File "/tmp/ansible_nfbvjP/ansible_module_nxos_system.py", line 354, in main
    have = map_config_to_obj(module)
  File "/tmp/ansible_nfbvjP/ansible_module_nxos_system.py", line 281, in map_config_to_obj
    'name_servers': parse_name_servers(config, vrf_config),
  File "/tmp/ansible_nfbvjP/ansible_module_nxos_system.py", line 255, in parse_name_servers
    for addr in match.group(1).split(' '):
AttributeError: 'NoneType' object has no attribute 'group'

fatal: [n9k]: FAILED! => {
    "changed": false, 
    "failed": true, 
    "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_nfbvjP/ansible_module_nxos_system.py\", line 367, in <module>\n    main()\n  File \"/tmp/ansible_nfbvjP/ansible_module_nxos_system.py\", line 354, in main\n    have = map_config_to_obj(module)\n  File \"/tmp/ansible_nfbvjP/ansible_module_nxos_system.py\", line 281, in map_config_to_obj\n    'name_servers': parse_name_servers(config, vrf_config),\n  File \"/tmp/ansible_nfbvjP/ansible_module_nxos_system.py\", line 255, in parse_name_servers\n    for addr in match.group(1).split(' '):\nAttributeError: 'NoneType' object has no attribute 'group'\n", 
    "module_stdout": "", 
    "msg": "MODULE FAILURE", 
    "rc": 0
}
	to retry, use: --limit @/root/agents-ci/ansible/test/integration/nxos.retry

PLAY RECAP ************************************************************************************************************************************************************************************************************************************
n9k          : ok=6    changed=0    unreachable=0    failed=1   

```


Discussion:
cc @jedelman8 @GGabriele @rahushen @privateip @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_system.py b/lib/ansible/modules/network/nxos/nxos_system.py
--- a/lib/ansible/modules/network/nxos/nxos_system.py
+++ b/lib/ansible/modules/network/nxos/nxos_system.py
@@ -87,10 +87,6 @@
   nxos_system:
     state: absent
 
-- name: configure DNS lookup sources
-  nxos_system:
-    lookup_source: Management1
-
 - name: configure name servers
   nxos_system:
     name_servers:
@@ -251,8 +247,9 @@ def parse_name_servers(config, vrf_config):
             objects.append({'server': addr, 'vrf': None})
 
     for vrf, cfg in iteritems(vrf_config):
-        for item in re.findall('ip name-server (\S+)', cfg, re.M):
-            for addr in match.group(1).split(' '):
+        vrf_match = re.search('ip name-server (.+)', cfg, re.M)
+        if vrf_match:
+            for addr in vrf_match.group(1).split(' '):
                 objects.append({'server': addr, 'vrf': vrf})
 
     return objects
@@ -334,7 +331,7 @@ def main():
         name_servers=dict(type='list'),
 
         system_mtu=dict(type='int'),
-
+        lookup_source=dict(),
         state=dict(default='present', choices=['present', 'absent'])
     )
 


Test patch: