Repository: ansible/ansible

Issue:
(P2) nxos_bgp_af idempotence fails when multiple networks are defined
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report

##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
- nxos_bgp_af

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
ansible 2.4.0 (devel ebb1d75ee0) last updated 2017/08/10 15:40:07 (GMT -400)
  config file = None
  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 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.
-->
Ubuntu 14.04 

##### SUMMARY
<!--- Explain the problem briefly -->
nxos_bgp_af module crops the list of networks to the first one when checking for existing networks. This causes idempotence issues.


##### 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.
-->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
---
- name: NXOS Sample Playbook
  hosts: nxos
  connection: local
  gather_facts: no

  tasks:

  - set_fact:
      bgp_af_def:
      - {bgp_af: ipv4, bgp_saf: unicast , adv_net: ['10.114.0.0/16', '10.74.0.0/16', '137.221.96.0/22', '137.221.100.0/22'] }
      - {bgp_af: ipv6, bgp_saf: unicast , adv_net: ['2a04:e800:5010:3623::/64', '2a04:e800:5010:3673::/64'] }

  - name: turn on BGP
    nxos_feature:
      feature: bgp
      state: enabled
      provider: "{{ cli }}"

  - name: BGP Address Family Config
    nxos_bgp_af:
      asn: 65535
      afi: "{{ item.bgp_af }}"
      safi: "{{ item.bgp_saf }}"
      networks: "{{ item.adv_net }}"
      maximum_paths: 8
      maximum_paths_ibgp: 8
      provider: "{{ cli }}"
    with_items: "{{ bgp_af_def }}"
```

<!--- You can also paste gist.github.com links for larger files -->



Discussion:
cc @GGabriele @jedelman8 @mikewiebe @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_bgp_af.py b/lib/ansible/modules/network/nxos/nxos_bgp_af.py
--- a/lib/ansible/modules/network/nxos/nxos_bgp_af.py
+++ b/lib/ansible/modules/network/nxos/nxos_bgp_af.py
@@ -198,7 +198,7 @@
         prefixes to advertise. The list must be in the form of an array.
         Each entry in the array must include a prefix address and an
         optional route-map. For example [['10.0.0.0/16', 'routemap_LA'],
-        ['192.168.1.1', 'Chicago'], ['192.168.2.0/24],
+        ['192.168.1.1', 'Chicago'], ['192.168.2.0/24'],
         ['192.168.3.0/24', 'routemap_NYC']].
     required: false
     default: null
@@ -359,7 +359,12 @@ def get_value(arg, config, module):
             if inject_re.search(config):
                 value.append('copy_attributes')
 
-    elif arg in ['networks', 'redistribute']:
+    elif arg == 'networks':
+        value = []
+        for network in command_val_re.findall(config):
+            value.append(network.split())
+
+    elif arg == 'redistribute':
         value = []
         if has_command_val:
             value = has_command_val.group('value').split()
@@ -367,9 +372,8 @@ def get_value(arg, config, module):
             if value:
                 if len(value) == 3:
                     value.pop(1)
-                elif arg == 'redistribute' and len(value) == 4:
-                    value = ['{0} {1}'.format(
-                        value[0], value[1]), value[3]]
+                elif len(value) == 4:
+                    value = ['{0} {1}'.format(value[0], value[1]), value[3]]
 
     elif command == 'distance':
         distance_re = r'.*distance\s(?P<d_ebgp>\w+)\s(?P<d_ibgp>\w+)\s(?P<d_local>\w+)'


Test patch: